MCPcopy
hub / github.com/tdewolff/minify / TestResponseWriter

Function TestResponseWriter

minify_test.go:245–283  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

243}
244
245func 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
285func TestMiddleware(t *testing.T) {
286 m := New()

Callers

nothing calls this directly

Calls 9

WriteMethod · 0.95
NewFunction · 0.85
AddFuncMethod · 0.80
ResponseWriterMethod · 0.80
AddMethod · 0.80
HeaderMethod · 0.80
CloseMethod · 0.45
StringMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…