Skip to content

Commit

Permalink
Make Gin wrapper's status configurable and use 204 as default (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Apr 28, 2024
1 parent 4c32059 commit 85fc0ca
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 104 deletions.
18 changes: 14 additions & 4 deletions wrapper/gin/gin.go
Expand Up @@ -16,18 +16,19 @@ type Options = cors.Options
// about configured 'optionPassthrough' option.
type corsWrapper struct {
*cors.Cors
optionPassthrough bool
optionsSuccessStatus int
optionsPassthrough bool
}

// build transforms wrapped cors.Cors handler into Gin middleware.
func (c corsWrapper) build() gin.HandlerFunc {
return func(ctx *gin.Context) {
c.HandlerFunc(ctx.Writer, ctx.Request)
if !c.optionPassthrough &&
if !c.optionsPassthrough &&
ctx.Request.Method == http.MethodOptions &&
ctx.GetHeader("Access-Control-Request-Method") != "" {
// Abort processing next Gin middlewares.
ctx.AbortWithStatus(http.StatusOK)
ctx.AbortWithStatus(c.optionsSuccessStatus)
}
}
}
Expand All @@ -46,5 +47,14 @@ func Default() gin.HandlerFunc {

// New creates a new CORS Gin middleware with the provided options.
func New(options Options) gin.HandlerFunc {
return corsWrapper{cors.New(options), options.OptionsPassthrough}.build()
status := options.OptionsSuccessStatus
if status == 0 {
status = http.StatusNoContent
}
wrapper := corsWrapper{
Cors: cors.New(options),
optionsSuccessStatus: status,
optionsPassthrough: options.OptionsPassthrough,
}
return wrapper.build()
}
17 changes: 7 additions & 10 deletions wrapper/gin/gin_test.go
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/rs/cors"
)

func init() {
Expand Down Expand Up @@ -38,35 +37,33 @@ func TestCorsWrapper_buildAbortsWhenPreflight(t *testing.T) {
res := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(res)
ctx.Request, _ = http.NewRequest("OPTIONS", "http://example.com/foo", nil)
ctx.Request.Header.Add("Origin", "http://example.com/")
ctx.Request.Header.Add("Origin", "http://example.org")
ctx.Request.Header.Add("Access-Control-Request-Method", "POST")
ctx.Status(http.StatusAccepted)
res.Code = http.StatusAccepted

handler := corsWrapper{Cors: cors.New(Options{
// Intentionally left blank.
})}.build()
handler := New(Options{ /* Intentionally left blank. */ })

handler(ctx)

if !ctx.IsAborted() {
t.Error("Should abort on preflight requests")
}
if res.Code != http.StatusOK {
t.Error("Should abort with 200 OK status")
if res.Code != http.StatusNoContent {
t.Error("Should abort with 204 Non Content status")
}
}

func TestCorsWrapper_buildNotAbortsWhenPassthrough(t *testing.T) {
res := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(res)
ctx.Request, _ = http.NewRequest("OPTIONS", "http://example.com/foo", nil)
ctx.Request.Header.Add("Origin", "http://example.com/")
ctx.Request.Header.Add("Origin", "http://example.org")
ctx.Request.Header.Add("Access-Control-Request-Method", "POST")

handler := corsWrapper{cors.New(Options{
handler := New(Options{
OptionsPassthrough: true,
}), true}.build()
})

handler(ctx)

Expand Down
36 changes: 22 additions & 14 deletions wrapper/gin/go.mod
Expand Up @@ -3,27 +3,35 @@ module github.com/rs/cors/wrapper/gin
go 1.17

require (
github.com/gin-gonic/gin v1.7.7
github.com/rs/cors v1.8.1
github.com/gin-gonic/gin v1.9.1
github.com/rs/cors v1.11.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/rs/cors => ../../

0 comments on commit 85fc0ca

Please sign in to comment.