Support CSRF on route test POST requests.
This commit is contained in:
parent
e90434edd5
commit
5c64cd6191
1 changed files with 20 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -51,8 +52,14 @@ type httpRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func request(t *testing.T) *httpRequest {
|
func request(t *testing.T) *httpRequest {
|
||||||
|
jar, err := cookiejar.New(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
r := httpRequest{
|
r := httpRequest{
|
||||||
t: t,
|
t: t,
|
||||||
|
body: url.Values{},
|
||||||
|
client: http.Client{
|
||||||
|
Jar: jar,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return &r
|
return &r
|
||||||
}
|
}
|
||||||
|
|
@ -83,6 +90,18 @@ func (h *httpRequest) get() *httpResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpRequest) post() *httpResponse {
|
func (h *httpRequest) post() *httpResponse {
|
||||||
|
// Make a get request to get the CSRF token
|
||||||
|
doc := h.get().
|
||||||
|
assertStatusCode(http.StatusOK).
|
||||||
|
toDoc()
|
||||||
|
|
||||||
|
// Extract the CSRF and include it in the POST request body
|
||||||
|
csrf := doc.Find(`input[name="csrf"]`).First()
|
||||||
|
token, exists := csrf.Attr("value")
|
||||||
|
assert.True(h.t, exists)
|
||||||
|
h.body["csrf"] = []string{token}
|
||||||
|
|
||||||
|
// Make the POST requests
|
||||||
resp, err := h.client.PostForm(h.route, h.body)
|
resp, err := h.client.PostForm(h.route, h.body)
|
||||||
require.NoError(h.t, err)
|
require.NoError(h.t, err)
|
||||||
r := httpResponse{
|
r := httpResponse{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue