Skip to content

Commit

Permalink
chore: add DefaultAbortStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Dec 30, 2024
1 parent 3953f90 commit 4f86e53
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.V
}

func (r *ContextRequest) Abort(code ...int) {
realCode := http.StatusBadRequest
realCode := contractshttp.DefaultAbortStatus
if len(code) > 0 {
realCode = code[0]
}
Expand Down
2 changes: 1 addition & 1 deletion group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func TestGroup(t *testing.T) {

func abortMiddleware() contractshttp.Middleware {
return func(ctx contractshttp.Context) {
ctx.Request().AbortWithStatus(http.StatusNonAuthoritativeInfo)
ctx.Request().Abort(http.StatusNonAuthoritativeInfo)
}
}

Expand Down
2 changes: 1 addition & 1 deletion middleware_cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Cors() http.Middleware {

if ctx.Request().Origin().Method == http.MethodOptions &&
ctx.Request().Header("Access-Control-Request-Method") != "" {
ctx.Request().AbortWithStatus(http.StatusNoContent)
ctx.Request().Abort(http.StatusNoContent)
}

ctx.Request().Next()
Expand Down
2 changes: 1 addition & 1 deletion middleware_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Timeout(timeout time.Duration) contractshttp.Middleware {
case <-done:
case <-timeoutCtx.Done():
if errors.Is(timeoutCtx.Err(), context.DeadlineExceeded) {
ctx.Request().AbortWithStatus(contractshttp.DefaultAbortStatus)
ctx.Request().Abort(contractshttp.StatusRequestTimeout)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions middleware_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/gin-gonic/gin"
contractshttp "github.com/goravel/framework/contracts/http"
mocksconfig "github.com/goravel/framework/mocks/config"
mockslog "github.com/goravel/framework/mocks/log"
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestTimeoutMiddleware(t *testing.T) {
require.NoError(t, err)

route.ServeHTTP(w, req)
assert.Equal(t, contractshttp.DefaultAbortStatus, w.Code)
assert.Equal(t, contractshttp.StatusRequestTimeout, w.Code)
})

t.Run("normal request", func(t *testing.T) {
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestTimeoutMiddleware(t *testing.T) {

t.Run("panic with custom recover", func(t *testing.T) {
globalRecover := func(ctx contractshttp.Context, err any) {
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, gin.H{"error": "Internal Panic"})
ctx.Request().Abort(http.StatusInternalServerError)
}
route.Recover(globalRecover)

Expand All @@ -83,6 +82,6 @@ func TestTimeoutMiddleware(t *testing.T) {

route.ServeHTTP(w, req)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "{\"error\":\"Internal Panic\"}", w.Body.String())
assert.Empty(t, w.Body.String())
})
}
2 changes: 1 addition & 1 deletion middleware_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Tls() contractshttp.Middleware {
})

if err := secureMiddleware.Process(ctx.Response().Writer(), ctx.Request().Origin()); err != nil {
ctx.Request().AbortWithStatus(http.StatusForbidden)
ctx.Request().Abort(http.StatusForbidden)
}

ctx.Request().Next()
Expand Down
2 changes: 1 addition & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

var globalRecoverCallback func(ctx contractshttp.Context, err any) = func(ctx contractshttp.Context, err any) {
LogFacade.WithContext(ctx).Request(ctx.Request()).Error(err)
ctx.Request().AbortWithStatus(http.StatusInternalServerError)
ctx.Request().Abort(http.StatusInternalServerError)
}

type Route struct {
Expand Down
5 changes: 2 additions & 3 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
contractshttp "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
Expand All @@ -34,7 +33,7 @@ func TestRecoverWithCustomCallback(t *testing.T) {
assert.Nil(t, err)

globalRecover := func(ctx contractshttp.Context, err any) {
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, gin.H{"error": "Internal Panic"})
ctx.Request().Abort(http.StatusInternalServerError)
}

route.Recover(globalRecover)
Expand All @@ -45,7 +44,7 @@ func TestRecoverWithCustomCallback(t *testing.T) {

route.ServeHTTP(w, req)

assert.Equal(t, "{\"error\":\"Internal Panic\"}", w.Body.String())
assert.Empty(t, w.Body.String())
assert.Equal(t, http.StatusInternalServerError, w.Code)

mockConfig.AssertExpectations(t)
Expand Down

0 comments on commit 4f86e53

Please sign in to comment.