core: Refactor DataStore

This commit is contained in:
Marius 2019-08-25 22:10:55 +02:00
parent 5acc586800
commit 5004d3ca4d
8 changed files with 377 additions and 212 deletions

View File

@ -32,13 +32,27 @@ func TestConcat(t *testing.T) {
SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
IsPartial: true, IsPartial: true,
IsFinal: false, IsFinal: false,
PartialUploads: nil, PartialUploads: nil,
MetaData: make(map[string]string), MetaData: make(map[string]string),
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
IsPartial: true,
IsFinal: false,
PartialUploads: nil,
MetaData: make(map[string]string),
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
BasePath: "files", BasePath: "files",
@ -57,9 +71,17 @@ func TestConcat(t *testing.T) {
}) })
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("foo").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("foo").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
IsPartial: true, IsPartial: true,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
BasePath: "files", BasePath: "files",
@ -84,13 +106,21 @@ func TestConcat(t *testing.T) {
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
a := assert.New(t) a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
uploadA := NewMockFullUpload(ctrl)
uploadB := NewMockFullUpload(ctrl)
uploadC := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("a").Return(FileInfo{ store.EXPECT().GetUpload("a").Return(uploadA, nil),
uploadA.EXPECT().GetInfo().Return(FileInfo{
IsPartial: true, IsPartial: true,
Size: 5, Size: 5,
Offset: 5, Offset: 5,
}, nil), }, nil),
store.EXPECT().GetInfo("b").Return(FileInfo{ store.EXPECT().GetUpload("b").Return(uploadB, nil),
uploadB.EXPECT().GetInfo().Return(FileInfo{
IsPartial: true, IsPartial: true,
Size: 5, Size: 5,
Offset: 5, Offset: 5,
@ -101,7 +131,15 @@ func TestConcat(t *testing.T) {
IsFinal: true, IsFinal: true,
PartialUploads: []string{"a", "b"}, PartialUploads: []string{"a", "b"},
MetaData: make(map[string]string), MetaData: make(map[string]string),
}).Return("foo", nil), }).Return(uploadC, nil),
uploadC.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 10,
IsPartial: false,
IsFinal: true,
PartialUploads: []string{"a", "b"},
MetaData: make(map[string]string),
}, nil),
store.EXPECT().ConcatUploads("foo", []string{"a", "b"}).Return(nil), store.EXPECT().ConcatUploads("foo", []string{"a", "b"}).Return(nil),
) )
@ -136,12 +174,20 @@ func TestConcat(t *testing.T) {
}) })
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("foo").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("foo").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
IsFinal: true, IsFinal: true,
PartialUploads: []string{"a", "b"}, PartialUploads: []string{"a", "b"},
Size: 10, Size: 10,
Offset: 10, Offset: 10,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
BasePath: "files", BasePath: "files",
@ -164,13 +210,21 @@ func TestConcat(t *testing.T) {
}) })
SubTest(t, "CreateWithUnfinishedFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "CreateWithUnfinishedFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
// This upload is still unfinished (mismatching offset and size) and // This upload is still unfinished (mismatching offset and size) and
// will therefore cause the POST request to fail. // will therefore cause the POST request to fail.
store.EXPECT().GetInfo("c").Return(FileInfo{ gomock.InOrder(
store.EXPECT().GetUpload("c").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "c",
IsPartial: true, IsPartial: true,
Size: 5, Size: 5,
Offset: 3, Offset: 3,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
BasePath: "files", BasePath: "files",
@ -188,10 +242,18 @@ func TestConcat(t *testing.T) {
}) })
SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("huge").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("huge").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "huge",
Size: 1000, Size: 1000,
Offset: 1000, Offset: 1000,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
MaxSize: 100, MaxSize: 100,
@ -210,11 +272,19 @@ func TestConcat(t *testing.T) {
}) })
SubTest(t, "UploadToFinalFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadToFinalFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("foo").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("foo").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 10, Size: 10,
Offset: 0, Offset: 0,
IsFinal: true, IsFinal: true,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,

View File

@ -1,93 +0,0 @@
package handler_test
import (
"net/http"
"testing"
"github.com/golang/mock/gomock"
. "github.com/tus/tusd/pkg/handler"
"github.com/stretchr/testify/assert"
)
func TestTerminate(t *testing.T) {
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
composer = NewStoreComposer()
composer.UseCore(store)
composer.UseTerminater(store)
handler, _ := NewHandler(Config{
StoreComposer: composer,
})
(&httpTest{
Method: "OPTIONS",
Code: http.StatusOK,
ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload,termination",
},
}).Run(handler, t)
})
SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("foo"),
store.EXPECT().GetInfo("foo").Return(FileInfo{
ID: "foo",
Size: 10,
}, nil),
store.EXPECT().Terminate("foo").Return(nil),
locker.EXPECT().UnlockUpload("foo"),
)
composer = NewStoreComposer()
composer.UseCore(store)
composer.UseTerminater(store)
composer.UseLocker(locker)
handler, _ := NewHandler(Config{
StoreComposer: composer,
NotifyTerminatedUploads: true,
})
c := make(chan FileInfo, 1)
handler.TerminatedUploads = c
(&httpTest{
Method: "DELETE",
URL: "foo",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusNoContent,
}).Run(handler, t)
info := <-c
a := assert.New(t)
a.Equal("foo", info.ID)
a.Equal(int64(10), info.Size)
})
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
composer = NewStoreComposer()
composer.UseCore(store)
handler, _ := NewUnroutedHandler(Config{
StoreComposer: composer,
})
(&httpTest{
Method: "DELETE",
URL: "foo",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusNotImplemented,
}).Run(http.HandlerFunc(handler.DelFile), t)
})
}

