(t *testing.T)
| 153 | } |
| 154 | |
| 155 | func 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) |
nothing calls this directly
no test coverage detected
searching dependent graphs…