mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
9a28fb8608
enable cors to use regex pattern for allowed origins implementation is similar to another popular cors middleware: https://github.com/astaxie/beego/blob/master/plugins/cors/cors.go#L196-L201
224 lines
6.7 KiB
Go
224 lines
6.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCORS(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
// Wildcard origin
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
h := CORS()(echo.NotFoundHandler)
|
|
h(c)
|
|
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
|
|
// Allow origins
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
h = CORSWithConfig(CORSConfig{
|
|
AllowOrigins: []string{"localhost"},
|
|
})(echo.NotFoundHandler)
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
|
h(c)
|
|
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
|
|
// Preflight request
|
|
req = httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
cors := CORSWithConfig(CORSConfig{
|
|
AllowOrigins: []string{"localhost"},
|
|
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))
|
|
|
|
// Preflight request with `AllowOrigins` *
|
|
req = httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
req.Header.Set(echo.HeaderOrigin, "localhost")
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
cors = CORSWithConfig(CORSConfig{
|
|
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))
|
|
|
|
// 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{
|
|
AllowOrigins: []string{"http://*.example.com"},
|
|
})
|
|
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))
|
|
}
|
|
|
|
func Test_allowOriginScheme(t *testing.T) {
|
|
tests := []struct {
|
|
domain, pattern string
|
|
expected bool
|
|
}{
|
|
{
|
|
domain: "http://example.com",
|
|
pattern: "http://example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "https://example.com",
|
|
pattern: "https://example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://example.com",
|
|
pattern: "https://example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: "https://example.com",
|
|
pattern: "http://example.com",
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
e := echo.New()
|
|
for _, tt := range tests {
|
|
req := httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
req.Header.Set(echo.HeaderOrigin, tt.domain)
|
|
cors := CORSWithConfig(CORSConfig{
|
|
AllowOrigins: []string{tt.pattern},
|
|
})
|
|
h := cors(echo.NotFoundHandler)
|
|
h(c)
|
|
|
|
if tt.expected {
|
|
assert.Equal(t, tt.domain, rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
} else {
|
|
assert.Equal(t, "", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
}
|
|
}
|
|
}
|
|
|
|
func Test_allowOriginSubdomain(t *testing.T) {
|
|
tests := []struct {
|
|
domain, pattern string
|
|
expected bool
|
|
}{
|
|
{
|
|
domain: "http://aaa.example.com",
|
|
pattern: "http://*.example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://bbb.aaa.example.com",
|
|
pattern: "http://*.example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://bbb.aaa.example.com",
|
|
pattern: "http://*.aaa.example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://aaa.example.com:8080",
|
|
pattern: "http://*.example.com:8080",
|
|
expected: true,
|
|
},
|
|
|
|
{
|
|
domain: "http://fuga.hoge.com",
|
|
pattern: "http://*.example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: "http://ccc.bbb.example.com",
|
|
pattern: "http://*.aaa.example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
|
|
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
|
|
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
|
|
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com`,
|
|
pattern: "http://*.example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com`,
|
|
pattern: "http://*.example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: "http://ccc.bbb.example.com",
|
|
pattern: "http://example.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
domain: "https://prod-preview--aaa.bbb.com",
|
|
pattern: "https://*--aaa.bbb.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://ccc.bbb.example.com",
|
|
pattern: "http://*.example.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
domain: "http://ccc.bbb.example.com",
|
|
pattern: "http://foo.[a-z]*.example.com",
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
e := echo.New()
|
|
for _, tt := range tests {
|
|
req := httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
req.Header.Set(echo.HeaderOrigin, tt.domain)
|
|
cors := CORSWithConfig(CORSConfig{
|
|
AllowOrigins: []string{tt.pattern},
|
|
})
|
|
h := cors(echo.NotFoundHandler)
|
|
h(c)
|
|
|
|
if tt.expected {
|
|
assert.Equal(t, tt.domain, rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
} else {
|
|
assert.Equal(t, "", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
|
}
|
|
}
|
|
}
|