MCPcopy
hub / github.com/labstack/echo / TestGzipWithMinLengthChunked

Function TestGzipWithMinLengthChunked

middleware/compress_test.go:276–333  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

274}
275
276func TestGzipWithMinLengthChunked(t *testing.T) {
277 e := echo.New()
278
279 // Gzip chunked
280 chunkBuf := make([]byte, 5)
281
282 req := httptest.NewRequest(http.MethodGet, "/", nil)
283 req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
284 rec := httptest.NewRecorder()
285
286 var r *gzip.Reader = nil
287
288 c := e.NewContext(req, rec)
289 next := func(c *echo.Context) error {
290 rc := http.NewResponseController(c.Response())
291 c.Response().Header().Set("Content-Type", "text/event-stream")
292 c.Response().Header().Set("Transfer-Encoding", "chunked")
293
294 // Write and flush the first part of the data
295 c.Response().Write([]byte("test\n"))
296 rc.Flush()
297
298 // Read the first part of the data
299 assert.True(t, rec.Flushed)
300 assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
301
302 var err error
303 r, err = gzip.NewReader(rec.Body)
304 assert.NoError(t, err)
305
306 _, err = io.ReadFull(r, chunkBuf)
307 assert.NoError(t, err)
308 assert.Equal(t, "test\n", string(chunkBuf))
309
310 // Write and flush the second part of the data
311 c.Response().Write([]byte("test\n"))
312 rc.Flush()
313
314 _, err = io.ReadFull(r, chunkBuf)
315 assert.NoError(t, err)
316 assert.Equal(t, "test\n", string(chunkBuf))
317
318 // Write the final part of the data and return
319 c.Response().Write([]byte("test"))
320 return nil
321 }
322 err := GzipWithConfig(GzipConfig{MinLength: 10})(next)(c)
323
324 assert.NoError(t, err)
325 assert.NotNil(t, r)
326
327 buf := new(bytes.Buffer)
328
329 buf.ReadFrom(r)
330 assert.Equal(t, "test", buf.String())
331
332 r.Close()
333}

Callers

nothing calls this directly

Calls 10

ResponseMethod · 0.95
GzipWithConfigFunction · 0.85
SetMethod · 0.80
NewContextMethod · 0.80
HeaderMethod · 0.45
WriteMethod · 0.45
FlushMethod · 0.45
GetMethod · 0.45
StringMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…