1
0
mirror of https://github.com/labstack/echo.git synced 2025-12-07 23:12:43 +02:00

First commit to v3, #665

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-09-22 22:53:44 -07:00
parent 04f45046b1
commit 2aec0353f5
66 changed files with 656 additions and 3264 deletions

View File

@@ -2,17 +2,17 @@ package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)
func TestCORS(t *testing.T) {
e := echo.New()
req := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
cors := CORSWithConfig(CORSConfig{
AllowCredentials: true,
@@ -26,26 +26,26 @@ func TestCORS(t *testing.T) {
assert.Equal(t, "", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
// Empty origin header
req = test.NewRequest(echo.GET, "/", nil)
rec = test.NewResponseRecorder()
req, _ = http.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
req.Header().Set(echo.HeaderOrigin, "")
req.Header.Set(echo.HeaderOrigin, "")
h(c)
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
// Wildcard origin
req = test.NewRequest(echo.GET, "/", nil)
rec = test.NewResponseRecorder()
req, _ = http.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
req.Header().Set(echo.HeaderOrigin, "localhost")
req.Header.Set(echo.HeaderOrigin, "localhost")
h(c)
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
// Simple request
req = test.NewRequest(echo.GET, "/", nil)
rec = test.NewResponseRecorder()
req, _ = http.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
req.Header().Set(echo.HeaderOrigin, "localhost")
req.Header.Set(echo.HeaderOrigin, "localhost")
cors = CORSWithConfig(CORSConfig{
AllowOrigins: []string{"localhost"},
AllowCredentials: true,
@@ -58,11 +58,11 @@ func TestCORS(t *testing.T) {
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
// Preflight request
req = test.NewRequest(echo.OPTIONS, "/", nil)
rec = test.NewResponseRecorder()
req, _ = http.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)
req.Header.Set(echo.HeaderOrigin, "localhost")
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
h(c)
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
assert.NotEmpty(t, rec.Header().Get(echo.HeaderAccessControlAllowMethods))