-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch_test.go
363 lines (313 loc) · 8.95 KB
/
patch_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package tusd_test
import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd"
)
func TestPatch(t *testing.T) {
SubTest(t, "UploadChunk", func(t *testing.T, store *MockFullDataStore) {
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 10,
}, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
store.EXPECT().FinishUpload("yes"),
)
handler, _ := NewHandler(Config{
DataStore: store,
NotifyCompleteUploads: true,
})
c := make(chan FileInfo, 1)
handler.CompleteUploads = c
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "5",
},
ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "10",
},
}).Run(handler, t)
a := assert.New(t)
info := <-c
a.Equal("yes", info.ID)
a.EqualValues(int64(10), info.Size)
a.Equal(int64(10), info.Offset)
})
SubTest(t, "MethodOverriding", func(t *testing.T, store *MockFullDataStore) {
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 20,
}, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "POST",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Upload-Offset": "5",
"Content-Type": "application/offset+octet-stream",
"X-HTTP-Method-Override": "PATCH",
},
ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "10",
},
}).Run(handler, t)
})
SubTest(t, "UploadChunkToFinished", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 20,
Size: 20,
}, nil)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "20",
},
ReqBody: strings.NewReader(""),
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "20",
},
}).Run(handler, t)
})
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "no",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "5",
},
Code: http.StatusNotFound,
}).Run(handler, t)
})
SubTest(t, "MissmatchingOffsetFail", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
}, nil)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "4",
},
Code: http.StatusConflict,
}).Run(handler, t)
})
SubTest(t, "ExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Size: 10,
}, nil)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "5",
},
ReqBody: strings.NewReader("hellothisismorethan15bytes"),
Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t)
})
SubTest(t, "InvalidContentTypeFail", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/fail",
"Upload-Offset": "5",
},
ReqBody: strings.NewReader("hellothisismorethan15bytes"),
Code: http.StatusBadRequest,
}).Run(handler, t)
})
SubTest(t, "InvalidOffsetFail", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "-5",
},
ReqBody: strings.NewReader("hellothisismorethan15bytes"),
Code: http.StatusBadRequest,
}).Run(handler, t)
})
SubTest(t, "OverflowWithoutLength", func(t *testing.T, store *MockFullDataStore) {
// In this test we attempt to upload more than 15 bytes to an upload
// which has only space for 15 bytes (offset of 5 and size of 20).
// The request does not contain the Content-Length header and the handler
// therefore does not know the chunk's size before. The wanted behavior
// is that even if the uploader supplies more than 15 bytes, we only
// pass 15 bytes to the data store and ignore the rest.
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Size: 20,
}, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
store.EXPECT().FinishUpload("yes"),
)
handler, _ := NewHandler(Config{
DataStore: store,
})
// Wrap the string.Reader in a NopCloser to hide its type. else
// http.NewRequest() will detect the we supply a strings.Reader as body
// and use this information to set the Content-Length header which we
// explicitly do not want (see comment above for reason).
body := ioutil.NopCloser(strings.NewReader("hellothisismorethan15bytes"))
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "5",
},
ReqBody: body,
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "20",
},
}).Run(handler, t)
})
SubTest(t, "Locker", func(t *testing.T, store *MockFullDataStore) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("yes").Return(nil),
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 0,
Size: 20,
}, nil),
store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
locker.EXPECT().UnlockUpload("yes").Return(nil),
)
composer := NewStoreComposer()
composer.UseCore(store)
composer.UseLocker(locker)
handler, _ := NewHandler(Config{
StoreComposer: composer,
})
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "0",
},
ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent,
}).Run(handler, t)
})
SubTest(t, "NotifyUploadProgress", func(t *testing.T, store *MockFullDataStore) {
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
ID: "yes",
Offset: 0,
Size: 100,
}, nil),
store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil),
)
handler, _ := NewHandler(Config{
DataStore: store,
NotifyUploadProgress: true,
})
c := make(chan FileInfo)
handler.UploadProgress = c
reader, writer := io.Pipe()
a := assert.New(t)
go func() {
writer.Write([]byte("first "))
info := <-c
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(6), info.Offset)
writer.Write([]byte("second "))
writer.Write([]byte("third"))
info = <-c
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(18), info.Offset)
writer.Close()
info = <-c
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(18), info.Offset)
}()
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "0",
},
ReqBody: reader,
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "18",
},
}).Run(handler, t)
// Wait a short time after the request has been handled before closing the
// channel because another goroutine may still write to the channel.
<-time.After(10 * time.Millisecond)
close(handler.UploadProgress)
_, more := <-c
a.False(more)
})
}