Skip to content

Commit c7c792d

Browse files
committed
Fix CSRF tests for Go 1.12
1 parent 36f524e commit c7c792d

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

middleware/csrf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
110110
if config.CookieMaxAge == 0 {
111111
config.CookieMaxAge = DefaultCSRFConfig.CookieMaxAge
112112
}
113-
if config.CookieSameSite == http.SameSiteNoneMode {
113+
if config.CookieSameSite == SameSiteNoneMode {
114114
config.CookieSecure = true
115115
}
116116

middleware/csrf_samesite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !go1.12
1+
// +build go1.13
22

33
package middleware
44

middleware/csrf_samesite_1.12.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build go1.12
1+
// +build !go1.13
22

33
package middleware
44

middleware/csrf_samesite_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// +build go1.13
2+
3+
package middleware
4+
5+
import (
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/labstack/echo/v4"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// Test for SameSiteModeNone moved to separate file for Go 1.12 support
15+
func TestCSRFWithSameSiteModeNone(t *testing.T) {
16+
e := echo.New()
17+
req := httptest.NewRequest(http.MethodGet, "/", nil)
18+
rec := httptest.NewRecorder()
19+
c := e.NewContext(req, rec)
20+
21+
csrf := CSRFWithConfig(CSRFConfig{
22+
CookieSameSite: SameSiteNoneMode,
23+
})
24+
25+
h := csrf(func(c echo.Context) error {
26+
return c.String(http.StatusOK, "test")
27+
})
28+
29+
r := h(c)
30+
assert.NoError(t, r)
31+
assert.Regexp(t, "SameSite=None", rec.Header()["Set-Cookie"])
32+
assert.Regexp(t, "Secure", rec.Header()["Set-Cookie"])
33+
}

middleware/csrf_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,3 @@ func TestCSRFWithSameSiteDefaultMode(t *testing.T) {
138138
fmt.Println(rec.Header()["Set-Cookie"])
139139
assert.NotRegexp(t, "SameSite=", rec.Header()["Set-Cookie"])
140140
}
141-
142-
func TestCSRFWithSameSiteModeNone(t *testing.T) {
143-
e := echo.New()
144-
req := httptest.NewRequest(http.MethodGet, "/", nil)
145-
rec := httptest.NewRecorder()
146-
c := e.NewContext(req, rec)
147-
148-
csrf := CSRFWithConfig(CSRFConfig{
149-
CookieSameSite: SameSiteNoneMode,
150-
})
151-
152-
h := csrf(func(c echo.Context) error {
153-
return c.String(http.StatusOK, "test")
154-
})
155-
156-
r := h(c)
157-
assert.NoError(t, r)
158-
assert.Regexp(t, "SameSite=None", rec.Header()["Set-Cookie"])
159-
assert.Regexp(t, "Secure", rec.Header()["Set-Cookie"])
160-
}

0 commit comments

Comments
 (0)