Skip to content

Commit 57eaa74

Browse files
committed
New test added for CSRF protection check
1 parent 17bb8fb commit 57eaa74

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

handlers_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,24 @@ func TestCreateNoteHandler(t *testing.T) {
112112
t.Errorf("return code %d instead %d", w.Code, http.StatusCreated)
113113
}
114114
}
115+
116+
// Checks cross site request forgery (CSRF) protection works.
117+
func TestCreateNoteForbidden(t *testing.T) {
118+
srv := stubServer()
119+
w := requestMainForm(srv)
120+
121+
data := url.Values{}
122+
cookies := w.Result().Cookies()
123+
data.Set("body", TestRandomString)
124+
data.Set("csrf_token", "WRONG_TOKEN")
125+
126+
w = httptest.NewRecorder()
127+
req, _ := http.NewRequest("POST", "/note", bytes.NewBufferString(data.Encode()))
128+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
129+
req.AddCookie(cookies[0])
130+
srv.router.ServeHTTP(w, req)
131+
132+
if w.Code != http.StatusForbidden {
133+
t.Errorf("return code %d instead %d", w.Code, http.StatusForbidden)
134+
}
135+
}

0 commit comments

Comments
 (0)