From 014b5419474305ec019080b561399331dbed2d87 Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Sat, 29 Jun 2024 12:00:45 +0800 Subject: [PATCH] fix: cookie MaxAge is incorrect (#66) * fix: cookie MaxAge is incorrect * optimize test cases * optimize name * optimize test cases * update name --- context_response.go | 7 +- context_response_test.go | 857 ++++++++++++--------------------------- test.txt | 1 + 3 files changed, 259 insertions(+), 606 deletions(-) create mode 100644 test.txt diff --git a/context_response.go b/context_response.go index 9c32099..0cd8072 100644 --- a/context_response.go +++ b/context_response.go @@ -5,8 +5,8 @@ import ( "net/http" "github.com/gin-gonic/gin" - contractshttp "github.com/goravel/framework/contracts/http" + "github.com/goravel/framework/support/carbon" ) type ContextResponse struct { @@ -19,6 +19,11 @@ func NewContextResponse(instance *gin.Context, origin contractshttp.ResponseOrig } func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.ContextResponse { + if cookie.MaxAge == 0 { + if !cookie.Expires.IsZero() { + cookie.MaxAge = int(cookie.Expires.Sub(carbon.Now().StdTime()).Seconds()) + } + } r.instance.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly) return r diff --git a/context_response_test.go b/context_response_test.go index d02085a..16b75f7 100644 --- a/context_response_test.go +++ b/context_response_test.go @@ -1,642 +1,289 @@ package gin import ( + "io" "net/http" "net/http/httptest" "strings" "testing" contractshttp "github.com/goravel/framework/contracts/http" - configmocks "github.com/goravel/framework/mocks/config" - "github.com/goravel/framework/support/json" - "github.com/stretchr/testify/assert" + mocksconfig "github.com/goravel/framework/mocks/config" + "github.com/stretchr/testify/suite" ) -func TestResponse(t *testing.T) { - var ( - err error - gin *Route - req *http.Request - mockConfig *configmocks.Config - ) - beforeEach := func() { - mockConfig = &configmocks.Config{} - mockConfig.On("GetBool", "app.debug").Return(true).Once() - mockConfig.On("GetInt", "http.drivers.gin.body_limit", 4096).Return(4096).Once() - } - tests := []struct { - name string - method string - url string - cookieName string - setup func(method, url string) error - expectCode int - expectBody string - expectHeader string - expectCookieValue string - }{ - { - name: "Data", - method: "GET", - url: "/data", - setup: func(method, url string) error { - gin.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Data(http.StatusOK, "text/html; charset=utf-8", []byte("Goravel")) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, - { - name: "Success Data", - method: "GET", - url: "/success/data", - setup: func(method, url string) error { - gin.Get("/success/data", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().Data("text/html; charset=utf-8", []byte("Goravel")) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, - { - name: "Json", - method: "GET", - url: "/json", - setup: func(method, url string) error { - gin.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Json(http.StatusOK, contractshttp.Json{ - "id": "1", - }) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "{\"id\":\"1\"}", - }, - { - name: "String", - method: "GET", - url: "/string", - setup: func(method, url string) error { - gin.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().String(http.StatusCreated, "Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusCreated, - expectBody: "Goravel", - }, - { - name: "Success Json", - method: "GET", - url: "/success/json", - setup: func(method, url string) error { - gin.Get("/success/json", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().Json(contractshttp.Json{ - "id": "1", - }) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "{\"id\":\"1\"}", - }, - { - name: "Success String", - method: "GET", - url: "/success/string", - setup: func(method, url string) error { - gin.Get("/success/string", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().String("Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, - { - name: "File", - method: "GET", - url: "/file", - setup: func(method, url string) error { - gin.Get("/file", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().File("./README.md") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - }, - { - name: "Download", - method: "GET", - url: "/download", - setup: func(method, url string) error { - gin.Get("/download", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Download("./README.md", "README.md") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - }, - { - name: "Header", - method: "GET", - url: "/header", - setup: func(method, url string) error { - gin.Get("/header", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Header("Hello", "goravel").String(http.StatusOK, "Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - expectHeader: "goravel", - }, - { - name: "NoContent", - method: "GET", - url: "/no/content", - setup: func(method, url string) error { - gin.Get("/no/content", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().NoContent() - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusNoContent, - }, - { - name: "NoContentWithCode", - method: "GET", - url: "/no/content/with/code", - setup: func(method, url string) error { - gin.Get("/no/content/with/code", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().NoContent(http.StatusAccepted) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusAccepted, - }, - { - name: "Origin", - method: "GET", - url: "/origin", - setup: func(method, url string) error { - mockConfig.On("Get", "cors.paths").Return([]string{}).Once() - mockConfig.On("GetString", "http.tls.host").Return("").Once() - mockConfig.On("GetString", "http.tls.port").Return("").Once() - mockConfig.On("GetString", "http.tls.ssl.cert").Return("").Once() - mockConfig.On("GetString", "http.tls.ssl.key").Return("").Once() - ConfigFacade = mockConfig - - gin.GlobalMiddleware(func(ctx contractshttp.Context) { - ctx.Response().Header("global", "goravel") - ctx.Request().Next() - - assert.Equal(t, "Goravel", ctx.Response().Origin().Body().String()) - assert.Equal(t, "goravel", ctx.Response().Origin().Header().Get("global")) - assert.Equal(t, 7, ctx.Response().Origin().Size()) - assert.Equal(t, 200, ctx.Response().Origin().Status()) - }) - gin.Get("/origin", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().String(http.StatusOK, "Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, - { - name: "Redirect", - method: "GET", - url: "/redirect", - setup: func(method, url string) error { - gin.Get("/redirect", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Redirect(http.StatusMovedPermanently, "https://goravel.dev") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusMovedPermanently, - expectBody: "Moved Permanently.\n\n", - }, - { - name: "WithoutCookie", - method: "GET", - url: "/without/cookie", - cookieName: "goravel", - setup: func(method, url string) error { - gin.Get("/without/cookie", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().WithoutCookie("goravel").String(http.StatusOK, "Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - req.AddCookie(&http.Cookie{ - Name: "goravel", - Value: "goravel", - }) - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - expectCookieValue: "", - }, - { - name: "Cookie", - method: "GET", - url: "/cookie", - cookieName: "goravel", - setup: func(method, url string) error { - gin.Get("/cookie", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Cookie(contractshttp.Cookie{ - Name: "goravel", - Value: "goravel", - }).String(http.StatusOK, "Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - expectCookieValue: "goravel", - }, - } +type ContextResponseSuite struct { + suite.Suite + route *Route + mockConfig *mocksconfig.Config +} - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - beforeEach() - gin, err = NewRoute(mockConfig, nil) - assert.Nil(t, err) - - assert.Nil(t, test.setup(test.method, test.url)) - - w := httptest.NewRecorder() - - gin.ServeHTTP(w, req) - - if test.expectBody != "" { - assert.Equal(t, test.expectBody, w.Body.String(), test.name) - } - if test.expectHeader != "" { - assert.Equal(t, test.expectHeader, strings.Join(w.Header().Values("Hello"), ""), test.name) - } - if test.cookieName != "" { - cookies := w.Result().Cookies() - exist := false - for _, cookie := range cookies { - if cookie.Name == test.cookieName { - exist = true - assert.Equal(t, test.expectCookieValue, cookie.Value) - } - } - assert.True(t, exist) - } - assert.Equal(t, test.expectCode, w.Code, test.name) - - mockConfig.AssertExpectations(t) - }) - } +func TestContextResponseSuite(t *testing.T) { + suite.Run(t, new(ContextResponseSuite)) } -func TestResponse_Success(t *testing.T) { - var ( - err error - route *Route - req *http.Request - mockConfig *configmocks.Config - ) - beforeEach := func() { - mockConfig = &configmocks.Config{} - mockConfig.On("GetBool", "app.debug").Return(false).Once() - mockConfig.On("GetInt", "http.drivers.gin.body_limit", 4096).Return(4096).Once() - ConfigFacade = mockConfig - } - tests := []struct { - name string - method string - url string - setup func(method, url string) error - expectCode int - expectBody string - expectBodyJson string - }{ - { - name: "Data", - method: "GET", - url: "/data", - setup: func(method, url string) error { - route.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().Data("text/html; charset=utf-8", []byte("Goravel")) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, - { - name: "Json", - method: "GET", - url: "/json", - setup: func(method, url string) error { - route.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().Json(contractshttp.Json{ - "id": "1", - }) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBodyJson: "{\"id\":\"1\"}", - }, - { - name: "String", - method: "GET", - url: "/string", - setup: func(method, url string) error { - route.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Success().String("Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusOK, - expectBody: "Goravel", - }, +func (s *ContextResponseSuite) SetupTest() { + s.mockConfig = &mocksconfig.Config{} + s.mockConfig.EXPECT().GetBool("app.debug").Return(true).Once() + s.mockConfig.EXPECT().GetInt("http.drivers.gin.body_limit", 4096).Return(4096).Once() + + var err error + s.route, err = NewRoute(s.mockConfig, nil) + s.Require().Nil(err) +} + +func (s *ContextResponseSuite) TearDownTest() { + s.mockConfig.AssertExpectations(s.T()) +} + +func (s *ContextResponseSuite) TestCookie() { + cookieName := "goravel" + s.route.Get("/cookie", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Cookie(contractshttp.Cookie{ + Name: cookieName, + Value: "Goravel", + }).String(http.StatusOK, "Goravel") + }) + + code, body, _, cookies := s.request("GET", "/cookie", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) + exist := false + for _, cookie := range cookies { + if cookie.Name == cookieName { + exist = true + s.Equal("Goravel", cookie.Value) + } } + s.True(exist) +} + +func (s *ContextResponseSuite) TestData() { + s.route.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Data(http.StatusOK, "text/html; charset=utf-8", []byte("Goravel")) + }) - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - beforeEach() - route, err = NewRoute(mockConfig, nil) - assert.Nil(t, err) + code, body, _, _ := s.request("GET", "/data", nil) - err := test.setup(test.method, test.url) - assert.Nil(t, err) + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} + +func (s *ContextResponseSuite) TestDownload() { + s.route.Get("/download", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Download("./test.txt", "README.md") + }) - w := httptest.NewRecorder() - route.ServeHTTP(w, req) + code, body, _, _ := s.request("GET", "/download", nil) - if test.expectBody != "" { - assert.Equal(t, test.expectBody, w.Body.String()) - } - if test.expectBodyJson != "" { - bodyMap := make(map[string]any) - exceptBodyMap := make(map[string]any) + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} - err = json.Unmarshal(w.Body.Bytes(), &bodyMap) - assert.Nil(t, err) - err = json.UnmarshalString(test.expectBodyJson, &exceptBodyMap) - assert.Nil(t, err) +func (s *ContextResponseSuite) TestFile() { + s.route.Get("/file", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().File("./test.txt") + }) - assert.Equal(t, exceptBodyMap, bodyMap) - } + code, body, _, _ := s.request("GET", "/file", nil) - assert.Equal(t, test.expectCode, w.Code) + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} - mockConfig.AssertExpectations(t) +func (s *ContextResponseSuite) TestHeader() { + s.route.Get("/header", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Header("Hello", "Goravel").String(http.StatusOK, "Goravel") + }) + + code, body, header, _ := s.request("GET", "/header", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) + s.Equal("Goravel", strings.Join(header.Values("Hello"), "")) +} + +func (s *ContextResponseSuite) TestJson() { + s.route.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Json(http.StatusOK, contractshttp.Json{ + "id": "1", }) - } + }) + + code, body, _, _ := s.request("GET", "/json", nil) + + s.Equal("{\"id\":\"1\"}", body) + s.Equal(http.StatusOK, code) } -func TestResponse_Status(t *testing.T) { - var ( - err error - route *Route - req *http.Request - mockConfig *configmocks.Config - ) - beforeEach := func() { - mockConfig = &configmocks.Config{} - mockConfig.On("GetBool", "app.debug").Return(false).Once() - mockConfig.On("GetInt", "http.drivers.gin.body_limit", 4096).Return(4096).Once() - ConfigFacade = mockConfig - } - tests := []struct { - name string - method string - url string - setup func(method, url string) error - expectCode int - expectBody string - expectBodyJson string - }{ - { - name: "Data", - method: "GET", - url: "/data", - setup: func(method, url string) error { - route.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Status(http.StatusCreated).Data("text/html; charset=utf-8", []byte("Goravel")) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusCreated, - expectBody: "Goravel", - }, - { - name: "Json", - method: "GET", - url: "/json", - setup: func(method, url string) error { - route.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Status(http.StatusCreated).Json(contractshttp.Json{ - "id": "1", - }) - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusCreated, - expectBodyJson: "{\"id\":\"1\"}", - }, - { - name: "String", - method: "GET", - url: "/string", - setup: func(method, url string) error { - route.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { - return ctx.Response().Status(http.StatusCreated).String("Goravel") - }) - - var err error - req, err = http.NewRequest(method, url, nil) - if err != nil { - return err - } - - return nil - }, - expectCode: http.StatusCreated, - expectBody: "Goravel", - }, - } +func (s *ContextResponseSuite) TestNoContent() { + s.route.Get("/no-content", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().NoContent() + }) + + code, body, _, _ := s.request("GET", "/no-content", nil) + + s.Empty(body) + s.Equal(http.StatusNoContent, code) +} - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - beforeEach() - route, err = NewRoute(mockConfig, nil) - assert.Nil(t, err) +func (s *ContextResponseSuite) TestNoContent_WithCode() { + s.route.Get("/no-content-with-code", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().NoContent(http.StatusAccepted) + }) - err := test.setup(test.method, test.url) - assert.Nil(t, err) + code, body, _, _ := s.request("GET", "/no-content-with-code", nil) - w := httptest.NewRecorder() - route.ServeHTTP(w, req) + s.Empty(body) + s.Equal(http.StatusAccepted, code) +} + +func (s *ContextResponseSuite) TestOrigin() { + s.mockConfig.EXPECT().Get("cors.paths").Return([]string{}).Once() + s.mockConfig.EXPECT().GetString("http.tls.host").Return("").Once() + s.mockConfig.EXPECT().GetString("http.tls.port").Return("").Once() + s.mockConfig.EXPECT().GetString("http.tls.ssl.cert").Return("").Once() + s.mockConfig.EXPECT().GetString("http.tls.ssl.key").Return("").Once() + ConfigFacade = s.mockConfig + + s.route.GlobalMiddleware(func(ctx contractshttp.Context) { + ctx.Response().Header("global", "goravel") + ctx.Request().Next() + + s.Equal("Goravel", ctx.Response().Origin().Body().String()) + s.Equal("goravel", ctx.Response().Origin().Header().Get("global")) + s.Equal(7, ctx.Response().Origin().Size()) + s.Equal(http.StatusOK, ctx.Response().Origin().Status()) + }) + s.route.Get("/origin", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().String(http.StatusOK, "Goravel") + }) + + code, body, _, _ := s.request("GET", "/origin", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} - if test.expectBody != "" { - assert.Equal(t, test.expectBody, w.Body.String()) - } - if test.expectBodyJson != "" { - bodyMap := make(map[string]any) - exceptBodyMap := make(map[string]any) +func (s *ContextResponseSuite) TestRedirect() { + s.route.Get("/redirect", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Redirect(http.StatusMovedPermanently, "https://goravel.dev") + }) - err = json.Unmarshal(w.Body.Bytes(), &bodyMap) - assert.Nil(t, err) - err = json.UnmarshalString(test.expectBodyJson, &exceptBodyMap) - assert.Nil(t, err) + code, body, _, _ := s.request("GET", "/redirect", nil) - assert.Equal(t, exceptBodyMap, bodyMap) - } + s.Equal("Moved Permanently.\n\n", body) + s.Equal(http.StatusMovedPermanently, code) +} - assert.Equal(t, test.expectCode, w.Code) +func (s *ContextResponseSuite) TestString() { + s.route.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().String(http.StatusCreated, "Goravel") + }) - mockConfig.AssertExpectations(t) + code, body, _, _ := s.request("GET", "/string", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusCreated, code) +} + +func (s *ContextResponseSuite) TestSuccess_Data() { + s.route.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Success().Data("text/html; charset=utf-8", []byte("Goravel")) + }) + + code, body, _, _ := s.request("GET", "/data", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} + +func (s *ContextResponseSuite) TestSuccess_Json() { + s.route.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Success().Json(contractshttp.Json{ + "id": "1", + }) + }) + + code, body, _, _ := s.request("GET", "/json", nil) + + s.Equal("{\"id\":\"1\"}", body) + s.Equal(http.StatusOK, code) +} + +func (s *ContextResponseSuite) TestSuccess_String() { + s.route.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Success().String("Goravel") + }) + + code, body, _, _ := s.request("GET", "/string", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) +} + +func (s *ContextResponseSuite) TestStatus_Data() { + s.route.Get("/data", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Status(http.StatusCreated).Data("text/html; charset=utf-8", []byte("Goravel")) + }) + + code, body, _, _ := s.request("GET", "/data", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusCreated, code) +} + +func (s *ContextResponseSuite) TestStatus_Json() { + s.route.Get("/json", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Status(http.StatusCreated).Json(contractshttp.Json{ + "id": "1", }) + }) + + code, body, _, _ := s.request("GET", "/json", nil) + + s.Equal("{\"id\":\"1\"}", body) + s.Equal(http.StatusCreated, code) +} + +func (s *ContextResponseSuite) TestStatus_String() { + s.route.Get("/string", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().Status(http.StatusCreated).String("Goravel") + }) + + code, body, _, _ := s.request("GET", "/string", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusCreated, code) +} + +func (s *ContextResponseSuite) TestWithoutCookie() { + cookieName := "goravel" + s.route.Get("/without-cookie", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().WithoutCookie(cookieName).String(http.StatusOK, "Goravel") + }) + + code, body, _, cookies := s.request("GET", "/without-cookie", nil) + + s.Equal("Goravel", body) + s.Equal(http.StatusOK, code) + exist := false + for _, cookie := range cookies { + if cookie.Name == cookieName { + exist = true + s.Empty(cookie.Value) + } } + s.True(exist) +} + +func (s *ContextResponseSuite) request(method, url string, body io.Reader) (int, string, http.Header, []*http.Cookie) { + req, err := http.NewRequest(method, url, body) + s.Require().Nil(err) + + w := httptest.NewRecorder() + s.route.ServeHTTP(w, req) + + return w.Code, w.Body.String(), w.Header(), w.Result().Cookies() } diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c00271d --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Goravel \ No newline at end of file