From 946babf47fa561d43cbc2a1e26cae9cf53fabb27 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Thu, 1 Feb 2024 08:15:29 +0100 Subject: [PATCH] Tests Redirection and removes unused code --- ctx.go | 14 +------------- ctx_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) 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..7d25d4f9 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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()) + }) +}