-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_test.go
108 lines (91 loc) · 2.23 KB
/
get_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
package tusd_test
import (
"net/http"
"strings"
"testing"
"github.com/golang/mock/gomock"
. "github.com/tus/tusd"
)
type closingStringReader struct {
*strings.Reader
closed bool
}
func (reader *closingStringReader) Close() error {
reader.closed = true
return nil
}
func TestGet(t *testing.T) {
SubTest(t, "Download", func(t *testing.T, store *MockFullDataStore) {
reader := &closingStringReader{
Reader: strings.NewReader("hello"),
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("yes"),
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Size: 20,
MetaData: map[string]string{
"filename": "file.jpg\"evil",
},
}, nil),
store.EXPECT().GetReader("yes").Return(reader, nil),
locker.EXPECT().UnlockUpload("yes"),
)
composer := NewStoreComposer()
composer.UseCore(store)
composer.UseGetReader(store)
composer.UseLocker(locker)
handler, _ := NewHandler(Config{
StoreComposer: composer,
})
(&httpTest{
Method: "GET",
URL: "yes",
ResHeader: map[string]string{
"Content-Length": "5",
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
},
Code: http.StatusOK,
ResBody: "hello",
}).Run(handler, t)
if !reader.closed {
t.Error("expected reader to be closed")
}
})
SubTest(t, "EmptyDownload", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 0,
MetaData: map[string]string{
"filename": "file.jpg\"evil",
},
}, nil)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "GET",
URL: "yes",
ResHeader: map[string]string{
"Content-Length": "0",
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
},
Code: http.StatusNoContent,
ResBody: "",
}).Run(handler, t)
})
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore) {
composer := NewStoreComposer()
composer.UseCore(store)
handler, _ := NewUnroutedHandler(Config{
StoreComposer: composer,
})
(&httpTest{
Method: "GET",
URL: "foo",
Code: http.StatusNotImplemented,
}).Run(http.HandlerFunc(handler.GetFile), t)
})
}