-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkg_test.go
133 lines (124 loc) · 3.57 KB
/
pkg_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package zipserver_test
import (
"archive/zip"
"bytes"
"compress/flate"
"io"
"mime"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"artyom.dev/zipserver"
)
func TestHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost/"+testFileName, nil)
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
w := httptest.NewRecorder()
zipserver.Handler(zipFile()).ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status: %s", resp.Status)
}
if s := resp.Header.Get("Vary"); s != "Accept-Encoding" {
t.Fatalf("unexpected Vary value: %q", s)
}
if s := resp.Header.Get("Content-Encoding"); s != "deflate" {
t.Fatalf("unexpected Content-Encoding value (want deflate): %q", s)
}
if s := resp.Header.Get("Content-Type"); s != "text/plain; charset=utf-8" {
t.Fatalf("unexpected Content-Type value: %q", s)
}
fr := flate.NewReader(resp.Body)
defer fr.Close()
got, err := io.ReadAll(fr)
if err != nil {
t.Fatalf("decompressing body: %v", err)
}
want, err := os.ReadFile(testFileName)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Fatalf("payloads differ (got %d bytes, want %d bytes)", len(got), len(want))
}
}
func TestHandler_seekableFile(t *testing.T) {
if ct := mime.TypeByExtension(path.Ext(testFileNoSuffix)); ct != "" {
t.Fatalf("got non-empty mime type for file named %q: %q", testFileNoSuffix, ct)
}
req := httptest.NewRequest(http.MethodGet, "http://localhost/"+testFileNoSuffix, nil)
w := httptest.NewRecorder()
zipserver.Handler(zipFile()).ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status: %s", resp.Status)
}
if s := resp.Header.Get("Vary"); s != "Accept-Encoding" {
t.Fatalf("unexpected Vary value: %q", s)
}
if s := resp.Header.Get("Content-Type"); s != "text/plain; charset=utf-8" {
t.Fatalf("unexpected Content-Type value: %q", s)
}
if resp.Uncompressed {
t.Fatal("got automatically uncompressed response")
}
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("reading body: %v", err)
}
want, err := os.ReadFile(testFileName)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Fatalf("payloads differ (got %d bytes, want %d bytes)", len(got), len(want))
}
}
type noopResponseWriter struct{ hdr http.Header }
func (w *noopResponseWriter) WriteHeader(int) {}
func (w *noopResponseWriter) Header() http.Header { return w.hdr }
func (w *noopResponseWriter) Write(b []byte) (int, error) { return len(b), nil }
func BenchmarkHandler(b *testing.B) {
handler := zipserver.Handler(zipFile())
req := httptest.NewRequest(http.MethodGet, "http://localhost/"+testFileName, nil)
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
w := &noopResponseWriter{hdr: make(http.Header)}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(w.hdr)
handler.ServeHTTP(w, req)
}
}
const testFileName = "LICENSE.txt"
const testFileNoSuffix = "unknown"
func zipFile() *zip.Reader {
buf := new(bytes.Buffer)
b, err := os.ReadFile(testFileName)
if err != nil {
panic(err)
}
zw := zip.NewWriter(buf)
for _, name := range [...]string{testFileName, testFileNoSuffix} {
w, err := zw.CreateHeader(&zip.FileHeader{Name: name, Method: zip.Deflate})
if err != nil {
panic(err)
}
if _, err := w.Write(b); err != nil {
panic(err)
}
}
if err := zw.Close(); err != nil {
panic(err)
}
rd := bytes.NewReader(buf.Bytes())
zr, err := zip.NewReader(rd, rd.Size())
if err != nil {
panic(err)
}
return zr
}