diff --git a/ctx.go b/ctx.go index d39d5afa..5123341b 100644 --- a/ctx.go +++ b/ctx.go @@ -119,7 +119,7 @@ type ContextNoBody struct { } func (c ContextNoBody) Body() (any, error) { - slog.Warn("this method should not be called. It probably happened because you passed the context to another controller with the Pass method.") + slog.Warn("this method should not be called. It probably happened because you passed the context to another controller.") return body[map[string]any](c) } @@ -131,18 +131,6 @@ func (c ContextNoBody) MustBody() any { return b } -// SafeShallowCopy returns a safe shallow copy of the context. -// It allows to modify the base context while modifying the request context. -// It is data-safe, meaning that any sensitive data will not be shared between the original context and the copy. -func (c *ContextWithBody[B]) SafeShallowCopy() *ContextWithBody[B] { - c.pathParams = nil - c.body = nil - c.request = nil - c.response = nil - - return c -} - // SetStatus sets the status code of the response. // Alias to http.ResponseWriter.WriteHeader. func (c ContextNoBody) SetStatus(code int) { diff --git a/ctx_test.go b/ctx_test.go index 059dea17..bdc6460e 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -388,7 +388,7 @@ func TestMainLang(t *testing.T) { require.Equal(t, c.MainLocale(), "fr-CH") } -func TestClassicContext_Body(t *testing.T) { +func TestContextNoBody_Body(t *testing.T) { body := `{"name":"John","age":30}` r := httptest.NewRequest("GET", "/", strings.NewReader(body)) ctx := ContextNoBody{ @@ -403,7 +403,7 @@ func TestClassicContext_Body(t *testing.T) { }), res) } -func TestClassicContext_MustBody(t *testing.T) { +func TestContextNoBody_MustBody(t *testing.T) { t.Run("can read JSON body", func(t *testing.T) { body := `{"name":"John","age":30}` r := httptest.NewRequest("GET", "/", strings.NewReader(body)) @@ -430,3 +430,26 @@ func TestClassicContext_MustBody(t *testing.T) { }) }) } + +func TestContextNoBody_Redirect(t *testing.T) { + s := NewServer() + + Get(s, "/", func(c ContextNoBody) (any, error) { + return c.Redirect(301, "/foo") + }) + + Get(s, "/foo", func(c ContextNoBody) (ans, error) { + return ans{Ans: "foo"}, nil + }) + + t.Run("can redirect", func(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + + s.Mux.ServeHTTP(w, r) + + require.Equal(t, 301, w.Code) + require.Equal(t, "/foo", w.Header().Get("Location")) + require.Equal(t, "Moved Permanently.\n\n", w.Body.String()) + }) +}