mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 19:25:49 +03:00
Oops. Tests.
This commit is contained in:
parent
f04ff063ed
commit
a946d65fe6
3 changed files with 8 additions and 5 deletions
|
@ -80,7 +80,6 @@ func TestGzipHandler(t *testing.T) {
|
||||||
|
|
||||||
func nextFunc(shouldGzip bool) middleware.Handler {
|
func nextFunc(shouldGzip bool) middleware.Handler {
|
||||||
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
w.WriteHeader(200)
|
|
||||||
w.Write([]byte("test"))
|
w.Write([]byte("test"))
|
||||||
if shouldGzip {
|
if shouldGzip {
|
||||||
if r.Header.Get("Accept-Encoding") != "" {
|
if r.Header.Get("Accept-Encoding") != "" {
|
||||||
|
|
|
@ -31,6 +31,7 @@ func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool {
|
||||||
type ResponseFilterWriter struct {
|
type ResponseFilterWriter struct {
|
||||||
filters []ResponseFilter
|
filters []ResponseFilter
|
||||||
shouldCompress bool
|
shouldCompress bool
|
||||||
|
statusCodeWritten bool
|
||||||
*gzipResponseWriter
|
*gzipResponseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +40,7 @@ func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *
|
||||||
return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
|
return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write wraps underlying Write method and compresses if filters
|
// Write wraps underlying WriteHeader method and compresses if filters
|
||||||
// are satisfied.
|
// are satisfied.
|
||||||
func (r *ResponseFilterWriter) WriteHeader(code int) {
|
func (r *ResponseFilterWriter) WriteHeader(code int) {
|
||||||
// Determine if compression should be used or not.
|
// Determine if compression should be used or not.
|
||||||
|
@ -62,11 +63,15 @@ func (r *ResponseFilterWriter) WriteHeader(code int) {
|
||||||
} else {
|
} else {
|
||||||
r.ResponseWriter.WriteHeader(code)
|
r.ResponseWriter.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
r.statusCodeWritten = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write wraps underlying Write method and compresses if filters
|
// Write wraps underlying Write method and compresses if filters
|
||||||
// are satisfied
|
// are satisfied
|
||||||
func (r *ResponseFilterWriter) Write(b []byte) (int, error) {
|
func (r *ResponseFilterWriter) Write(b []byte) (int, error) {
|
||||||
|
if !r.statusCodeWritten {
|
||||||
|
r.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
if r.shouldCompress {
|
if r.shouldCompress {
|
||||||
return r.gzipResponseWriter.Write(b)
|
return r.gzipResponseWriter.Write(b)
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,6 @@ func TestResponseFilterWriter(t *testing.T) {
|
||||||
for i, ts := range tests {
|
for i, ts := range tests {
|
||||||
server.Next = middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
server.Next = middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
w.Header().Set("Content-Length", fmt.Sprint(len(ts.body)))
|
w.Header().Set("Content-Length", fmt.Sprint(len(ts.body)))
|
||||||
w.WriteHeader(200)
|
|
||||||
w.Write([]byte(ts.body))
|
w.Write([]byte(ts.body))
|
||||||
return 200, nil
|
return 200, nil
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue