this repo has no description
1package httpencoding
2
3import (
4 "io"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8)
9
10var body = "hello world"
11
12func TestHandler(t *testing.T) {
13 svr := httptest.NewServer(Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14 w.Write([]byte(body))
15 })))
16 defer svr.Close()
17
18 for _, coding := range []string{"zstd", "gzip"} {
19 t.Run(coding, func(t *testing.T) {
20 req, err := http.NewRequestWithContext(t.Context(), "GET", svr.URL, http.NoBody)
21 if err != nil {
22 t.Fatalf("create request: %v", err)
23 }
24 req.Header.Set("accept-encoding", coding)
25
26 res, err := svr.Client().Do(req)
27 if err != nil {
28 t.Fatalf("send request: %v", err)
29 }
30 if res.StatusCode != 200 {
31 t.Errorf("response code: %v", res.Status)
32 }
33
34 if got := res.Header.Get("content-encoding"); got != coding {
35 t.Errorf("unexpected content-encoding: %v", got)
36 }
37 defer res.Body.Close()
38
39 _, err = io.ReadAll(res.Body)
40 if err != nil {
41 t.Errorf("read response: %v", err)
42 }
43 })
44 }
45}