handler: Retain request context values while detaching to avoid cancellation.

Fixes #315
This commit is contained in:
Nolan Woods 2020-01-02 18:20:07 -08:00 committed by Bastien Amar
parent 0822c0ac43
commit 0f3122b4cb
8 changed files with 153 additions and 109 deletions

View File

@ -38,14 +38,14 @@ func TestConcat(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
IsPartial: true,
IsFinal: false,
PartialUploads: nil,
MetaData: make(map[string]string),
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
IsPartial: true,
@ -77,8 +77,8 @@ func TestConcat(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
IsPartial: true,
}, nil),
@ -114,26 +114,26 @@ func TestConcat(t *testing.T) {
uploadC := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "a").Return(uploadA, nil),
uploadA.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "a").Return(uploadA, nil),
uploadA.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
IsPartial: true,
Size: 5,
Offset: 5,
}, nil),
store.EXPECT().GetUpload(context.Background(), "b").Return(uploadB, nil),
uploadB.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "b").Return(uploadB, nil),
uploadB.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
IsPartial: true,
Size: 5,
Offset: 5,
}, nil),
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 10,
IsPartial: false,
IsFinal: true,
PartialUploads: []string{"a", "b"},
MetaData: make(map[string]string),
}).Return(uploadC, nil),
uploadC.EXPECT().GetInfo(context.Background()).Return(FileInfo{
uploadC.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 10,
IsPartial: false,
@ -142,7 +142,7 @@ func TestConcat(t *testing.T) {
MetaData: make(map[string]string),
}, nil),
store.EXPECT().AsConcatableUpload(uploadC).Return(uploadC),
uploadC.EXPECT().ConcatUploads(context.Background(), []Upload{uploadA, uploadB}).Return(nil),
uploadC.EXPECT().ConcatUploads(WrapsContext(context.Background()), []Upload{uploadA, uploadB}).Return(nil),
)
handler, _ := NewHandler(Config{
@ -188,8 +188,8 @@ func TestConcat(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
IsFinal: true,
PartialUploads: []string{"a", "b"},
@ -226,8 +226,8 @@ func TestConcat(t *testing.T) {
// This upload is still unfinished (mismatching offset and size) and
// will therefore cause the POST request to fail.
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "c").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "c").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "c",
IsPartial: true,
Size: 5,
@ -256,8 +256,8 @@ func TestConcat(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "huge").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "huge").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "huge",
Size: 1000,
Offset: 1000,
@ -286,8 +286,8 @@ func TestConcat(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 10,
Offset: 0,

View File

@ -35,8 +35,8 @@ func TestGet(t *testing.T) {
gomock.InOrder(
locker.EXPECT().NewLock("yes").Return(lock, nil),
lock.EXPECT().Lock().Return(nil),
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 5,
Size: 20,
MetaData: map[string]string{
@ -44,7 +44,7 @@ func TestGet(t *testing.T) {
"filetype": "image/jpeg",
},
}, nil),
upload.EXPECT().GetReader(context.Background()).Return(reader, nil),
upload.EXPECT().GetReader(WrapsContext(context.Background())).Return(reader, nil),
lock.EXPECT().Unlock().Return(nil),
)
@ -79,8 +79,8 @@ func TestGet(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 0,
}, nil),
)
@ -107,8 +107,8 @@ func TestGet(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 0,
MetaData: map[string]string{
"filetype": "non-a-valid-mime-type",
@ -139,8 +139,8 @@ func TestGet(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 0,
MetaData: map[string]string{
"filetype": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.v1",

View File

@ -21,8 +21,8 @@ func TestHead(t *testing.T) {
gomock.InOrder(
locker.EXPECT().NewLock("yes").Return(lock, nil),
lock.EXPECT().Lock().Return(nil),
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 11,
Size: 44,
MetaData: map[string]string{
@ -64,7 +64,7 @@ func TestHead(t *testing.T) {
})
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetUpload(context.Background(), "no").Return(nil, os.ErrNotExist)
store.EXPECT().GetUpload(WrapsContext(context.Background()), "no").Return(nil, os.ErrNotExist)
handler, _ := NewHandler(Config{
StoreComposer: composer,
@ -93,8 +93,8 @@ func TestHead(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
SizeIsDeferred: true,
Size: 0,
}, nil),
@ -123,8 +123,8 @@ func TestHead(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
SizeIsDeferred: false,
Size: 10,
}, nil),

View File

@ -23,14 +23,14 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 10,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().FinishUpload(context.Background()),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -75,14 +75,14 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 10,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().FinishUpload(context.Background()),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -112,8 +112,8 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 20,
Size: 20,
@ -141,7 +141,7 @@ func TestPatch(t *testing.T) {
})
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetUpload(context.Background(), "no").Return(nil, os.ErrNotExist)
store.EXPECT().GetUpload(WrapsContext(context.Background()), "no").Return(nil, os.ErrNotExist)
handler, _ := NewHandler(Config{
StoreComposer: composer,
@ -165,8 +165,8 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
}, nil),
@ -194,8 +194,8 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 10,
@ -268,14 +268,14 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 20,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
upload.EXPECT().FinishUpload(context.Background()),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -310,17 +310,17 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 0,
SizeIsDeferred: true,
}, nil),
store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
upload.EXPECT().DeclareLength(context.Background(), int64(20)),
upload.EXPECT().WriteChunk(context.Background(), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
upload.EXPECT().FinishUpload(context.Background()),
upload.EXPECT().DeclareLength(WrapsContext(context.Background()), int64(20)),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(5), NewReaderMatcher("hellothisismore")).Return(int64(15), nil),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -353,16 +353,16 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 20,
Size: 0,
SizeIsDeferred: true,
}, nil),
store.EXPECT().AsLengthDeclarableUpload(upload).Return(upload),
upload.EXPECT().DeclareLength(context.Background(), int64(20)),
upload.EXPECT().FinishUpload(context.Background()),
upload.EXPECT().DeclareLength(WrapsContext(context.Background()), int64(20)),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -392,26 +392,26 @@ func TestPatch(t *testing.T) {
upload2 := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload1, nil),
upload1.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload1, nil),
upload1.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 5,
Size: 0,
SizeIsDeferred: true,
}, nil),
store.EXPECT().AsLengthDeclarableUpload(upload1).Return(upload1),
upload1.EXPECT().DeclareLength(context.Background(), int64(20)),
upload1.EXPECT().WriteChunk(context.Background(), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
upload1.EXPECT().DeclareLength(WrapsContext(context.Background()), int64(20)),
upload1.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(5), NewReaderMatcher("hello")).Return(int64(5), nil),
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload2, nil),
upload2.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload2, nil),
upload2.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 10,
Size: 20,
SizeIsDeferred: false,
}, nil),
upload2.EXPECT().WriteChunk(context.Background(), int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil),
upload2.EXPECT().FinishUpload(context.Background()),
upload2.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(10), NewReaderMatcher("thisismore")).Return(int64(10), nil),
upload2.EXPECT().FinishUpload(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{
@ -461,13 +461,13 @@ func TestPatch(t *testing.T) {
gomock.InOrder(
locker.EXPECT().NewLock("yes").Return(lock, nil),
lock.EXPECT().Lock().Return(nil),
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 0,
Size: 20,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
lock.EXPECT().Unlock().Return(nil),
)
@ -498,13 +498,13 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 0,
Size: 100,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil),
)
handler, _ := NewHandler(Config{
@ -572,15 +572,15 @@ func TestPatch(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "yes",
Offset: 0,
Size: 100,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(0), NewReaderMatcher("first ")).Return(int64(6), http.ErrBodyReadAfterClose),
store.EXPECT().AsTerminatableUpload(upload).Return(upload),
upload.EXPECT().Terminate(context.Background()),
upload.EXPECT().Terminate(WrapsContext(context.Background())),
)
handler, _ := NewHandler(Config{

View File

@ -20,7 +20,7 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{
"foo": "hello",
@ -28,7 +28,7 @@ func TestPost(t *testing.T) {
"empty": "",
},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{
@ -76,16 +76,16 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 0,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 0,
MetaData: map[string]string{},
}, nil),
upload.EXPECT().FinishUpload(context.Background()).Return(nil),
upload.EXPECT().FinishUpload(WrapsContext(context.Background())).Return(nil),
)
handler, _ := NewHandler(Config{
@ -211,11 +211,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
@ -248,11 +248,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
@ -286,11 +286,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
@ -325,11 +325,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
@ -367,14 +367,14 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{
@ -384,7 +384,7 @@ func TestPost(t *testing.T) {
}, nil),
locker.EXPECT().NewLock("foo").Return(lock, nil),
lock.EXPECT().Lock().Return(nil),
upload.EXPECT().WriteChunk(context.Background(), int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
upload.EXPECT().WriteChunk(WrapsContext(context.Background()), int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
lock.EXPECT().Unlock().Return(nil),
)
@ -420,11 +420,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
@ -454,11 +454,11 @@ func TestPost(t *testing.T) {
upload := NewMockFullUpload(ctrl)
gomock.InOrder(
store.EXPECT().NewUpload(context.Background(), FileInfo{
store.EXPECT().NewUpload(WrapsContext(context.Background()), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},

View File

@ -40,13 +40,13 @@ func TestTerminate(t *testing.T) {
gomock.InOrder(
locker.EXPECT().NewLock("foo").Return(lock, nil),
lock.EXPECT().Lock().Return(nil),
store.EXPECT().GetUpload(context.Background(), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
store.EXPECT().GetUpload(WrapsContext(context.Background()), "foo").Return(upload, nil),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
ID: "foo",
Size: 10,
}, nil),
store.EXPECT().AsTerminatableUpload(upload).Return(upload),
upload.EXPECT().Terminate(context.Background()).Return(nil),
upload.EXPECT().Terminate(WrapsContext(context.Background())).Return(nil),
lock.EXPECT().Unlock().Return(nil),
)

View File

@ -0,0 +1,26 @@
package handler
import (
"context"
"github.com/golang/mock/gomock"
)
type contextWithValuesMatcher struct {
baseCtx context.Context
}
func WrapsContext(ctx context.Context) gomock.Matcher {
return contextWithValuesMatcher{ctx}
}
func (c contextWithValuesMatcher) Matches(x interface{}) bool {
ctx, ok := x.(contextWithValues)
if !ok {
return false
}
return ctx.Context == c.baseCtx
}
func (c contextWithValuesMatcher) String() string {
return "wraps base context "
}

View File

@ -55,6 +55,24 @@ func NewHTTPError(err error, statusCode int) HTTPError {
return httpError{err, statusCode}
}
type contextWithValues struct {
context.Context
valueHolder context.Context
}
func (c contextWithValues) Value(key interface{}) interface{} {
return c.valueHolder.Value(key)
}
func newContextWithValues(ctx context.Context) contextWithValues {
return contextWithValues{
// Use background to not get cancel event
Context: context.Background(),
// Use request context to get stored values
valueHolder: ctx,
}
}
var (
ErrUnsupportedVersion = NewHTTPError(errors.New("unsupported version"), http.StatusPreconditionFailed)
ErrMaxSizeExceeded = NewHTTPError(errors.New("maximum size exceeded"), http.StatusRequestEntityTooLarge)
@ -274,7 +292,7 @@ func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler {
// PostFile creates a new file upload using the datastore after validating the
// length and parsing the metadata.
func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := newContextWithValues(r.Context())
// Check for presence of application/offset+octet-stream. If another content
// type is defined, it will be ignored and treated as none was set because
@ -417,7 +435,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
// HeadFile returns the length and offset for the HEAD request
func (handler *UnroutedHandler) HeadFile(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := newContextWithValues(r.Context())
id, err := extractIDFromPath(r.URL.Path)
if err != nil {
@ -481,7 +499,7 @@ func (handler *UnroutedHandler) HeadFile(w http.ResponseWriter, r *http.Request)
// PatchFile adds a chunk to an upload. This operation is only allowed
// if enough space in the upload is left.
func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := newContextWithValues(r.Context())
// Check for presence of application/offset+octet-stream
if r.Header.Get("Content-Type") != "application/offset+octet-stream" {
@ -702,7 +720,7 @@ func (handler *UnroutedHandler) finishUploadIfComplete(ctx context.Context, uplo
// GetFile handles requests to download a file using a GET request. This is not
// part of the specification.
func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := newContextWithValues(r.Context())
id, err := extractIDFromPath(r.URL.Path)
if err != nil {
@ -823,7 +841,7 @@ func filterContentType(info FileInfo) (contentType string, contentDisposition st
// DelFile terminates an upload permanently.
func (handler *UnroutedHandler) DelFile(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := newContextWithValues(r.Context())
// Abort the request handling if the required interface is not implemented
if !handler.composer.UsesTerminater {