MCPcopy Index your code
hub / github.com/labstack/echo / TestGzip_chunked

Function TestGzip_chunked

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

Source from the content-addressed store, hash-verified

69}
70
71func TestGzip_chunked(t *testing.T) {
72 e := echo.New()
73 req := httptest.NewRequest(http.MethodGet, "/", nil)
74 req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
75 rec := httptest.NewRecorder()
76 c := e.NewContext(req, rec)
77
78 chunkChan := make(chan struct{})
79 waitChan := make(chan struct{})
80 h := Gzip()(func(c *echo.Context) error {
81 rc := http.NewResponseController(c.Response())
82 c.Response().Header().Set("Content-Type", "text/event-stream")
83 c.Response().Header().Set("Transfer-Encoding", "chunked")
84
85 // Write and flush the first part of the data
86 c.Response().Write([]byte("first\n"))
87 rc.Flush()
88
89 chunkChan <- struct{}{}
90 <-waitChan
91
92 // Write and flush the second part of the data
93 c.Response().Write([]byte("second\n"))
94 rc.Flush()
95
96 chunkChan <- struct{}{}
97 <-waitChan
98
99 // Write the final part of the data and return
100 c.Response().Write([]byte("third"))
101
102 chunkChan <- struct{}{}
103 return nil
104 })
105
106 go func() {
107 err := h(c)
108 chunkChan <- struct{}{}
109 assert.NoError(t, err)
110 }()
111
112 <-chunkChan // wait for first write
113 waitChan <- struct{}{}
114
115 <-chunkChan // wait for second write
116 waitChan <- struct{}{}
117
118 <-chunkChan // wait for final write in handler
119 <-chunkChan // wait for return from handler
120 time.Sleep(5 * time.Millisecond) // to have time for flushing
121
122 assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
123
124 r, err := gzip.NewReader(rec.Body)
125 assert.NoError(t, err)
126 buf := new(bytes.Buffer)
127 buf.ReadFrom(r)
128 assert.Equal(t, "first\nsecond\nthird", buf.String())

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…