Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retains HTTP Context values #455

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions pkg/handler/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 47 additions & 9 deletions pkg/handler/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd/pkg/handler"
)

Expand Down Expand Up @@ -35,16 +36,16 @@ 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{
"filename": "file.jpg\"evil",
"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),
)

Expand Down Expand Up @@ -79,8 +80,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),
)
Expand All @@ -107,8 +108,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",
Expand Down Expand Up @@ -139,8 +140,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",
Expand All @@ -165,4 +166,41 @@ func TestGet(t *testing.T) {
ResBody: "",
}).Run(handler, t)
})

SubTest(t, "Pass info threw HTTP context", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)

gomock.InOrder(
store.EXPECT().GetUpload(WrapsContext(context.Background()), "yes").DoAndReturn(func(ctx context.Context, id string) (Upload, error) {
assert.Equal(t, "42", ctx.Value("My-Key"))
return upload, nil
}),
upload.EXPECT().GetInfo(WrapsContext(context.Background())).Return(FileInfo{
Offset: 0,
}, nil),
)

handler, _ := NewHandler(Config{
StoreComposer: composer,
})
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "My-Key", "42")
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
(&httpTest{
Method: "GET",
URL: "yes",
ResHeader: map[string]string{
"Content-Length": "0",
"Content-Disposition": `attachment`,
},
Code: http.StatusNoContent,
ResBody: "",
}).Run(middleware(handler), t)
})
}
14 changes: 7 additions & 7 deletions pkg/handler/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
Loading