|
5 | 5 | package handlers |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "bufio" |
8 | 9 | "io" |
| 10 | + "net" |
9 | 11 | "net/http" |
10 | 12 | "net/http/httptest" |
11 | 13 | "strconv" |
@@ -88,3 +90,65 @@ func TestCompressHandlerGzipDeflate(t *testing.T) { |
88 | 90 | t.Fatalf("wrong content type, got %s want %s", w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8") |
89 | 91 | } |
90 | 92 | } |
| 93 | + |
| 94 | +type fullyFeaturedResponseWriter struct{} |
| 95 | + |
| 96 | +// Header/Write/WriteHeader implement the http.ResponseWriter interface. |
| 97 | +func (fullyFeaturedResponseWriter) Header() http.Header { |
| 98 | + return http.Header{} |
| 99 | +} |
| 100 | +func (fullyFeaturedResponseWriter) Write([]byte) (int, error) { |
| 101 | + return 0, nil |
| 102 | +} |
| 103 | +func (fullyFeaturedResponseWriter) WriteHeader(int) {} |
| 104 | + |
| 105 | +// Flush implements the http.Flusher interface. |
| 106 | +func (fullyFeaturedResponseWriter) Flush() {} |
| 107 | + |
| 108 | +// Hijack implements the http.Hijacker interface. |
| 109 | +func (fullyFeaturedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 110 | + return nil, nil, nil |
| 111 | +} |
| 112 | + |
| 113 | +// CloseNotify implements the http.CloseNotifier interface. |
| 114 | +func (fullyFeaturedResponseWriter) CloseNotify() <-chan bool { |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func TestCompressHandlerPreserveInterfaces(t *testing.T) { |
| 119 | + // Compile time validation fullyFeaturedResponseWriter implements all the |
| 120 | + // interfaces we're asserting in the test case below. |
| 121 | + var ( |
| 122 | + _ http.Flusher = fullyFeaturedResponseWriter{} |
| 123 | + _ http.CloseNotifier = fullyFeaturedResponseWriter{} |
| 124 | + _ http.Hijacker = fullyFeaturedResponseWriter{} |
| 125 | + ) |
| 126 | + var h http.Handler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 127 | + comp := r.Header.Get("Accept-Encoding") |
| 128 | + if _, ok := rw.(*compressResponseWriter); !ok { |
| 129 | + t.Fatalf("ResponseWriter wasn't wrapped by compressResponseWriter, got %T type", rw) |
| 130 | + } |
| 131 | + if _, ok := rw.(http.Flusher); !ok { |
| 132 | + t.Errorf("ResponseWriter lost http.Flusher interface for %q", comp) |
| 133 | + } |
| 134 | + if _, ok := rw.(http.CloseNotifier); !ok { |
| 135 | + t.Errorf("ResponseWriter lost http.CloseNotifier interface for %q", comp) |
| 136 | + } |
| 137 | + if _, ok := rw.(http.Hijacker); !ok { |
| 138 | + t.Errorf("ResponseWriter lost http.Hijacker interface for %q", comp) |
| 139 | + } |
| 140 | + }) |
| 141 | + h = CompressHandler(h) |
| 142 | + var ( |
| 143 | + rw fullyFeaturedResponseWriter |
| 144 | + ) |
| 145 | + r, err := http.NewRequest("GET", "/", nil) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("Failed to create test request: %v", err) |
| 148 | + } |
| 149 | + r.Header.Set("Accept-Encoding", "gzip") |
| 150 | + h.ServeHTTP(rw, r) |
| 151 | + |
| 152 | + r.Header.Set("Accept-Encoding", "deflate") |
| 153 | + h.ServeHTTP(rw, r) |
| 154 | +} |
0 commit comments