(t *testing.T)
| 243 | } |
| 244 | |
| 245 | func TestResponseWriter(t *testing.T) { |
| 246 | m := New() |
| 247 | m.AddFunc("text/html", func(m *M, w io.Writer, r io.Reader, _ map[string]string) error { |
| 248 | _, _ = io.ReadAll(r) |
| 249 | _, err := w.Write([]byte("minified")) |
| 250 | return err |
| 251 | }) |
| 252 | |
| 253 | // use file extension |
| 254 | b := &bytes.Buffer{} |
| 255 | w := &testResponseWriter{b, http.Header{}} |
| 256 | r := &http.Request{RequestURI: "/index.html"} |
| 257 | mw := m.ResponseWriter(w, r) |
| 258 | _, err := mw.Write([]byte("input")) |
| 259 | test.Error(t, mw.Close()) |
| 260 | test.String(t, b.String(), "minified") |
| 261 | _, err = mw.Write([]byte("test")) |
| 262 | test.T(t, err, io.ErrClosedPipe) |
| 263 | |
| 264 | // use Content-Type header |
| 265 | b = &bytes.Buffer{} |
| 266 | w = &testResponseWriter{b, http.Header{}} |
| 267 | r = &http.Request{RequestURI: "/index"} |
| 268 | mw = m.ResponseWriter(w, r) |
| 269 | mw.Header().Add("Content-Type", "text/html") |
| 270 | _, _ = mw.Write([]byte("input")) |
| 271 | mw.WriteHeader(http.StatusForbidden) |
| 272 | test.Error(t, mw.Close()) |
| 273 | test.String(t, b.String(), "minified") |
| 274 | |
| 275 | // don't minify |
| 276 | b = &bytes.Buffer{} |
| 277 | w = &testResponseWriter{b, http.Header{}} |
| 278 | r = &http.Request{RequestURI: "/image.png"} |
| 279 | mw = m.ResponseWriter(w, r) |
| 280 | _, _ = mw.Write([]byte("input")) |
| 281 | test.Error(t, mw.Close()) |
| 282 | test.String(t, b.String(), "input") |
| 283 | } |
| 284 | |
| 285 | func TestMiddleware(t *testing.T) { |
| 286 | m := New() |
nothing calls this directly
no test coverage detected
searching dependent graphs…