Easier get request controller testing.
This commit is contained in:
parent
e499257686
commit
a5d8d2ea35
2 changed files with 28 additions and 13 deletions
|
|
@ -8,9 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAbout_Get(t *testing.T) {
|
func TestAbout_Get(t *testing.T) {
|
||||||
resp := GetRequest(t, "about")
|
doc := getRequest(t, "about").
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
assertStatusCode(http.StatusOK).
|
||||||
doc := GetGoqueryDoc(t, resp)
|
toDoc()
|
||||||
|
|
||||||
h1 := doc.Find("h1.title")
|
h1 := doc.Find("h1.title")
|
||||||
assert.Len(t, h1.Nodes, 1)
|
assert.Len(t, h1.Nodes, 1)
|
||||||
assert.Equal(t, "About", h1.Text())
|
assert.Equal(t, "About", h1.Text())
|
||||||
|
|
|
||||||
|
|
@ -32,17 +32,31 @@ func TestMain(m *testing.M) {
|
||||||
os.Exit(exitVal)
|
os.Exit(exitVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRequest(t *testing.T, route string, routeParams ...interface{}) *http.Response {
|
type httpResponse struct {
|
||||||
|
*http.Response
|
||||||
|
t *testing.T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *httpResponse) assertStatusCode(code int) *httpResponse {
|
||||||
|
assert.Equal(h.t, code, h.Response)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *httpResponse) toDoc() *goquery.Document {
|
||||||
|
doc, err := goquery.NewDocumentFromReader(h.Body)
|
||||||
|
require.NoError(h.t, err)
|
||||||
|
err = h.Body.Close()
|
||||||
|
assert.NoError(h.t, err)
|
||||||
|
return doc
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRequest(t *testing.T, route string, routeParams ...interface{}) *httpResponse {
|
||||||
cli := http.Client{}
|
cli := http.Client{}
|
||||||
resp, err := cli.Get(srv.URL + c.Web.Reverse(route, routeParams))
|
resp, err := cli.Get(srv.URL + c.Web.Reverse(route, routeParams))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return resp
|
h := httpResponse{
|
||||||
}
|
t: t,
|
||||||
|
Response: resp,
|
||||||
func GetGoqueryDoc(t *testing.T, resp *http.Response) *goquery.Document {
|
}
|
||||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
return &h
|
||||||
require.NoError(t, err)
|
|
||||||
err = resp.Body.Close()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
return doc
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue