| 1 |
package csrf |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
// Tests that options functions are applied to the middleware. |
| 10 |
func TestOptions(t *testing.T) { |
| 11 |
var h http.Handler |
| 12 |
|
| 13 |
age := 86400 |
| 14 |
domain := "gorillatoolkit.org" |
| 15 |
path := "/forms/" |
| 16 |
header := "X-AUTH-TOKEN" |
| 17 |
field := "authenticity_token" |
| 18 |
errorHandler := unauthorizedHandler |
| 19 |
name := "_chimpanzee_csrf" |
| 20 |
|
| 21 |
testOpts := []Option{ |
| 22 |
MaxAge(age), |
| 23 |
Domain(domain), |
| 24 |
Path(path), |
| 25 |
HttpOnly(false), |
| 26 |
Secure(false), |
| 27 |
SameSite(SameSiteStrictMode), |
| 28 |
RequestHeader(header), |
| 29 |
FieldName(field), |
| 30 |
ErrorHandler(http.HandlerFunc(errorHandler)), |
| 31 |
CookieName(name), |
| 32 |
} |
| 33 |
|
| 34 |
// Parse our test options and check that they set the related struct fields. |
| 35 |
cs := parseOptions(h, testOpts...) |
| 36 |
|
| 37 |
if cs.opts.MaxAge != age { |
| 38 |
t.Errorf("MaxAge not set correctly: got %v want %v", cs.opts.MaxAge, age) |
| 39 |
} |
| 40 |
|
| 41 |
if cs.opts.Domain != domain { |
| 42 |
t.Errorf("Domain not set correctly: got %v want %v", cs.opts.Domain, domain) |
| 43 |
} |
| 44 |
|
| 45 |
if cs.opts.Path != path { |
| 46 |
t.Errorf("Path not set correctly: got %v want %v", cs.opts.Path, path) |
| 47 |
} |
| 48 |
|
| 49 |
if cs.opts.HttpOnly != false { |
| 50 |
t.Errorf("HttpOnly not set correctly: got %v want %v", cs.opts.HttpOnly, false) |
| 51 |
} |
| 52 |
|
| 53 |
if cs.opts.Secure != false { |
| 54 |
t.Errorf("Secure not set correctly: got %v want %v", cs.opts.Secure, false) |
| 55 |
} |
| 56 |
|
| 57 |
if cs.opts.SameSite != SameSiteStrictMode { |
| 58 |
t.Errorf("SameSite not set correctly: got %v want %v", cs.opts.SameSite, SameSiteStrictMode) |
| 59 |
} |
| 60 |
|
| 61 |
if cs.opts.RequestHeader != header { |
| 62 |
t.Errorf("RequestHeader not set correctly: got %v want %v", cs.opts.RequestHeader, header) |
| 63 |
} |
| 64 |
|
| 65 |
if cs.opts.FieldName != field { |
| 66 |
t.Errorf("FieldName not set correctly: got %v want %v", cs.opts.FieldName, field) |
| 67 |
} |
| 68 |
|
| 69 |
if !reflect.ValueOf(cs.opts.ErrorHandler).IsValid() { |
| 70 |
t.Errorf("ErrorHandler not set correctly: got %v want %v", |
| 71 |
reflect.ValueOf(cs.opts.ErrorHandler).IsValid(), reflect.ValueOf(errorHandler).IsValid()) |
| 72 |
} |
| 73 |
|
| 74 |
if cs.opts.CookieName != name { |
| 75 |
t.Errorf("CookieName not set correctly: got %v want %v", |
| 76 |
cs.opts.CookieName, name) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestMaxAge(t *testing.T) { |
| 81 |
t.Run("Ensure the default MaxAge is applied", func(t *testing.T) { |
| 82 |
handler := Protect(testKey)(nil) |
| 83 |
csrf := handler.(*csrf) |
| 84 |
cs := csrf.st.(*cookieStore) |
| 85 |
|
| 86 |
if cs.maxAge != defaultAge { |
| 87 |
t.Fatalf("default maxAge not applied: got %d (want %d)", cs.maxAge, defaultAge) |
| 88 |
} |
| 89 |
}) |
| 90 |
|
| 91 |
t.Run("Support an explicit MaxAge of 0 (session-only)", func(t *testing.T) { |
| 92 |
handler := Protect(testKey, MaxAge(0))(nil) |
| 93 |
csrf := handler.(*csrf) |
| 94 |
cs := csrf.st.(*cookieStore) |
| 95 |
|
| 96 |
if cs.maxAge != 0 { |
| 97 |
t.Fatalf("zero (0) maxAge not applied: got %d (want %d)", cs.maxAge, 0) |
| 98 |
} |
| 99 |
}) |
| 100 |
|
| 101 |
} |