2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-28 20:06:37 +00:00
|
|
|
"bytes"
|
2015-02-09 18:37:06 +00:00
|
|
|
"net/http"
|
2016-08-28 20:06:37 +00:00
|
|
|
"strings"
|
2015-02-09 18:37:06 +00:00
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2017-07-19 15:45:16 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-08-28 20:06:37 +00:00
|
|
|
|
2019-06-11 16:23:20 +00:00
|
|
|
. "github.com/tus/tusd/pkg/handler"
|
2015-02-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func TestPost(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{
|
2020-07-15 15:30:17 +00:00
|
|
|
"foo": "hello",
|
|
|
|
"bar": "world",
|
|
|
|
"empty": "",
|
2019-08-25 20:10:55 +00:00
|
|
|
},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{
|
2020-07-15 15:30:17 +00:00
|
|
|
"foo": "hello",
|
|
|
|
"bar": "world",
|
|
|
|
"empty": "",
|
2019-08-25 20:10:55 +00:00
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
)
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2017-07-19 15:45:16 +00:00
|
|
|
BasePath: "https://buy.art/files/",
|
|
|
|
NotifyCreatedUploads: true,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent, 1)
|
2017-07-19 15:45:16 +00:00
|
|
|
handler.CreatedUploads = c
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2016-10-13 16:50:49 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
// Invalid Base64-encoded values should be ignored
|
2020-07-15 15:30:17 +00:00
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=, hah INVALID, empty",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
2016-10-13 16:44:44 +00:00
|
|
|
"Location": "https://buy.art/files/foo",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2017-07-19 15:45:16 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
|
|
|
info := event.Upload
|
2017-07-19 15:45:16 +00:00
|
|
|
|
|
|
|
a := assert.New(t)
|
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.Equal(int64(300), info.Size)
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "CreateEmptyUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
2018-03-29 12:40:43 +00:00
|
|
|
|
2019-08-25 20:10:55 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 0,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 0,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()).Return(nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
)
|
2018-03-29 12:40:43 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-03-29 12:40:43 +00:00
|
|
|
BasePath: "https://buy.art/files/",
|
|
|
|
NotifyCompleteUploads: true,
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
handler.CompleteUploads = make(chan HookEvent, 1)
|
2018-03-29 12:40:43 +00:00
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "0",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "https://buy.art/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-handler.CompleteUploads
|
|
|
|
info := event.Upload
|
2018-03-29 12:40:43 +00:00
|
|
|
|
|
|
|
a := assert.New(t)
|
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.Equal(int64(0), info.Size)
|
|
|
|
a.Equal(int64(0), info.Offset)
|
2019-09-19 09:15:48 +00:00
|
|
|
|
|
|
|
req := event.HTTPRequest
|
|
|
|
a.Equal("POST", req.Method)
|
|
|
|
a.Equal("", req.URI)
|
2018-03-29 12:40:43 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
MaxSize: 400,
|
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Exceeding MaxSize",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "500",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
},
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidUploadLengthFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 17:03:18 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 17:03:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
URL: "",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "-5",
|
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
2018-04-28 19:39:40 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadLengthAndUploadDeferLengthFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2018-04-28 19:39:40 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-04-28 19:39:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
URL: "",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "10",
|
|
|
|
"Upload-Defer-Length": "1",
|
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "NeitherUploadLengthNorUploadDeferLengthFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2018-04-28 19:39:40 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-04-28 19:39:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
URL: "",
|
|
|
|
ReqHeader: map[string]string{
|
2019-05-15 21:57:20 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
2018-04-28 19:39:40 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidUploadDeferLengthFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2018-04-28 19:39:40 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-04-28 19:39:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
URL: "",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Defer-Length": "bad",
|
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
2016-10-13 17:03:18 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-08-28 20:06:37 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2016-08-28 20:06:37 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"X-Forwarded-Host": "foo.com",
|
|
|
|
"X-Forwarded-Proto": "https",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
BasePath: "/files/",
|
|
|
|
RespectForwardedHeaders: true,
|
|
|
|
})
|
2016-01-16 14:27:35 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"X-Forwarded-Host": "foo.com",
|
|
|
|
"X-Forwarded-Proto": "https",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "https://foo.com/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
BasePath: "/files/",
|
|
|
|
RespectForwardedHeaders: true,
|
|
|
|
})
|
2016-08-28 20:06:37 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"X-Forwarded-Host": "bar.com",
|
|
|
|
"X-Forwarded-Proto": "http",
|
2020-10-12 10:36:55 +00:00
|
|
|
"Forwarded": "for=192.168.10.112;host=upload.example.tld;proto=https;proto-version=",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
2020-10-12 10:36:55 +00:00
|
|
|
"Location": "https://upload.example.tld/files/foo",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-08-28 20:06:37 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
BasePath: "/files/",
|
|
|
|
RespectForwardedHeaders: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"X-Forwarded-Proto": "aaa",
|
|
|
|
"Forwarded": "proto=bbb",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-08-28 20:06:37 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:33:33 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2019-09-12 10:37:43 +00:00
|
|
|
locker := NewMockFullLocker(ctrl)
|
|
|
|
lock := NewMockFullLock(ctrl)
|
2019-08-25 20:10:55 +00:00
|
|
|
upload := NewMockFullUpload(ctrl)
|
2016-10-13 16:33:33 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2016-10-13 09:33:20 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{
|
|
|
|
"foo": "hello",
|
|
|
|
"bar": "world",
|
|
|
|
},
|
2019-08-25 20:10:55 +00:00
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{
|
|
|
|
"foo": "hello",
|
|
|
|
"bar": "world",
|
|
|
|
},
|
|
|
|
}, nil),
|
2019-09-12 10:37:43 +00:00
|
|
|
locker.EXPECT().NewLock("foo").Return(lock, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
lock.EXPECT().Lock(gomock.Any(), gomock.Any()).Return(nil),
|
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
|
2019-09-12 10:37:43 +00:00
|
|
|
lock.EXPECT().Unlock().Return(nil),
|
2016-10-13 09:33:20 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
composer = NewStoreComposer()
|
2016-10-13 16:33:33 +00:00
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseLocker(locker)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2016-10-13 16:33:33 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "CreateExceedingUploadSize", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
},
|
|
|
|
ReqBody: bytes.NewReader(make([]byte, 400)),
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "IncorrectContentType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}).Return(upload, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 300,
|
|
|
|
MetaData: map[string]string{},
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Incorrect content type",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/false",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
"Upload-Offset": "",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadToFinalUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
BasePath: "/files/",
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
2017-01-31 15:58:31 +00:00
|
|
|
"Upload-Concat": "final;http://tus.io/files/a http://tus.io/files/b",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
})
|
2016-08-28 20:06:37 +00:00
|
|
|
}
|