-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
220 lines (171 loc) · 5.88 KB
/
server_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main_test
import (
"bytes"
"context"
"crypto/rand"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
main "github.com/lesomnus/vcpkg-cache-http"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)
func WithHandler(f func(t *testing.T, store main.Store, handler *main.Handler)) func(*testing.T) {
return func(t *testing.T) {
require := require.New(t)
store, err := NewTestFsStore(t)
require.NoError(err)
handler := main.Handler{
Store: store,
Log: zerolog.New(io.Discard),
IsReadable: true,
IsWritable: true,
}
f(t, store, &handler)
}
}
func randomData(t *testing.T) []byte {
require := require.New(t)
data := make([]byte, 128)
_, err := rand.Read(data)
require.NoError(err)
return data
}
func TestServerProbe(t *testing.T) {
WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
require.HTTPStatusCode(handler.ServeHTTP, http.MethodGet, "/", nil, http.StatusOK)
})(t)
}
func TestServerGet(t *testing.T) {
t.Run("200 if cache hit", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
data := randomData(t)
err := store.Put(context.Background(), DescriptionFoo, bytes.NewReader(data))
require.NoError(err)
req := httptest.NewRequest(http.MethodGet, DescriptionFoo.String(), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusOK, res.StatusCode)
received, err := io.ReadAll(res.Body)
require.NoError(err)
require.Equal(data, received)
}))
t.Run("404 if cache not exists", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
ctx := context.Background()
err := store.Get(ctx, DescriptionFoo, io.Discard)
require.ErrorIs(err, main.ErrNotExist)
require.HTTPStatusCode(handler.ServeHTTP, http.MethodGet, DescriptionFoo.String(), nil, http.StatusNotFound)
}))
t.Run("404 if path invalid", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
paths := []string{
"/foo",
"/foo/bar",
"/foo/bar/baz/qux",
}
for _, path := range paths {
require.HTTPStatusCode(handler.ServeHTTP, http.MethodGet, path, nil, http.StatusNotFound)
}
}))
t.Run("405 if not readable", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
data := randomData(t)
ctx := context.Background()
err := store.Put(ctx, DescriptionFoo, bytes.NewReader(data))
require.NoError(err)
handler.IsReadable = false
require.HTTPStatusCode(handler.ServeHTTP, http.MethodGet, DescriptionFoo.String(), nil, http.StatusMethodNotAllowed)
}))
}
func TestServerHead(t *testing.T) {
t.Run("200 if cache exists", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
data := randomData(t)
ctx := context.Background()
err := store.Put(ctx, DescriptionFoo, bytes.NewReader(data))
require.NoError(err)
req := httptest.NewRequest(http.MethodHead, DescriptionFoo.String(), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusOK, res.StatusCode)
require.Equal(strconv.FormatInt(int64(len(data)), 10), res.Header.Get("Content-Length"))
}))
t.Run("404 if cache not exists", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
ctx := context.Background()
_, err := store.Head(ctx, DescriptionFoo)
require.ErrorIs(err, main.ErrNotExist)
require.HTTPStatusCode(handler.ServeHTTP, http.MethodHead, DescriptionFoo.String(), nil, http.StatusNotFound)
}))
}
func TestServerPut(t *testing.T) {
t.Run("cache will hit after PUT", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
data := randomData(t)
req := httptest.NewRequest(http.MethodPut, DescriptionFoo.String(), bytes.NewReader(data))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusOK, res.StatusCode)
var received bytes.Buffer
err := store.Get(context.Background(), DescriptionFoo, &received)
require.NoError(err)
require.Equal(data, received.Bytes())
}))
t.Run("405 if not readable", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
handler.IsWritable = false
data := randomData(t)
req := httptest.NewRequest(http.MethodPut, DescriptionFoo.String(), bytes.NewReader(data))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusMethodNotAllowed, res.StatusCode)
err := store.Get(context.Background(), DescriptionFoo, io.Discard)
require.ErrorIs(err, main.ErrNotExist)
}))
t.Run("409 if cache already exist", WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
require := require.New(t)
data := randomData(t)
{
req := httptest.NewRequest(http.MethodPut, DescriptionFoo.String(), bytes.NewReader(data))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusOK, res.StatusCode)
}
{
req := httptest.NewRequest(http.MethodPut, DescriptionFoo.String(), bytes.NewReader(data))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(http.StatusConflict, res.StatusCode)
}
}))
}
func TestServerInvalidMethod(t *testing.T) {
require := require.New(t)
methods := []string{
// http.MethodGet,
// http.MethodHead,
http.MethodPost,
// http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
"FOO",
}
WithHandler(func(t *testing.T, store main.Store, handler *main.Handler) {
for _, method := range methods {
require.HTTPStatusCode(handler.ServeHTTP, method, DescriptionFoo.String(), nil, http.StatusNotImplemented)
}
})(t)
}