2016-04-08 01:16:58 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-10-14 17:16:58 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-04-08 01:16:58 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-04-08 01:16:58 +02:00
|
|
|
"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
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", 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
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", 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
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodOptions, "/", 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` *
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodOptions, "/", nil)
|
2018-06-19 15:49:23 +02:00
|
|
|
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))
|
2019-03-09 20:32:49 +02:00
|
|
|
|
|
|
|
// Preflight request with `AllowOrigins` which allow all subdomains with *
|
|
|
|
req = httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
req.Header.Set(echo.HeaderOrigin, "http://aaa.example.com")
|
|
|
|
cors = CORSWithConfig(CORSConfig{
|
2020-01-25 19:48:53 +02:00
|
|
|
AllowOrigins: []string{"http://*.example.com"},
|
2019-03-09 20:32:49 +02:00
|
|
|
})
|
|
|
|
h = cors(echo.NotFoundHandler)
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, "http://aaa.example.com", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
|
|
|
|
|
|
req.Header.Set(echo.HeaderOrigin, "http://bbb.example.com")
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, "http://bbb.example.com", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|