2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-13 16:42:40 +00:00
|
|
|
"strings"
|
2015-02-17 13:19:56 +00:00
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2016-01-20 15:40:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-03-08 12:03:18 +00:00
|
|
|
. "github.com/tus/tusd/v2/pkg/handler"
|
2015-02-17 13:19:56 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func TestConcat(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
composer = NewStoreComposer()
|
2016-10-13 09:33:20 +00:00
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseConcater(store)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Tus-Extension": "creation,creation-with-upload,concatenation",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
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,
|
|
|
|
IsPartial: true,
|
|
|
|
IsFinal: false,
|
|
|
|
PartialUploads: nil,
|
|
|
|
MetaData: make(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,
|
|
|
|
IsPartial: true,
|
|
|
|
IsFinal: false,
|
|
|
|
PartialUploads: nil,
|
|
|
|
MetaData: make(map[string]string),
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
BasePath: "files",
|
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Concat": "partial",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Status", 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().GetUpload(gomock.Any(), "foo").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
IsPartial: true,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
BasePath: "files",
|
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Concat": "partial",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Final", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 09:33:20 +00:00
|
|
|
a := assert.New(t)
|
|
|
|
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
uploadA := NewMockFullUpload(ctrl)
|
|
|
|
uploadB := NewMockFullUpload(ctrl)
|
|
|
|
uploadC := NewMockFullUpload(ctrl)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "a").Return(uploadA, nil),
|
|
|
|
uploadA.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2016-10-13 09:33:20 +00:00
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 5,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "b").Return(uploadB, nil),
|
|
|
|
uploadB.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2016-10-13 09:33:20 +00:00
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 5,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
|
2016-10-13 09:33:20 +00:00
|
|
|
Size: 10,
|
|
|
|
IsPartial: false,
|
|
|
|
IsFinal: true,
|
|
|
|
PartialUploads: []string{"a", "b"},
|
|
|
|
MetaData: make(map[string]string),
|
2019-08-25 20:10:55 +00:00
|
|
|
}).Return(uploadC, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
uploadC.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 10,
|
|
|
|
IsPartial: false,
|
|
|
|
IsFinal: true,
|
|
|
|
PartialUploads: []string{"a", "b"},
|
|
|
|
MetaData: make(map[string]string),
|
|
|
|
}, nil),
|
2019-09-19 10:14:25 +00:00
|
|
|
store.EXPECT().AsConcatableUpload(uploadC).Return(uploadC),
|
2022-03-19 22:21:17 +00:00
|
|
|
uploadC.EXPECT().ConcatUploads(gomock.Any(), []Upload{uploadA, uploadB}).Return(nil),
|
2016-10-13 09:33:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
BasePath: "files",
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
NotifyCompleteUploads: true,
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent, 1)
|
2016-10-13 09:33:20 +00:00
|
|
|
handler.CompleteUploads = c
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
2017-01-31 15:58:31 +00:00
|
|
|
// A space between `final;` and the first URL should be allowed due to
|
|
|
|
// compatibility reasons, even if the specification does not define
|
|
|
|
// it. Therefore this character is included in this test case.
|
2019-09-19 09:15:48 +00:00
|
|
|
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
|
|
|
"X-Custom-Header": "tada",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
|
|
|
info := event.Upload
|
2016-10-13 09:33:20 +00:00
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.EqualValues(10, info.Size)
|
|
|
|
a.EqualValues(10, info.Offset)
|
|
|
|
a.False(info.IsPartial)
|
|
|
|
a.True(info.IsFinal)
|
|
|
|
a.Equal([]string{"a", "b"}, info.PartialUploads)
|
2019-09-19 09:15:48 +00:00
|
|
|
|
|
|
|
req := event.HTTPRequest
|
|
|
|
a.Equal("POST", req.Method)
|
|
|
|
a.Equal("", req.URI)
|
|
|
|
a.Equal("tada", req.Header.Get("X-Custom-Header"))
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Status", 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().GetUpload(gomock.Any(), "foo").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
IsFinal: true,
|
|
|
|
PartialUploads: []string{"a", "b"},
|
|
|
|
Size: 10,
|
|
|
|
Offset: 10,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
BasePath: "files",
|
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
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
|
|
|
"Upload-Length": "10",
|
|
|
|
"Upload-Offset": "10",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "CreateWithUnfinishedFail", 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)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
// This upload is still unfinished (mismatching offset and size) and
|
|
|
|
// will therefore cause the POST request to fail.
|
2019-08-25 20:10:55 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "c").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "c",
|
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 3,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
BasePath: "files",
|
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
2017-01-31 15:58:31 +00:00
|
|
|
"Upload-Concat": "final;http://tus.io/files/c",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "CreateExceedingMaxSizeFail", 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().GetUpload(gomock.Any(), "huge").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "huge",
|
|
|
|
Size: 1000,
|
|
|
|
Offset: 1000,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 09:33:20 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
MaxSize: 100,
|
|
|
|
BasePath: "files",
|
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
2017-01-31 15:58:31 +00:00
|
|
|
"Upload-Concat": "final;/files/huge",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-10-13 16:42:40 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadToFinalFail", 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().GetUpload(gomock.Any(), "foo").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 10,
|
|
|
|
Offset: 0,
|
|
|
|
IsFinal: true,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 16:42:40 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:42:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-10-13 17:03:18 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidConcatHeaderFail", 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",
|
2017-01-31 15:58:31 +00:00
|
|
|
"Upload-Concat": "final;",
|
2016-10-13 17:03:18 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
}
|