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

[BUG] feat(context): update request before calling next #16

Merged
merged 2 commits into from
May 10, 2023

Conversation

tigerwill90
Copy link
Owner

@tigerwill90 tigerwill90 commented May 10, 2023

When a wrapped http.Handler middleware replace the request, WrapM does not update the context request before calling next.

func TestWrapM(t *testing.T) {
	wrapped := WrapM(func(handler http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                        // clone and update the request
			req := r.Clone(r.Context())
			req.Header.Set("foo", "bar")
                        // call the next handler with the new request
			handler.ServeHTTP(w, req)
			_, _ = w.Write([]byte("fox"))
		})
	})
	invoked := false

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "https://example.com/foo", nil)

	fox := New(WithMiddleware(wrapped))
	fox.MustHandle(http.MethodGet, "/foo", func(c Context) {
                 // We should have the cloned request
		assert.Equal(t, "bar", c.Header("foo"))
		invoked = true
	})

	fox.ServeHTTP(w, r)
	assert.Equal(t, "fox", w.Body.String())
	assert.True(t, invoked)
}

@tigerwill90 tigerwill90 self-assigned this May 10, 2023
@tigerwill90 tigerwill90 added the bug Something isn't working label May 10, 2023
@tigerwill90 tigerwill90 requested review from 4nth0 and pawndev May 10, 2023 12:37
@tigerwill90 tigerwill90 merged commit 5887e08 into master May 10, 2023
@tigerwill90 tigerwill90 deleted the bug/wrapm-adapter branch May 10, 2023 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants