Ensure that the context values from the HTTP request are accessible
This commit is contained in:
parent
0f3122b4cb
commit
f9cecfc5bb
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue