MCPcopy
hub / github.com/valyala/fasthttp / TestFSServeFileCompressed

Function TestFSServeFileCompressed

fs_fs_test.go:155–265  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

153}
154
155func TestFSServeFileCompressed(t *testing.T) {
156 t.Parallel()
157
158 var ctx RequestCtx
159 ctx.Init(&Request{}, nil, nil)
160
161 var resp Response
162
163 expectedBody, err := getFileContents("/fs.go")
164 if err != nil {
165 t.Fatalf("unexpected error: %v", err)
166 }
167
168 // should prefer brotli over zstd, gzip and ignore unknown encoding
169 ctx.Request.SetRequestURI("http://foobar.com/baz")
170 ctx.Request.Header.Set(HeaderAcceptEncoding, "gzip, zstd, br, wompwomp")
171 ServeFS(&ctx, fsTestFilesystem, "fs.go")
172
173 s := ctx.Response.String()
174 br := bufio.NewReader(bytes.NewBufferString(s))
175 if err = resp.Read(br); err != nil {
176 t.Fatalf("unexpected error: %v", err)
177 }
178 if resp.StatusCode() != StatusOK {
179 t.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusOK)
180 }
181
182 ce := resp.Header.ContentEncoding()
183 if string(ce) != "br" {
184 t.Fatalf("unexpected 'Content-Encoding' %q. Expecting %q", string(ce), "br")
185 }
186
187 vary := resp.Header.PeekBytes(strVary)
188 if !bytes.Equal(vary, strAcceptEncoding) {
189 t.Fatalf("unexpected 'Vary': %q. Expecting %q", string(vary), HeaderAcceptEncoding)
190 }
191
192 body, err := resp.BodyUnbrotli()
193 if err != nil {
194 t.Fatalf("unexpected error on unbrotli response body: %v", err)
195 }
196 if !bytes.Equal(body, expectedBody) {
197 t.Fatalf("unexpected body: len=%d. Expected len=%d", len(body), len(expectedBody))
198 }
199
200 // should prefer zstd over gzip and ignore unknown encoding
201 ctx.Request.Reset()
202 ctx.Request.SetRequestURI("http://foobar.com/baz")
203 ctx.Request.Header.Set(HeaderAcceptEncoding, "gzip, zstd, wompwomp")
204 ServeFS(&ctx, fsTestFilesystem, "fs.go")
205
206 s = ctx.Response.String()
207 br = bufio.NewReader(bytes.NewBufferString(s))
208 if err = resp.Read(br); err != nil {
209 t.Fatalf("unexpected error: %v", err)
210 }
211 if resp.StatusCode() != StatusOK {
212 t.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusOK)

Callers

nothing calls this directly

Calls 15

InitMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
BodyUnbrotliMethod · 0.95
BodyUnzstdMethod · 0.95
BodyGunzipMethod · 0.95
getFileContentsFunction · 0.85
ServeFSFunction · 0.85
NewReaderMethod · 0.80
ResetMethod · 0.65
SetRequestURIMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…