View File

@ -14,10 +14,12 @@ func TestHead(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
locker := NewMockLocker(ctrl) locker := NewMockLocker(ctrl)
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
locker.EXPECT().LockUpload("yes"), locker.EXPECT().LockUpload("yes"),
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
Offset: 11, Offset: 11,
Size: 44, Size: 44,
MetaData: map[string]string{ MetaData: map[string]string{
@ -59,7 +61,7 @@ func TestHead(t *testing.T) {
}) })
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist) store.EXPECT().GetUpload("no").Return(nil, os.ErrNotExist)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -83,10 +85,17 @@ func TestHead(t *testing.T) {
}) })
SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("yes").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
SizeIsDeferred: true, SizeIsDeferred: true,
Size: 0, Size: 0,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -106,8 +115,13 @@ func TestHead(t *testing.T) {
}) })
SubTest(t, "NoDeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "NoDeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
SizeIsDeferred: false, SizeIsDeferred: false,
Size: 10, Size: 10,
}, nil), }, nil),

View File

@ -17,14 +17,19 @@ import (
func TestPatch(t *testing.T) { func TestPatch(t *testing.T) {
SubTest(t, "UploadChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 5, Offset: 5,
Size: 10, Size: 10,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
store.EXPECT().FinishUpload("yes"), upload.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -58,13 +63,19 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "MethodOverriding", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "MethodOverriding", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 5, Offset: 5,
Size: 20, Size: 10,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -89,10 +100,18 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "UploadChunkToFinished", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadChunkToFinished", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("yes").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes",
Offset: 20, Offset: 20,
Size: 20, Size: 20,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -115,7 +134,7 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist) store.EXPECT().GetUpload("no").Return(nil, os.ErrNotExist)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -134,9 +153,17 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "MissmatchingOffsetFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "MissmatchingOffsetFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("yes").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes",
Offset: 5, Offset: 5,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -155,10 +182,18 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "ExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "ExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("yes").Return(FileInfo{ ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes",
Offset: 5, Offset: 5,
Size: 10, Size: 10,
}, nil) }, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -221,14 +256,19 @@ func TestPatch(t *testing.T) {
// is that even if the uploader supplies more than 15 bytes, we only // is that even if the uploader supplies more than 15 bytes, we only
// pass 15 bytes to the data store and ignore the rest. // pass 15 bytes to the data store and ignore the rest.
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 5, Offset: 5,
Size: 20, Size: 20,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
store.EXPECT().FinishUpload("yes"), upload.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -258,16 +298,22 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "DeclareLengthOnFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "DeclareLengthOnFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 5, Offset: 5,
Size: 0, Size: 0,
SizeIsDeferred: true, SizeIsDeferred: true,
}, nil), }, nil),
store.EXPECT().DeclareLength("yes", int64(20)), store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), upload.EXPECT().DeclareLength(int64(20)),
store.EXPECT().FinishUpload("yes"), upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
upload.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -295,15 +341,21 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "DeclareLengthAfterFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "DeclareLengthAfterFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 20, Offset: 20,
Size: 0, Size: 0,
SizeIsDeferred: true, SizeIsDeferred: true,
}, nil), }, nil),
store.EXPECT().DeclareLength("yes", int64(20)), store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
store.EXPECT().FinishUpload("yes"), upload.EXPECT().DeclareLength(int64(20)),
upload.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -327,23 +379,32 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "DeclareLengthOnNonFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "DeclareLengthOnNonFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload1 := NewMockFullUpload(ctrl)
upload2 := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload1, nil),
upload1.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 5, Offset: 5,
Size: 0, Size: 0,
SizeIsDeferred: true, SizeIsDeferred: true,
}, nil), }, nil),
store.EXPECT().DeclareLength("yes", int64(20)), store.EXPECT().AsLengthDeclarableUpload(upload1).Return(upload1),
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), upload1.EXPECT().DeclareLength(int64(20)),
store.EXPECT().GetInfo("yes").Return(FileInfo{ upload1.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
store.EXPECT().GetUpload("yes").Return(upload2, nil),
upload2.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 10, Offset: 10,
Size: 20, Size: 20,
SizeIsDeferred: false, SizeIsDeferred: false,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil), upload2.EXPECT().WriteChunk(int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil),
store.EXPECT().FinishUpload("yes"), upload2.EXPECT().FinishUpload(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -387,14 +448,17 @@ func TestPatch(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
locker := NewMockLocker(ctrl) locker := NewMockLocker(ctrl)
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
locker.EXPECT().LockUpload("yes").Return(nil), locker.EXPECT().LockUpload("yes").Return(nil),
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes",
Offset: 0, Offset: 0,
Size: 20, Size: 20,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil), upload.EXPECT().WriteChunk(int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
locker.EXPECT().UnlockUpload("yes").Return(nil), locker.EXPECT().UnlockUpload("yes").Return(nil),
) )
@ -420,13 +484,18 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "NotifyUploadProgress", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "NotifyUploadProgress", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 0, Offset: 0,
Size: 100, Size: 100,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil), upload.EXPECT().WriteChunk(int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
@ -487,14 +556,20 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "StopUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "StopUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetUpload("yes").Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "yes", ID: "yes",
Offset: 0, Offset: 0,
Size: 100, Size: 100,
}, nil), }, nil),
store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose), upload.EXPECT().WriteChunk(int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose),
store.EXPECT().Terminate("yes").Return(nil), store.EXPECT().AsTerminatableUpload(upload).Return(upload),
upload.EXPECT().Terminate(),
) )
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{

View File

@ -14,13 +14,27 @@ import (
func TestPost(t *testing.T) { func TestPost(t *testing.T) {
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{ MetaData: map[string]string{
"foo": "hello", "foo": "hello",
"bar": "world", "bar": "world",
}, },
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -53,12 +67,22 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "CreateEmptyUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "CreateEmptyUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 0, Size: 0,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
store.EXPECT().FinishUpload("foo").Return(nil) ID: "foo",
Size: 0,
MetaData: map[string]string{},
}, nil),
upload.EXPECT().FinishUpload().Return(nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -173,10 +197,21 @@ func TestPost(t *testing.T) {
SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -199,10 +234,21 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -226,10 +272,21 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -254,10 +311,21 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -286,6 +354,7 @@ func TestPost(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
locker := NewMockLocker(ctrl) locker := NewMockLocker(ctrl)
upload := NewMockFullUpload(ctrl)
gomock.InOrder( gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
@ -294,9 +363,17 @@ func TestPost(t *testing.T) {
"foo": "hello", "foo": "hello",
"bar": "world", "bar": "world",
}, },
}).Return("foo", nil), }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
},
}, nil),
locker.EXPECT().LockUpload("foo"), locker.EXPECT().LockUpload("foo"),
store.EXPECT().WriteChunk("foo", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil), upload.EXPECT().WriteChunk(int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
locker.EXPECT().UnlockUpload("foo"), locker.EXPECT().UnlockUpload("foo"),
) )
@ -327,10 +404,21 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "CreateExceedingUploadSize", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "CreateExceedingUploadSize", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,
@ -350,10 +438,21 @@ func TestPost(t *testing.T) {
}) })
SubTest(t, "IncorrectContentType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "IncorrectContentType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
MetaData: map[string]string{}, MetaData: map[string]string{},
}).Return("foo", nil) }).Return(upload, nil),
upload.EXPECT().GetInfo().Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,