From 5004d3ca4dc1c3dc0686cb9d1f19d887d92a0d84 Mon Sep 17 00:00:00 2001 From: Marius Date: Sun, 25 Aug 2019 22:10:55 +0200 Subject: [PATCH] core: Refactor DataStore --- pkg/handler/{f => }/concat_test.go | 136 +++++++++++++++------ pkg/handler/{f => }/config_test.go | 0 pkg/handler/{f => }/cors_test.go | 0 pkg/handler/f/terminate_test.go | 93 --------------- pkg/handler/{f => }/head_test.go | 28 +++-- pkg/handler/{f => }/options_test.go | 0 pkg/handler/{f => }/patch_test.go | 157 ++++++++++++++++++------- pkg/handler/{f => }/post_test.go | 175 ++++++++++++++++++++++------ 8 files changed, 377 insertions(+), 212 deletions(-) rename pkg/handler/{f => }/concat_test.go (66%) rename pkg/handler/{f => }/config_test.go (100%) rename pkg/handler/{f => }/cors_test.go (100%) delete mode 100644 pkg/handler/f/terminate_test.go rename pkg/handler/{f => }/head_test.go (80%) rename pkg/handler/{f => }/options_test.go (100%) rename pkg/handler/{f => }/patch_test.go (74%) rename pkg/handler/{f => }/post_test.go (74%) diff --git a/pkg/handler/f/concat_test.go b/pkg/handler/concat_test.go similarity index 66% rename from pkg/handler/f/concat_test.go rename to pkg/handler/concat_test.go index 799f7d2..1f0ce89 100644 --- a/pkg/handler/f/concat_test.go +++ b/pkg/handler/concat_test.go @@ -32,13 +32,27 @@ func TestConcat(t *testing.T) { SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - IsPartial: true, - IsFinal: false, - PartialUploads: nil, - MetaData: make(map[string]string), - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + IsPartial: true, + IsFinal: false, + PartialUploads: nil, + MetaData: make(map[string]string), + }).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{ BasePath: "files", @@ -57,9 +71,17 @@ func TestConcat(t *testing.T) { }) SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("foo").Return(FileInfo{ - IsPartial: true, - }, nil) + 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, + }, nil), + ) handler, _ := NewHandler(Config{ BasePath: "files", @@ -84,13 +106,21 @@ func TestConcat(t *testing.T) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { a := assert.New(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + uploadA := NewMockFullUpload(ctrl) + uploadB := NewMockFullUpload(ctrl) + uploadC := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("a").Return(FileInfo{ + store.EXPECT().GetUpload("a").Return(uploadA, nil), + uploadA.EXPECT().GetInfo().Return(FileInfo{ IsPartial: true, Size: 5, Offset: 5, }, nil), - store.EXPECT().GetInfo("b").Return(FileInfo{ + store.EXPECT().GetUpload("b").Return(uploadB, nil), + uploadB.EXPECT().GetInfo().Return(FileInfo{ IsPartial: true, Size: 5, Offset: 5, @@ -101,7 +131,15 @@ func TestConcat(t *testing.T) { IsFinal: true, PartialUploads: []string{"a", "b"}, 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), ) @@ -136,12 +174,20 @@ func TestConcat(t *testing.T) { }) SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("foo").Return(FileInfo{ - IsFinal: true, - PartialUploads: []string{"a", "b"}, - Size: 10, - Offset: 10, - }, nil) + 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, + PartialUploads: []string{"a", "b"}, + Size: 10, + Offset: 10, + }, nil), + ) handler, _ := NewHandler(Config{ BasePath: "files", @@ -164,13 +210,21 @@ func TestConcat(t *testing.T) { }) 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 // will therefore cause the POST request to fail. - store.EXPECT().GetInfo("c").Return(FileInfo{ - IsPartial: true, - Size: 5, - Offset: 3, - }, nil) + gomock.InOrder( + store.EXPECT().GetUpload("c").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "c", + IsPartial: true, + Size: 5, + Offset: 3, + }, nil), + ) handler, _ := NewHandler(Config{ BasePath: "files", @@ -188,10 +242,18 @@ func TestConcat(t *testing.T) { }) SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("huge").Return(FileInfo{ - Size: 1000, - Offset: 1000, - }, nil) + 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, + Offset: 1000, + }, nil), + ) handler, _ := NewHandler(Config{ MaxSize: 100, @@ -210,11 +272,19 @@ func TestConcat(t *testing.T) { }) SubTest(t, "UploadToFinalFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("foo").Return(FileInfo{ - Size: 10, - Offset: 0, - IsFinal: true, - }, nil) + 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, + Offset: 0, + IsFinal: true, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, diff --git a/pkg/handler/f/config_test.go b/pkg/handler/config_test.go similarity index 100% rename from pkg/handler/f/config_test.go rename to pkg/handler/config_test.go diff --git a/pkg/handler/f/cors_test.go b/pkg/handler/cors_test.go similarity index 100% rename from pkg/handler/f/cors_test.go rename to pkg/handler/cors_test.go diff --git a/pkg/handler/f/terminate_test.go b/pkg/handler/f/terminate_test.go deleted file mode 100644 index b035d9f..0000000 --- a/pkg/handler/f/terminate_test.go +++ /dev/null @@ -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) - }) -} diff --git a/pkg/handler/f/head_test.go b/pkg/handler/head_test.go similarity index 80% rename from pkg/handler/f/head_test.go rename to pkg/handler/head_test.go index 73cc836..a7bc3e6 100644 --- a/pkg/handler/f/head_test.go +++ b/pkg/handler/head_test.go @@ -14,10 +14,12 @@ func TestHead(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() locker := NewMockLocker(ctrl) + upload := NewMockFullUpload(ctrl) gomock.InOrder( locker.EXPECT().LockUpload("yes"), - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ Offset: 11, Size: 44, MetaData: map[string]string{ @@ -59,7 +61,7 @@ func TestHead(t *testing.T) { }) 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{ StoreComposer: composer, @@ -83,10 +85,17 @@ func TestHead(t *testing.T) { }) SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("yes").Return(FileInfo{ - SizeIsDeferred: true, - Size: 0, - }, nil) + 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, + Size: 0, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -106,8 +115,13 @@ func TestHead(t *testing.T) { }) SubTest(t, "NoDeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ SizeIsDeferred: false, Size: 10, }, nil), diff --git a/pkg/handler/f/options_test.go b/pkg/handler/options_test.go similarity index 100% rename from pkg/handler/f/options_test.go rename to pkg/handler/options_test.go diff --git a/pkg/handler/f/patch_test.go b/pkg/handler/patch_test.go similarity index 74% rename from pkg/handler/f/patch_test.go rename to pkg/handler/patch_test.go index 9ed9283..44843da 100644 --- a/pkg/handler/f/patch_test.go +++ b/pkg/handler/patch_test.go @@ -17,14 +17,19 @@ import ( func TestPatch(t *testing.T) { SubTest(t, "UploadChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 5, Size: 10, }, nil), - store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), - store.EXPECT().FinishUpload("yes"), + upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), + upload.EXPECT().FinishUpload(), ) handler, _ := NewHandler(Config{ @@ -58,13 +63,19 @@ func TestPatch(t *testing.T) { }) SubTest(t, "MethodOverriding", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 5, - Size: 20, + Size: 10, }, 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{ @@ -89,10 +100,18 @@ func TestPatch(t *testing.T) { }) SubTest(t, "UploadChunkToFinished", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("yes").Return(FileInfo{ - Offset: 20, - Size: 20, - }, nil) + 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, + Size: 20, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -115,7 +134,7 @@ func TestPatch(t *testing.T) { }) 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{ StoreComposer: composer, @@ -134,9 +153,17 @@ func TestPatch(t *testing.T) { }) SubTest(t, "MissmatchingOffsetFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("yes").Return(FileInfo{ - Offset: 5, - }, nil) + 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, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -155,10 +182,18 @@ func TestPatch(t *testing.T) { }) SubTest(t, "ExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().GetInfo("yes").Return(FileInfo{ - Offset: 5, - Size: 10, - }, nil) + 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, + Size: 10, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -221,14 +256,19 @@ func TestPatch(t *testing.T) { // is that even if the uploader supplies more than 15 bytes, we only // pass 15 bytes to the data store and ignore the rest. + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 5, Size: 20, }, nil), - store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), - store.EXPECT().FinishUpload("yes"), + upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), + upload.EXPECT().FinishUpload(), ) handler, _ := NewHandler(Config{ @@ -258,16 +298,22 @@ func TestPatch(t *testing.T) { }) SubTest(t, "DeclareLengthOnFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 5, Size: 0, SizeIsDeferred: true, }, nil), - store.EXPECT().DeclareLength("yes", int64(20)), - store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), - store.EXPECT().FinishUpload("yes"), + store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload), + upload.EXPECT().DeclareLength(int64(20)), + upload.EXPECT().WriteChunk(int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil), + upload.EXPECT().FinishUpload(), ) handler, _ := NewHandler(Config{ @@ -295,15 +341,21 @@ func TestPatch(t *testing.T) { }) SubTest(t, "DeclareLengthAfterFinalChunk", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 20, Size: 0, SizeIsDeferred: true, }, nil), - store.EXPECT().DeclareLength("yes", int64(20)), - store.EXPECT().FinishUpload("yes"), + store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload), + upload.EXPECT().DeclareLength(int64(20)), + upload.EXPECT().FinishUpload(), ) handler, _ := NewHandler(Config{ @@ -327,23 +379,32 @@ func TestPatch(t *testing.T) { }) 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( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload1, nil), + upload1.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 5, Size: 0, SizeIsDeferred: true, }, nil), - store.EXPECT().DeclareLength("yes", int64(20)), - store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil), - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().AsLengthDeclarableUpload(upload1).Return(upload1), + upload1.EXPECT().DeclareLength(int64(20)), + 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", Offset: 10, Size: 20, SizeIsDeferred: false, }, nil), - store.EXPECT().WriteChunk("yes", int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil), - store.EXPECT().FinishUpload("yes"), + upload2.EXPECT().WriteChunk(int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil), + upload2.EXPECT().FinishUpload(), ) handler, _ := NewHandler(Config{ @@ -387,14 +448,17 @@ func TestPatch(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() locker := NewMockLocker(ctrl) + upload := NewMockFullUpload(ctrl) gomock.InOrder( 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, Size: 20, }, 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), ) @@ -420,13 +484,18 @@ func TestPatch(t *testing.T) { }) SubTest(t, "NotifyUploadProgress", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 0, Size: 100, }, 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{ @@ -487,14 +556,20 @@ func TestPatch(t *testing.T) { }) SubTest(t, "StopUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + gomock.InOrder( - store.EXPECT().GetInfo("yes").Return(FileInfo{ + store.EXPECT().GetUpload("yes").Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ ID: "yes", Offset: 0, Size: 100, }, nil), - store.EXPECT().WriteChunk("yes", int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose), - store.EXPECT().Terminate("yes").Return(nil), + upload.EXPECT().WriteChunk(int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose), + store.EXPECT().AsTerminatableUpload(upload).Return(upload), + upload.EXPECT().Terminate(), ) handler, _ := NewHandler(Config{ diff --git a/pkg/handler/f/post_test.go b/pkg/handler/post_test.go similarity index 74% rename from pkg/handler/f/post_test.go rename to pkg/handler/post_test.go index 1b3992a..406803b 100644 --- a/pkg/handler/f/post_test.go +++ b/pkg/handler/post_test.go @@ -14,13 +14,27 @@ import ( func TestPost(t *testing.T) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{ - "foo": "hello", - "bar": "world", - }, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{ + "foo": "hello", + "bar": "world", + }, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{ + "foo": "hello", + "bar": "world", + }, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -53,12 +67,22 @@ func TestPost(t *testing.T) { }) SubTest(t, "CreateEmptyUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 0, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) - store.EXPECT().FinishUpload("foo").Return(nil) + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 0, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 0, + MetaData: map[string]string{}, + }, nil), + upload.EXPECT().FinishUpload().Return(nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -173,10 +197,21 @@ func TestPost(t *testing.T) { SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -199,10 +234,21 @@ func TestPost(t *testing.T) { }) SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -226,10 +272,21 @@ func TestPost(t *testing.T) { }) SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -254,10 +311,21 @@ func TestPost(t *testing.T) { }) SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -286,6 +354,7 @@ func TestPost(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() locker := NewMockLocker(ctrl) + upload := NewMockFullUpload(ctrl) gomock.InOrder( store.EXPECT().NewUpload(FileInfo{ @@ -294,9 +363,17 @@ func TestPost(t *testing.T) { "foo": "hello", "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"), - 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"), ) @@ -327,10 +404,21 @@ func TestPost(t *testing.T) { }) SubTest(t, "CreateExceedingUploadSize", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer, @@ -350,10 +438,21 @@ func TestPost(t *testing.T) { }) SubTest(t, "IncorrectContentType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { - store.EXPECT().NewUpload(FileInfo{ - Size: 300, - MetaData: map[string]string{}, - }).Return("foo", nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + upload := NewMockFullUpload(ctrl) + + gomock.InOrder( + store.EXPECT().NewUpload(FileInfo{ + Size: 300, + MetaData: map[string]string{}, + }).Return(upload, nil), + upload.EXPECT().GetInfo().Return(FileInfo{ + ID: "foo", + Size: 300, + MetaData: map[string]string{}, + }, nil), + ) handler, _ := NewHandler(Config{ StoreComposer: composer,