-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
014d716
commit cb0761e
Showing
6 changed files
with
473 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
# cors | ||
# cors | ||
[![Build Status](https://travis-ci.org/go-http-utils/cors.svg?branch=master)](https://travis-ci.org/go-http-utils/cors) | ||
[![Coverage Status](https://coveralls.io/repos/github/go-http-utils/cors/badge.svg?branch=master)](https://coveralls.io/github/go-http-utils/cors?branch=master) | ||
|
||
CORS middleware for Go. | ||
|
||
## Installation | ||
|
||
``` | ||
go get -u github.com/go-http-utils/cors | ||
``` | ||
|
||
## Documentation | ||
|
||
API documentation can be found here: https://godoc.org/github.com/go-http-utils/cors | ||
|
||
## Usage | ||
|
||
```go | ||
import ( | ||
"github.com/go-http-utils/cors" | ||
) | ||
``` | ||
|
||
```go | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { | ||
res.Write([]byte("Hello World")) | ||
}) | ||
|
||
http.ListenAndServe(":8080", cors.Handler(mux)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package cors | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-http-utils/headers" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type CorsSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *CorsSuite) TestDefaultAllowOrigin() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("*", res.Header.Get(headers.AccessControlAllowOrigin)) | ||
} | ||
|
||
func (s *CorsSuite) TestReflectAllowOrigin() { | ||
origin := "helloworld.org" | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
req, err := http.NewRequest(http.MethodGet, server.URL+"/", nil) | ||
|
||
s.Nil(err) | ||
|
||
req.Header.Set(headers.Origin, origin) | ||
|
||
res, err := sendRequest(req) | ||
|
||
s.Nil(err) | ||
s.Equal(origin, res.Header.Get(headers.AccessControlAllowOrigin)) | ||
} | ||
|
||
func (s *CorsSuite) TestValidatorAllowOrigin() { | ||
origin := "helloworld.org" | ||
validator := func(req *http.Request) string { | ||
return origin | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetAllowOriginValidator(validator), SetAllowOrigin(false))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
req, err := http.NewRequest(http.MethodGet, server.URL+"/", nil) | ||
|
||
s.Nil(err) | ||
|
||
req.Header.Set(headers.Origin, "test.org") | ||
|
||
res, err := sendRequest(req) | ||
|
||
s.Nil(err) | ||
s.Equal(origin, res.Header.Get(headers.AccessControlAllowOrigin)) | ||
} | ||
|
||
func (s *CorsSuite) TestNotAllowOrigin() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetAllowOrigin(false))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("", res.Header.Get(headers.AccessControlAllowOrigin)) | ||
s.Equal("", res.Header.Get(headers.AccessControlAllowMethods)) | ||
} | ||
|
||
func (s *CorsSuite) TestEmptyExpose() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("", res.Header.Get(headers.AccessControlExposeHeaders)) | ||
} | ||
|
||
func (s *CorsSuite) TestExpose() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetExposeHeaders([]string{headers.AcceptRanges, headers.AcceptDatetime}))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal(headers.AcceptRanges+","+headers.AcceptDatetime, | ||
res.Header.Get(headers.AccessControlExposeHeaders)) | ||
} | ||
|
||
func (s *CorsSuite) TestMaxAge() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetMaxAge(600))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("600", res.Header.Get(headers.AccessControlMaxAge)) | ||
} | ||
|
||
func (s *CorsSuite) TestDefualtMethods() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("GET,HEAD,PUT,POST,DELETE", | ||
res.Header.Get(headers.AccessControlAllowMethods)) | ||
} | ||
|
||
func (s *CorsSuite) TestMethods() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetMethods([]string{http.MethodHead, http.MethodTrace}))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("HEAD,TRACE", res.Header.Get(headers.AccessControlAllowMethods)) | ||
} | ||
|
||
func (s *CorsSuite) TestCredentials() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetCredentials(true))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
res, err := http.Get(server.URL + "/") | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusOK, res.StatusCode) | ||
s.Equal("true", res.Header.Get(headers.AccessControlAllowCredentials)) | ||
} | ||
|
||
func (s *CorsSuite) TestDefualtAllowHeader() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
req, err := http.NewRequest(http.MethodGet, server.URL+"/", nil) | ||
|
||
s.Nil(err) | ||
|
||
req.Header.Set(headers.AccessControlRequestHeaders, "FOO-BAR") | ||
|
||
res, err := sendRequest(req) | ||
|
||
s.Nil(err) | ||
s.Equal("FOO-BAR", res.Header.Get(headers.AccessControlAllowHeaders)) | ||
} | ||
|
||
func (s *CorsSuite) TestAllowHeader() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc), | ||
SetAllowHeaders([]string{"FOO", "BAR"}))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
req, err := http.NewRequest(http.MethodGet, server.URL+"/", nil) | ||
|
||
s.Nil(err) | ||
|
||
req.Header.Set(headers.AccessControlRequestHeaders, "FOO-BAR") | ||
|
||
res, err := sendRequest(req) | ||
|
||
s.Nil(err) | ||
s.Equal("Foo,Bar", res.Header.Get(headers.AccessControlAllowHeaders)) | ||
} | ||
|
||
func (s *CorsSuite) TestOptionRequest() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", Handler(http.HandlerFunc(helloHandlerFunc))) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
req, err := http.NewRequest(http.MethodOptions, server.URL+"/", nil) | ||
|
||
s.Nil(err) | ||
|
||
res, err := sendRequest(req) | ||
|
||
s.Nil(err) | ||
s.Equal(http.StatusNoContent, res.StatusCode) | ||
} | ||
|
||
func TestCors(t *testing.T) { | ||
suite.Run(t, new(CorsSuite)) | ||
} | ||
|
||
func helloHandlerFunc(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusOK) | ||
|
||
res.Write([]byte("Hello World")) | ||
} | ||
|
||
func sendRequest(req *http.Request) (*http.Response, error) { | ||
cli := &http.Client{} | ||
|
||
return cli.Do(req) | ||
} |
Oops, something went wrong.