Skip to content

Commit

Permalink
add NoContent method
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Jun 4, 2024
1 parent 6dd02df commit 86afc84
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (r *ContextResponse) Json(code int, obj any) contractshttp.Response {
return &JsonResponse{code, obj, r.instance}
}

func (r *ContextResponse) NoContent(code ...int) contractshttp.Response {
if len(code) == 0 {
code = append(code, http.StatusNoContent)
}

return &NoContentResponse{code[0], r.instance}
}

func (r *ContextResponse) Origin() contractshttp.ResponseOrigin {
return r.origin
}
Expand Down
36 changes: 36 additions & 0 deletions context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,42 @@ func TestResponse(t *testing.T) {
expectBody: "Goravel",
expectHeader: "goravel",
},
{
name: "NoContent",
method: "GET",
url: "/no/content",
setup: func(method, url string) error {
fiber.Get("/no/content", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().NoContent()

Check failure on line 231 in context_response_test.go

View workflow job for this annotation

GitHub Actions / lint

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)

Check failure on line 231 in context_response_test.go

View workflow job for this annotation

GitHub Actions / codecov

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)

Check failure on line 231 in context_response_test.go

View workflow job for this annotation

GitHub Actions / windows (1.21)

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)

Check failure on line 231 in context_response_test.go

View workflow job for this annotation

GitHub Actions / test (1.21)

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method 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 {
fiber.Get("/no/content/with/code", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().NoContent(http.StatusCreated)

Check failure on line 249 in context_response_test.go

View workflow job for this annotation

GitHub Actions / lint

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent) (typecheck)

Check failure on line 249 in context_response_test.go

View workflow job for this annotation

GitHub Actions / codecov

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)

Check failure on line 249 in context_response_test.go

View workflow job for this annotation

GitHub Actions / windows (1.21)

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)

Check failure on line 249 in context_response_test.go

View workflow job for this annotation

GitHub Actions / test (1.21)

ctx.Response().NoContent undefined (type "github.com/goravel/framework/contracts/http".ContextResponse has no field or method NoContent)
})

var err error
req, err = http.NewRequest(method, url, nil)
if err != nil {
return err
}
return nil
},
expectCode: http.StatusCreated,
},
{
name: "Origin",
method: "GET",
Expand Down
9 changes: 9 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func (r *JsonResponse) Render() error {
return r.instance.Status(r.code).JSON(r.obj)
}

type NoContentResponse struct {
code int
instance *fiber.Ctx
}

func (r *NoContentResponse) Render() error {
return r.instance.Status(r.code).Send(nil)
}

type RedirectResponse struct {
code int
location string
Expand Down

0 comments on commit 86afc84

Please sign in to comment.