MCPcopy
hub / github.com/gofiber/fiber / Test_Ctx_SaveFileToStorage_ContextPropagation

Function Test_Ctx_SaveFileToStorage_ContextPropagation

ctx_test.go:5946–5997  ·  ctx_test.go::Test_Ctx_SaveFileToStorage_ContextPropagation

go test -run Test_Ctx_SaveFileToStorage_ContextPropagation

(t *testing.T)

Source from the content-addressed store, hash-verified

5944
5945// go test -run Test_Ctx_SaveFileToStorage_ContextPropagation
5946func Test_Ctx_SaveFileToStorage_ContextPropagation(t *testing.T) {
5947 t.Parallel()
5948
5949 type ctxKeyType string
5950
5951 const ctxKey ctxKeyType = "storage-context-key"
5952
5953 storage := &mockContextAwareStorage{t: t, key: ctxKey, expectedValue: "expected-context-value"}
5954 app := New()
5955
5956 app.Post("/test", func(c Ctx) error {
5957 fh, err := c.FormFile("file")
5958 require.NoError(t, err)
5959
5960 ctxWithValue := context.WithValue(context.Background(), ctxKey, storage.expectedValue)
5961 ctx, cancel := context.WithCancel(ctxWithValue)
5962 storage.validateCtx = func(received context.Context) {
5963 if received != ctx {
5964 storage.helperFailure("storage received unexpected context instance")
5965 }
5966 }
5967 storage.cancel = cancel
5968
5969 c.SetContext(ctx)
5970
5971 err = c.SaveFileToStorage(fh, "test", storage)
5972 require.NoError(t, err)
5973
5974 require.True(t, storage.ctxMatched.Load(), "storage should receive the context installed on Ctx")
5975 require.True(t, storage.cancelObserved.Load(), "storage should observe context cancellation")
5976
5977 return nil
5978 })
5979
5980 body := &bytes.Buffer{}
5981 writer := multipart.NewWriter(body)
5982
5983 ioWriter, err := writer.CreateFormFile("file", "test")
5984 require.NoError(t, err)
5985
5986 _, err = ioWriter.Write([]byte("hello world"))
5987 require.NoError(t, err)
5988 require.NoError(t, writer.Close())
5989
5990 req := httptest.NewRequest(MethodPost, "/test", body)
5991 req.Header.Set("Content-Type", writer.FormDataContentType())
5992 req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
5993
5994 resp, err := app.Test(req)
5995 require.NoError(t, err, "app.Test(req)")
5996 require.Equal(t, StatusOK, resp.StatusCode, "Status code")
5997}
5998
5999// go test -run Test_Ctx_Secure
6000func Test_Ctx_Secure(t *testing.T) {

Callers

nothing calls this directly

Calls 12

helperFailureMethod · 0.95
BytesMethod · 0.95
TestMethod · 0.80
NewFunction · 0.70
PostMethod · 0.65
FormFileMethod · 0.65
SetContextMethod · 0.65
SaveFileToStorageMethod · 0.65
LoadMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected