Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#275] Add a new NoContent() method for Response #60

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 {
return &NoContentResponse{code[0], r.instance}
}

return &NoContentResponse{http.StatusNoContent, r.instance}
}

func (r *ContextResponse) Origin() contractshttp.ResponseOrigin {
return r.origin
}
Expand Down
38 changes: 38 additions & 0 deletions context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,44 @@ func TestResponse(t *testing.T) {
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",
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ go 1.21

require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/color v1.5.4
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.13.1-0.20240602025035-84b54a821232
github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef
github.com/rs/cors v1.11.0
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
Expand Down Expand Up @@ -86,6 +85,7 @@ require (
github.com/google/wire v0.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gookit/filter v1.2.1 // indirect
github.com/gookit/goutil v0.6.15 // indirect
github.com/goravel/file-rotatelogs/v2 v2.4.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ github.com/gookit/validate v1.5.2 h1:i5I2OQ7WYHFRPRATGu9QarR9snnNHydvwSuHXaRWAV0
github.com/gookit/validate v1.5.2/go.mod h1:yuPy2WwDlwGRa06fFJ5XIO8QEwhRnTC2LmxmBa5SE14=
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.13.1-0.20240602025035-84b54a821232 h1:bXgugUzsNE+mrX2xhdlPngCUxMlrmKQR+UWbpdhNgqA=
github.com/goravel/framework v1.13.1-0.20240602025035-84b54a821232/go.mod h1:pDVx2pvJ71YgiMnWWXeOFZzErlSsxkc9Sf4ywmhZBpQ=
github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef h1:FaTDqgX2ZvquEVWdv1Rm4PtUe8KINyD60AxNuS27VQI=
github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef/go.mod h1:pDVx2pvJ71YgiMnWWXeOFZzErlSsxkc9Sf4ywmhZBpQ=
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/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down
11 changes: 11 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func (r *JsonResponse) Render() error {
return nil
}

type NoContentResponse struct {
code int
instance *gin.Context
}

func (r *NoContentResponse) Render() error {
r.instance.Status(r.code)

return nil
}

type RedirectResponse struct {
code int
location string
Expand Down
6 changes: 3 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
"github.com/gookit/color"
"github.com/goravel/framework/contracts/config"
httpcontract "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/support"
"github.com/goravel/framework/support/color"
"github.com/savioxavier/termlink"
)

Expand Down Expand Up @@ -96,7 +96,7 @@ func (r *Route) Run(host ...string) error {
}

r.outputRoutes()
color.Greenln(termlink.Link("[HTTP] Listening and serving HTTP on", host[0]))
color.Green().Println(termlink.Link("[HTTP] Listening and serving HTTP on", host[0]))

server := &http.Server{
Addr: host[0],
Expand Down Expand Up @@ -133,7 +133,7 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
}

r.outputRoutes()
color.Greenln(termlink.Link("[HTTPS] Listening and serving HTTPS on", host))
color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", host))

return r.instance.RunTLS(host, certFile, keyFile)
}
Expand Down
Loading