2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-26 08:08:37 +00:00
|
|
|
"errors"
|
2017-02-21 22:00:51 +00:00
|
|
|
"io"
|
2015-02-09 18:37:06 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-02-21 22:00:51 +00:00
|
|
|
"time"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 16:08:34 +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-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPatch(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadChunk", 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 16:08:34 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2016-10-13 16:08:34 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
|
|
|
Size: 10,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
|
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()),
|
2016-10-13 16:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
NotifyCompleteUploads: true,
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent, 1)
|
2016-10-13 16:08:34 +00:00
|
|
|
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)
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
|
|
|
info := event.Upload
|
2016-10-13 16:08:34 +00:00
|
|
|
a.Equal("yes", info.ID)
|
|
|
|
a.EqualValues(int64(10), info.Size)
|
|
|
|
a.Equal(int64(10), info.Offset)
|
2019-09-19 09:15:48 +00:00
|
|
|
|
|
|
|
req := event.HTTPRequest
|
|
|
|
a.Equal("PATCH", req.Method)
|
|
|
|
a.Equal("yes", req.URI)
|
|
|
|
a.Equal("5", req.Header.Get("Upload-Offset"))
|
2015-02-09 18:37:06 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "MethodOverriding", 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 16:08:34 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2016-10-13 16:08:34 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
2019-08-25 20:10:55 +00:00
|
|
|
Size: 10,
|
2016-10-13 16:08:34 +00:00
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
|
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()),
|
2016-10-13 16:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&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)
|
2015-02-09 18:37:06 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadChunkToFinished", 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(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 20,
|
|
|
|
Size: 20,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 16:08:34 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&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)
|
2015-12-26 20:23:09 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "no").Return(nil, ErrNotFound)
|
2016-10-13 16:08:34 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&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)
|
|
|
|
})
|
2016-04-09 20:09:22 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "MissmatchingOffsetFail", 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(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 16:08:34 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&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)
|
|
|
|
})
|
2016-04-09 20:09:22 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "ExceedingMaxSizeFail", 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(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
|
|
|
Size: 10,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 16:08:34 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&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)
|
|
|
|
})
|
2016-04-09 20:09:22 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidContentTypeFail", 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: "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)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidOffsetFail", 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: "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)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "OverflowWithoutLength", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:08:34 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
2016-10-13 16:08:34 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2018-03-29 12:40:43 +00:00
|
|
|
ID: "yes",
|
2016-10-13 16:08:34 +00:00
|
|
|
Offset: 5,
|
|
|
|
Size: 20,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
|
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()),
|
2016-10-13 16:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
2016-04-09 20:09:22 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "DeclareLengthOnFinalChunk", 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-05-13 13:53:35 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2018-05-13 13:53:35 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
|
|
|
Size: 0,
|
|
|
|
SizeIsDeferred: true,
|
|
|
|
}, nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().DeclareLength(gomock.Any(), int64(20)),
|
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
|
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()),
|
2018-05-13 13:53:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
MaxSize: 20,
|
2018-05-13 13:53:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
body := strings.NewReader("hellothisismore")
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
"Upload-Length": "20",
|
|
|
|
},
|
|
|
|
ReqBody: body,
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "20",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "DeclareLengthAfterFinalChunk", 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-05-13 13:53:35 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2018-05-13 13:53:35 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 20,
|
|
|
|
Size: 0,
|
|
|
|
SizeIsDeferred: true,
|
|
|
|
}, nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().DeclareLength(gomock.Any(), int64(20)),
|
|
|
|
upload.EXPECT().FinishUpload(gomock.Any()),
|
2018-05-13 13:53:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
MaxSize: 20,
|
2018-05-13 13:53:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "20",
|
|
|
|
"Upload-Length": "20",
|
|
|
|
},
|
|
|
|
ReqBody: nil,
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "DeclareLengthOnNonFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload1 := NewMockFullUpload(ctrl)
|
|
|
|
upload2 := NewMockFullUpload(ctrl)
|
|
|
|
|
2018-05-13 13:53:35 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload1, nil),
|
|
|
|
upload1.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2018-05-13 13:53:35 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 5,
|
|
|
|
Size: 0,
|
|
|
|
SizeIsDeferred: true,
|
|
|
|
}, nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
store.EXPECT().AsLengthDeclarableUpload(upload1).Return(upload1),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload1.EXPECT().DeclareLength(gomock.Any(), int64(20)),
|
|
|
|
upload1.EXPECT().WriteChunk(gomock.Any(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload2, nil),
|
|
|
|
upload2.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2018-05-13 13:53:35 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 10,
|
|
|
|
Size: 20,
|
|
|
|
SizeIsDeferred: false,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload2.EXPECT().WriteChunk(gomock.Any(), int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil),
|
|
|
|
upload2.EXPECT().FinishUpload(gomock.Any()),
|
2018-05-13 13:53:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
MaxSize: 20,
|
2018-05-13 13:53:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
"Upload-Length": "20",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "10",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "10",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("thisismore"),
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "20",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Locker", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:08:34 +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:08:34 +00:00
|
|
|
|
|
|
|
gomock.InOrder(
|
2019-09-12 10:37:43 +00:00
|
|
|
locker.EXPECT().NewLock("yes").Return(lock, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
lock.EXPECT().Lock(gomock.Any(), gomock.Any()).Return(nil),
|
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
ID: "yes",
|
2016-10-13 16:08:34 +00:00
|
|
|
Offset: 0,
|
|
|
|
Size: 20,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
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 16:08:34 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
composer = NewStoreComposer()
|
2016-10-13 16:08:34 +00:00
|
|
|
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)
|
|
|
|
})
|
2017-02-21 22:00:51 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "NotifyUploadProgress", 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)
|
|
|
|
|
2021-10-13 19:08:09 +00:00
|
|
|
// We simulate that the upload has already an offset of 10 bytes. Therefore, the progress notifications
|
|
|
|
// must be the sum of the exisiting offset and the newly read bytes.
|
2017-02-21 22:00:51 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2017-02-21 22:00:51 +00:00
|
|
|
ID: "yes",
|
2021-10-13 19:08:09 +00:00
|
|
|
Offset: 10,
|
2017-02-21 22:00:51 +00:00
|
|
|
Size: 100,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(10), NewReaderMatcher("first second third")).Return(int64(18), nil),
|
2017-02-21 22:00:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2017-02-21 22:00:51 +00:00
|
|
|
NotifyUploadProgress: true,
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent)
|
2017-02-21 22:00:51 +00:00
|
|
|
handler.UploadProgress = c
|
|
|
|
|
|
|
|
reader, writer := io.Pipe()
|
2017-02-21 22:33:59 +00:00
|
|
|
a := assert.New(t)
|
2017-02-21 22:00:51 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
writer.Write([]byte("first "))
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
2017-02-21 22:33:59 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
info := event.Upload
|
2017-02-21 22:33:59 +00:00
|
|
|
a.Equal("yes", info.ID)
|
|
|
|
a.Equal(int64(100), info.Size)
|
2021-10-13 19:08:09 +00:00
|
|
|
a.Equal(int64(16), info.Offset)
|
2017-02-21 22:33:59 +00:00
|
|
|
|
2017-02-21 22:00:51 +00:00
|
|
|
writer.Write([]byte("second "))
|
|
|
|
writer.Write([]byte("third"))
|
2017-02-21 22:33:59 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event = <-c
|
|
|
|
info = event.Upload
|
2017-02-21 22:33:59 +00:00
|
|
|
a.Equal("yes", info.ID)
|
|
|
|
a.Equal(int64(100), info.Size)
|
2021-10-13 19:08:09 +00:00
|
|
|
a.Equal(int64(28), info.Offset)
|
2017-02-21 22:33:59 +00:00
|
|
|
|
2017-02-21 22:00:51 +00:00
|
|
|
writer.Close()
|
2017-02-21 22:33:59 +00:00
|
|
|
|
2019-05-15 21:57:20 +00:00
|
|
|
// No progress event is sent after the writer is closed
|
|
|
|
// because an event for 18 bytes was already emitted.
|
2017-02-21 22:00:51 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
2021-10-13 19:08:09 +00:00
|
|
|
"Upload-Offset": "10",
|
2017-02-21 22:00:51 +00:00
|
|
|
},
|
|
|
|
ReqBody: reader,
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
2021-10-13 19:08:09 +00:00
|
|
|
"Upload-Offset": "28",
|
2017-02-21 22:00:51 +00:00
|
|
|
},
|
|
|
|
}).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)
|
|
|
|
})
|
2019-05-26 19:56:51 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "StopUpload", 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)
|
|
|
|
|
2019-05-26 19:56:51 +00:00
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-05-26 19:56:51 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 0,
|
|
|
|
Size: 100,
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(0), NewReaderMatcher("first ")).Return(int64(6), nil),
|
2019-08-25 20:10:55 +00:00
|
|
|
store.EXPECT().AsTerminatableUpload(upload).Return(upload),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().Terminate(gomock.Any()),
|
2019-05-26 19:56:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2019-05-26 19:56:51 +00:00
|
|
|
NotifyUploadProgress: true,
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent)
|
2019-05-26 19:56:51 +00:00
|
|
|
handler.UploadProgress = c
|
|
|
|
|
|
|
|
reader, writer := io.Pipe()
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
writer.Write([]byte("first "))
|
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
|
|
|
info := event.Upload
|
2019-05-26 19:56:51 +00:00
|
|
|
info.StopUpload()
|
|
|
|
|
|
|
|
// Wait a short time to ensure that the goroutine in the PATCH
|
|
|
|
// handler has received and processed the stop event.
|
|
|
|
<-time.After(10 * time.Millisecond)
|
|
|
|
|
|
|
|
// Assert that the "request body" has been closed.
|
|
|
|
_, err := writer.Write([]byte("second "))
|
|
|
|
a.Equal(err, io.ErrClosedPipe)
|
|
|
|
|
|
|
|
// Close the upload progress handler so that the main goroutine
|
|
|
|
// can exit properly after waiting for this goroutine to finish.
|
|
|
|
close(handler.UploadProgress)
|
|
|
|
}()
|
|
|
|
|
|
|
|
(&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.StatusBadRequest,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "",
|
|
|
|
},
|
2021-10-25 09:54:02 +00:00
|
|
|
ResBody: "ERR_UPLOAD_STOPPED: upload has been stopped by server\n",
|
2019-05-26 19:56:51 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
_, more := <-c
|
|
|
|
a.False(more)
|
|
|
|
})
|
2021-04-26 08:08:37 +00:00
|
|
|
|
|
|
|
SubTest(t, "BodyReadError", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
// This test ensure that error that occurr from reading the request body are not forwarded to the
|
|
|
|
// storage backend but are still causing an
|
|
|
|
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(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2021-04-26 08:08:37 +00:00
|
|
|
ID: "yes",
|
|
|
|
Offset: 0,
|
|
|
|
Size: 100,
|
|
|
|
}, nil),
|
|
|
|
// The reader for WriteChunk must not return an error.
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().WriteChunk(gomock.Any(), int64(0), NewReaderMatcher("first ")).Return(int64(6), nil),
|
2021-04-26 08:08:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
|
|
|
|
|
|
|
reader, writer := io.Pipe()
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
writer.Write([]byte("first "))
|
|
|
|
err := writer.CloseWithError(errors.New("an error while reading the body"))
|
|
|
|
a.NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
(&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.StatusInternalServerError,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "",
|
|
|
|
},
|
2021-10-25 09:54:02 +00:00
|
|
|
ResBody: "ERR_INTERNAL_SERVER_ERROR: an error while reading the body\n",
|
2021-04-26 08:08:37 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-04-09 20:09:22 +00:00
|
|
|
}
|