|
1 | 1 | package middleware
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "net/http"
|
5 | 6 | "net/http/httptest"
|
6 | 7 | "net/url"
|
@@ -117,3 +118,43 @@ func TestCSRFWithoutSameSiteMode(t *testing.T) {
|
117 | 118 | assert.NoError(t, r)
|
118 | 119 | assert.NotRegexp(t, "SameSite=", rec.Header()["Set-Cookie"])
|
119 | 120 | }
|
| 121 | + |
| 122 | +func TestCSRFWithSameSiteDefaultMode(t *testing.T) { |
| 123 | + e := echo.New() |
| 124 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 125 | + rec := httptest.NewRecorder() |
| 126 | + c := e.NewContext(req, rec) |
| 127 | + |
| 128 | + csrf := CSRFWithConfig(CSRFConfig{ |
| 129 | + CookieSameSite: http.SameSiteDefaultMode, |
| 130 | + }) |
| 131 | + |
| 132 | + h := csrf(func(c echo.Context) error { |
| 133 | + return c.String(http.StatusOK, "test") |
| 134 | + }) |
| 135 | + |
| 136 | + r := h(c) |
| 137 | + assert.NoError(t, r) |
| 138 | + fmt.Println(rec.Header()["Set-Cookie"]) |
| 139 | + assert.NotRegexp(t, "SameSite=", rec.Header()["Set-Cookie"]) |
| 140 | +} |
| 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: http.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