MCPcopy Index your code
hub / github.com/coder/coder / TestCompressorHeadings

Function TestCompressorHeadings

coderd/cachecompress/compress_internal_test.go:194–262  ·  view source on GitHub ↗

nolint: tparallel // we want to assert the state of the cache, so run synchronously

(t *testing.T)

Source from the content-addressed store, hash-verified

192
193// nolint: tparallel // we want to assert the state of the cache, so run synchronously
194func TestCompressorHeadings(t *testing.T) {
195 t.Parallel()
196 logger := testutil.Logger(t)
197 tempDir := t.TempDir()
198 cacheDir := filepath.Join(tempDir, "cache")
199 err := os.MkdirAll(cacheDir, 0o700)
200 require.NoError(t, err)
201 srcDir := filepath.Join(tempDir, "src")
202 err = os.MkdirAll(srcDir, 0o700)
203 require.NoError(t, err)
204 err = os.WriteFile(filepath.Join(srcDir, "file.html"), []byte("textstring"), 0o600)
205 require.NoError(t, err)
206
207 compressor := NewCompressor(logger, 5, cacheDir, http.FS(os.DirFS(srcDir)))
208
209 ts := httptest.NewServer(compressor)
210 defer ts.Close()
211
212 tests := []struct {
213 name string
214 path string
215 }{
216 {
217 name: "exists",
218 path: "/file.html",
219 },
220 {
221 name: "not found",
222 path: "/missing.html",
223 },
224 {
225 name: "not found directory",
226 path: "/a_directory/",
227 },
228 }
229
230 // nolint: paralleltest // we want to assert the state of the cache, so run synchronously
231 for _, tc := range tests {
232 t.Run(tc.name, func(t *testing.T) {
233 ctx := testutil.Context(t, testutil.WaitShort)
234 req := httptest.NewRequestWithContext(ctx, "GET", tc.path, nil)
235
236 // request directly from http.FileServer as our baseline response
237 respROrig := httptest.NewRecorder()
238 http.FileServer(http.Dir(srcDir)).ServeHTTP(respROrig, req)
239 respOrig := respROrig.Result()
240
241 req.Header.Add("Accept-Encoding", "gzip")
242 // serve twice so that we go thru cache hit and cache miss code
243 for range 2 {
244 respRec := httptest.NewRecorder()
245 compressor.ServeHTTP(respRec, req)
246 respComp := respRec.Result()
247
248 require.Equal(t, respOrig.StatusCode, respComp.StatusCode)
249 for key, values := range respOrig.Header {
250 if key == "Content-Length" {
251 continue // we don't get length on compressed responses

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
ServeHTTPMethod · 0.95
LoggerFunction · 0.92
ContextFunction · 0.92
NewCompressorFunction · 0.85
MkdirAllMethod · 0.80
ReadDirMethod · 0.80
TempDirMethod · 0.65
WriteFileMethod · 0.65
RunMethod · 0.65
AddMethod · 0.65
ResultMethod · 0.45

Tested by

no test coverage detected