Skip to content

Commit

Permalink
feat: [#507] Add Abort method for Response (#119)
Browse files Browse the repository at this point in the history
* add abort method

* update go mod file

* add abort

* write test cases

* update framework

* fix lint
  • Loading branch information
kkumar-gcc authored Dec 29, 2024
1 parent d790c69 commit 1d01a59
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
16 changes: 8 additions & 8 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.Cont
return r
}

func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.AbortableResponse {
return &DataResponse{code, contentType, data, r.instance}
}

Expand All @@ -65,11 +65,11 @@ func (r *ContextResponse) Header(key, value string) contractshttp.ContextRespons
return r
}

func (r *ContextResponse) Json(code int, obj any) contractshttp.Response {
func (r *ContextResponse) Json(code int, obj any) contractshttp.AbortableResponse {
return &JsonResponse{code, obj, r.instance}
}

func (r *ContextResponse) NoContent(code ...int) contractshttp.Response {
func (r *ContextResponse) NoContent(code ...int) contractshttp.AbortableResponse {
if len(code) > 0 {
return &NoContentResponse{code[0], r.instance}
}
Expand All @@ -81,11 +81,11 @@ func (r *ContextResponse) Origin() contractshttp.ResponseOrigin {
return r.origin
}

func (r *ContextResponse) Redirect(code int, location string) contractshttp.Response {
func (r *ContextResponse) Redirect(code int, location string) contractshttp.AbortableResponse {
return &RedirectResponse{code, location, r.instance}
}

func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.Response {
func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.AbortableResponse {
return &StringResponse{code, format, r.instance, values}
}

Expand Down Expand Up @@ -128,15 +128,15 @@ func NewStatus(instance *gin.Context, code int) *Status {
return &Status{instance, code}
}

func (r *Status) Data(contentType string, data []byte) contractshttp.Response {
func (r *Status) Data(contentType string, data []byte) contractshttp.AbortableResponse {
return &DataResponse{r.status, contentType, data, r.instance}
}

func (r *Status) Json(obj any) contractshttp.Response {
func (r *Status) Json(obj any) contractshttp.AbortableResponse {
return &JsonResponse{r.status, obj, r.instance}
}

func (r *Status) String(format string, values ...any) contractshttp.Response {
func (r *Status) String(format string, values ...any) contractshttp.AbortableResponse {
return &StringResponse{r.status, format, r.instance, values}
}

Expand Down
44 changes: 44 additions & 0 deletions context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ func (s *ContextResponseSuite) TestWithoutCookie() {
s.True(exist)
}

func (s *ContextResponseSuite) TestAbort() {
s.route.Get("/abort-redirected", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().String(http.StatusOK, "redirected")
})

s.route.Middleware(testJson()).Get("/json/abort", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().String(http.StatusOK, "Goravel")
})

s.route.Middleware(testRedirect()).Get("/redirect/abort", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().String(http.StatusOK, "redirect")
})

code, body, _, _ := s.request("GET", "/json/abort", nil)
s.Equal(contractshttp.StatusOK, code)
s.JSONEq(`{"name":"abort json"}`, body)

code, _, headers, _ := s.request("GET", "/redirect/abort", nil)
s.Equal(contractshttp.StatusMovedPermanently, code)
s.Equal("/abort-redirected", headers.Get("Location"))
}

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)
Expand All @@ -319,3 +341,25 @@ func (s *ContextResponseSuite) request(method, url string, body io.Reader) (int,

return w.Code, w.Body.String(), w.Header(), w.Result().Cookies()
}

func testJson() contractshttp.Middleware {
return func(ctx contractshttp.Context) {
err := ctx.Response().Json(contractshttp.StatusOK, map[string]any{
"name": "abort json",
}).Abort()
if err != nil {
panic(err)
}
ctx.Request().Next()
}
}

func testRedirect() contractshttp.Middleware {
return func(ctx contractshttp.Context) {
err := ctx.Response().Redirect(contractshttp.StatusMovedPermanently, "/abort-redirected").Abort()
if err != nil {
panic(err)
}
ctx.Request().Next()
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.4
require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.4
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866
github.com/rs/cors v1.11.1
github.com/savioxavier/termlink v1.4.1
github.com/spf13/cast v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ github.com/gookit/validate v1.5.4 h1:nwBo6vULnVUeNFCOde6RKFRbOCKJXVMnWR0ghedacLg
github.com/gookit/validate v1.5.4/go.mod h1:p9sRPfpvYB4vXICBpEPzv8FoAky+XhUOhWQghgmmat4=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd h1:+EjxacwKKlrb8zZLsXO7cOoflS3rAfQjSso2+yVXGpI=
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd/go.mod h1:9saug/7ORKnkeoPXxB7/QH3tVx4c9Z8y7uH4INUQX1E=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866 h1:Xz7jgacMzIN8mVzORo5xDlorANNnSDt54BhVtQ2ppM8=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
30 changes: 30 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func (r *DataResponse) Render() error {
return nil
}

func (r *DataResponse) Abort() error {
r.instance.Abort()

return r.Render()
}

type DownloadResponse struct {
filename string
filepath string
Expand Down Expand Up @@ -55,6 +61,12 @@ func (r *JsonResponse) Render() error {
return nil
}

func (r *JsonResponse) Abort() error {
r.instance.Abort()

return r.Render()
}

type NoContentResponse struct {
code int
instance *gin.Context
Expand All @@ -66,6 +78,12 @@ func (r *NoContentResponse) Render() error {
return nil
}

func (r *NoContentResponse) Abort() error {
r.instance.AbortWithStatus(r.code)

return nil
}

type RedirectResponse struct {
code int
location string
Expand All @@ -78,6 +96,12 @@ func (r *RedirectResponse) Render() error {
return nil
}

func (r *RedirectResponse) Abort() error {
r.instance.Abort()

return r.Render()
}

type StringResponse struct {
code int
format string
Expand All @@ -91,6 +115,12 @@ func (r *StringResponse) Render() error {
return nil
}

func (r *StringResponse) Abort() error {
r.instance.Abort()

return r.Render()
}

type HtmlResponse struct {
data any
instance *gin.Context
Expand Down

0 comments on commit 1d01a59

Please sign in to comment.