2016-04-08 01:16:58 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-04-08 01:16:58 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCORS(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-11-13 06:24:53 +02:00
|
|
|
|
2016-11-22 00:42:13 +02:00
|
|
|
// Wildcard origin
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2016-11-13 06:24:53 +02:00
|
|
|
h := CORS()(echo.NotFoundHandler)
|
2016-04-08 01:16:58 +02:00
|
|
|
h(c)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
2016-04-08 01:16:58 +02:00
|
|
|
|
2016-11-22 00:42:13 +02:00
|
|
|
// Allow origins
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.GET, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-11-22 00:42:13 +02:00
|
|
|
h = CORSWithConfig(CORSConfig{
|
|
|
|
AllowOrigins: []string{"localhost"},
|
|
|
|
})(echo.NotFoundHandler)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
2016-04-08 01:16:58 +02:00
|
|
|
h(c)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// Preflight request
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.OPTIONS, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
2016-11-13 06:24:53 +02:00
|
|
|
cors := CORSWithConfig(CORSConfig{
|
|
|
|
AllowOrigins: []string{"localhost"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 3600,
|
|
|
|
})
|
|
|
|
h = cors(echo.NotFoundHandler)
|
2016-04-08 01:16:58 +02:00
|
|
|
h(c)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
|
|
assert.NotEmpty(t, rec.Header().Get(echo.HeaderAccessControlAllowMethods))
|
|
|
|
assert.Equal(t, "true", rec.Header().Get(echo.HeaderAccessControlAllowCredentials))
|
|
|
|
assert.Equal(t, "3600", rec.Header().Get(echo.HeaderAccessControlMaxAge))
|
2018-06-19 15:49:23 +02:00
|
|
|
|
|
|
|
// Preflight request with `AllowOrigins` *
|
|
|
|
req = httptest.NewRequest(echo.OPTIONS, "/", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
2018-07-03 18:51:15 +02:00
|
|
|
cors = CORSWithConfig(CORSConfig{
|
2018-06-19 15:49:23 +02:00
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 3600,
|
|
|
|
})
|
|
|
|
h = cors(echo.NotFoundHandler)
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
|
|
assert.NotEmpty(t, rec.Header().Get(echo.HeaderAccessControlAllowMethods))
|
|
|
|
assert.Equal(t, "true", rec.Header().Get(echo.HeaderAccessControlAllowCredentials))
|
|
|
|
assert.Equal(t, "3600", rec.Header().Get(echo.HeaderAccessControlMaxAge))
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|