Ensure that the context values from the HTTP request are accessible

This commit is contained in:
Bastien Amar 2021-01-19 14:50:07 +01:00
parent 0f3122b4cb
commit f9cecfc5bb
1 changed files with 38 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd/pkg/handler"
)
@ -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)
})
}