diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-07-07 15:36:47 -0400 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-07-07 21:36:47 +0200 |
commit | f1adaef45849fc260b07be39a39b9c1ee5146923 (patch) | |
tree | bc33151ef4021ae14adf2e6cfc694b9e4729cef8 /integrations/integration_test.go | |
parent | 5651cc7413640f12a9eb8dee64d332ea9597afce (diff) | |
download | gitea-f1adaef45849fc260b07be39a39b9c1ee5146923.tar.gz gitea-f1adaef45849fc260b07be39a39b9c1ee5146923.zip |
Less verbose integration tests (#2123)
* Helper functions for intergration test boilerplate
Diffstat (limited to 'integrations/integration_test.go')
-rw-r--r-- | integrations/integration_test.go | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 9332e12187..478eb976c8 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -140,13 +140,13 @@ func (s *TestSession) GetCookie(name string) *http.Cookie { return nil } -func (s *TestSession) MakeRequest(t testing.TB, req *http.Request) *TestResponse { +func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse { baseURL, err := url.Parse(setting.AppURL) assert.NoError(t, err) for _, c := range s.jar.Cookies(baseURL) { req.AddCookie(c) } - resp := MakeRequest(req) + resp := MakeRequest(t, req, expectedStatus) ch := http.Header{} ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";")) @@ -164,8 +164,7 @@ func loginUser(t testing.TB, userName string) *TestSession { func loginUserWithPassword(t testing.TB, userName, password string) *TestSession { req := NewRequest(t, "GET", "/user/login") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{ @@ -173,8 +172,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession "user_name": userName, "password": password, }) - resp = MakeRequest(req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = MakeRequest(t, req, http.StatusFound) ch := http.Header{} ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";")) @@ -246,13 +244,18 @@ func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *ht return request } -func MakeRequest(req *http.Request) *TestResponse { +const NoExpectedStatus = -1 + +func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse { buffer := bytes.NewBuffer(nil) respWriter := &TestResponseWriter{ Writer: buffer, Headers: make(map[string][]string), } mac.ServeHTTP(respWriter, req) + if expectedStatus != NoExpectedStatus { + assert.EqualValues(t, expectedStatus, respWriter.HeaderCode) + } return &TestResponse{ HeaderCode: respWriter.HeaderCode, Body: buffer.Bytes(), @@ -264,3 +267,16 @@ func DecodeJSON(t testing.TB, resp *TestResponse, v interface{}) { decoder := json.NewDecoder(bytes.NewBuffer(resp.Body)) assert.NoError(t, decoder.Decode(v)) } + +func GetCSRF(t testing.TB, session *TestSession, urlStr string) string { + req := NewRequest(t, "GET", urlStr) + resp := session.MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + return doc.GetCSRF() +} + +func RedirectURL(t testing.TB, resp *TestResponse) string { + urlSlice := resp.Headers["Location"] + assert.NotEmpty(t, urlSlice, "No redirect URL founds") + return urlSlice[0] +} |