-
Notifications
You must be signed in to change notification settings - Fork 38
/
http_test.go
80 lines (63 loc) · 1.95 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package dry
import (
"compress/gzip"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHTTPCompressHandlerFunc(t *testing.T) {
for i := 0; i < 100; i++ {
handlerFunc := HTTPCompressHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world!")
})
request, err := http.NewRequest("GET", "/foobar", nil)
if err != nil {
t.Fatalf("http.NewRequest failed: %v", err)
}
request.Header.Set("Accept-Encoding", "gzip, deflate")
responseWriter := httptest.NewRecorder()
handlerFunc.ServeHTTP(responseWriter, request)
t.Logf("responseWriter.Body = %v", responseWriter.Body.Bytes())
reader, err := gzip.NewReader(responseWriter.Body)
if err != nil {
t.Fatalf("gzip.NewReader failed: %v", err)
}
readData, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("Reading from body failed: %v", err)
}
if string(readData) != "hello world!" {
t.Fatalf("Body content: expected \"hello world!\", got %s instead.", string(readData))
}
}
}
func TestHTTPCompressHandler(t *testing.T) {
for i := 0; i < 100; i++ {
handler := &HTTPCompressHandler{&helloWorldHandler{}}
request, err := http.NewRequest("GET", "/foobar", nil)
if err != nil {
t.Fatalf("http.NewRequest failed: %v", err)
}
request.Header.Set("Accept-Encoding", "gzip")
responseWriter := httptest.NewRecorder()
handler.ServeHTTP(responseWriter, request)
t.Logf("responseWriter.Body = %v", responseWriter.Body.Bytes())
reader, err := gzip.NewReader(responseWriter.Body)
if err != nil {
t.Fatalf("gzip.NewReader failed: %v", err)
}
readData, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("Reading from body failed: %v", err)
}
if string(readData) != "hallo welt." {
t.Fatalf("Body content: expected \"hallo welt.\", got %s instead.", string(readData))
}
}
}
type helloWorldHandler struct{}
func (h *helloWorldHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hallo welt.")
}