Skip to content

Commit

Permalink
Gin support (rs#51)
Browse files Browse the repository at this point in the history
* Impl gin.HandlerFunc wrappers for Cors handler
* Provide an example for Gin
* Update TravisCI matrix from Go 1.8 to Go 1.9 + 1.10
  • Loading branch information
tyranron authored and rs committed Mar 15, 2018
1 parent 36f00aa commit ff4d280
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: go
go:
- 1.8
- 1.9
- "1.10"
- tip
matrix:
allow_failures:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The server now runs on `localhost:8080`:
* [HttpRouter](https://github.com/julienschmidt/httprouter): [examples/httprouter/server.go](https://github.com/rs/cors/blob/master/examples/httprouter/server.go)
* [Gorilla](http://www.gorillatoolkit.org/pkg/mux): [examples/gorilla/server.go](https://github.com/rs/cors/blob/master/examples/gorilla/server.go)
* [Buffalo](https://gobuffalo.io): [examples/buffalo/server.go](https://github.com/rs/cors/blob/master/examples/buffalo/server.go)
* [Gin](https://gin-gonic.github.io/gin): [examples/gin/server.go](https://github.com/rs/cors/blob/master/examples/gin/server.go)

## Parameters

Expand Down
19 changes: 19 additions & 0 deletions examples/gin/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"net/http"

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

func main() {
router := gin.Default()

router.Use(cors.Default())
router.GET("/", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"hello": "world"})
})

router.Run(":8080")
}
50 changes: 50 additions & 0 deletions wrapper/gin/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package cors/wrapper/gin provides gin.HandlerFunc to handle CORS related
// requests as a wrapper of github.com/rs/cors handler.
package gin

import (
"net/http"

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

// Options is a configuration container to setup the CORS middleware.
type Options = cors.Options

// corsWrapper is a wrapper of cors.Cors handler which preserves information
// about configured 'optionPassthrough' option.
type corsWrapper struct {
*cors.Cors
optionPassthrough 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 &&
ctx.Request.Method == http.MethodOptions &&
ctx.GetHeader("Access-Control-Request-Method") != "" {
// Abort processing next Gin middlewares.
ctx.AbortWithStatus(http.StatusOK)
}
}
}

// AllowAll creates a new CORS Gin middleware with permissive configuration
// allowing all origins with all standard methods with any header and
// credentials.
func AllowAll() gin.HandlerFunc {
return corsWrapper{Cors: cors.AllowAll()}.build()
}

// Default creates a new CORS Gin middleware with default options.
func Default() gin.HandlerFunc {
return corsWrapper{Cors: cors.Default()}.build()
}

// 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()
}
76 changes: 76 additions & 0 deletions wrapper/gin/gin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gin

import (
"net/http"
"net/http/httptest"
"testing"

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

func init() {
gin.SetMode(gin.ReleaseMode)
}

func TestAllowAllNotNil(t *testing.T) {
handler := AllowAll()
if handler == nil {
t.Error("Should not return nil Gin handler")
}
}

func TestDefaultNotNil(t *testing.T) {
handler := Default()
if handler == nil {
t.Error("Should not return nil Gin handler")
}
}

func TestNewNotNil(t *testing.T) {
handler := New(Options{})
if handler == nil {
t.Error("Should not return nil Gin handler")
}
}

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("Access-Control-Request-Method", "POST")
ctx.Status(http.StatusAccepted)
res.Code = http.StatusAccepted

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

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")
}
}

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("Access-Control-Request-Method", "POST")

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

handler(ctx)

if ctx.IsAborted() {
t.Error("Should not abort when OPTIONS passthrough enabled")
}
}

0 comments on commit ff4d280

Please sign in to comment.