Skip to content

Improve integration test helper functions #2049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions integrations/api_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package integrations

import (
"fmt"
"net/http"
"testing"

Expand All @@ -25,9 +24,8 @@ func TestAPIListComments(t *testing.T) {
repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

session := loginUser(t, repoOwner.Name)
requestUrl := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments",
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
repoOwner.Name, repo.Name, issue.Index)
req := NewRequest(t, "GET", requestUrl)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

Expand Down
4 changes: 1 addition & 3 deletions integrations/api_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package integrations

import (
"fmt"
"net/http"
"testing"

Expand All @@ -22,8 +21,7 @@ func TestAPITeam(t *testing.T) {
user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)

session := loginUser(t, user.Name)
url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID)
req := NewRequest(t, "GET", url)
req := NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

Expand Down
2 changes: 0 additions & 2 deletions integrations/change_default_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestChangeDefaultBranch(t *testing.T) {
"action": "default_branch",
"branch": "DefaultBranch",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand All @@ -46,7 +45,6 @@ func TestChangeDefaultBranch(t *testing.T) {
"action": "default_branch",
"branch": "does_not_exist",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
}
1 change: 0 additions & 1 deletion integrations/delete_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestDeleteUser(t *testing.T) {
req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
"_csrf": doc.GetCSRF(),
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

Expand Down
5 changes: 0 additions & 5 deletions integrations/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func TestCreateFile(t *testing.T) {
"content": "Content",
"commit_choice": "direct",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
}
Expand All @@ -57,7 +56,6 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
"branchName": "master",
"canPush": "true",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
// Check if master branch has been locked successfully
Expand All @@ -83,7 +81,6 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
"commit_choice": "direct",
})

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
// Check body for error message
Expand Down Expand Up @@ -113,7 +110,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
"commit_choice": "direct",
},
)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down Expand Up @@ -150,7 +146,6 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
"new_branch_name": targetBranch,
},
)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down
13 changes: 10 additions & 3 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
"user_name": userName,
"password": password,
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = MakeRequest(req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down Expand Up @@ -218,18 +217,26 @@ func NewRequest(t testing.TB, method, urlStr string) *http.Request {
return NewRequestWithBody(t, method, urlStr, nil)
}

func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
}

func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
urlValues := url.Values{}
for key, value := range values {
urlValues[key] = []string{value}
}
return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
req := NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return req
}

func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
jsonBytes, err := json.Marshal(v)
assert.NoError(t, err)
return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
req.Header.Add("Content-Type", "application/json")
return req
}

func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
Expand Down
1 change: 0 additions & 1 deletion integrations/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title string)
"_csrf": htmlDoc.GetCSRF(),
"title": title,
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
redirectedURL := resp.Headers["Location"]
Expand Down
1 change: 0 additions & 1 deletion integrations/pull_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
"_csrf": htmlDoc.GetCSRF(),
"title": "This is a pull title",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down
2 changes: 0 additions & 2 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin
req = NewRequestWithValues(t, "POST", link, map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand All @@ -44,7 +43,6 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
req = NewRequestWithValues(t, "POST", link, map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

Expand Down
1 change: 0 additions & 1 deletion integrations/repo_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
},
)

req.Header.Add("Content-Type", "application/json")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)

Expand Down
1 change: 0 additions & 1 deletion integrations/repo_fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
"uid": "1",
"repo_name": "repo1",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down
1 change: 0 additions & 1 deletion integrations/repo_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func testRepoMigrate(t testing.TB, session *TestSession, cloneAddr, repoName str
"repo_name": repoName,
},
)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down
1 change: 0 additions & 1 deletion integrations/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestSignup(t *testing.T) {
"password": "examplePassword",
"retype": "examplePassword",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

Expand Down