Skip to content

Commit 3f280f8

Browse files
authored
Update HTTP status codes to modern codes (#18063)
* 2xx/3xx/4xx/5xx -> http.Status... * http.StatusFound -> http.StatusTemporaryRedirect * http.StatusMovedPermanently -> http.StatusPermanentRedirect
1 parent 395117d commit 3f280f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+211
-212
lines changed

cmd/web_acme.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {
128128
// URI always contains a leading slash, which would result in a double
129129
// slash
130130
target := strings.TrimSuffix(setting.AppURL, "/") + r.URL.RequestURI()
131-
http.Redirect(w, r, target, http.StatusFound)
131+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
132132
}

integrations/admin_user_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestAdminEditUser(t *testing.T) {
4646
}
4747

4848
func testSuccessfullEdit(t *testing.T, formData user_model.User) {
49-
makeRequest(t, formData, http.StatusFound)
49+
makeRequest(t, formData, http.StatusSeeOther)
5050
}
5151

5252
func makeRequest(t *testing.T, formData user_model.User, headerCode int) {

integrations/api_branch_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPSta
3737
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
3838
resp := session.MakeRequest(t, req, expectedHTTPStatus)
3939

40-
if resp.Code == 200 {
40+
if resp.Code == http.StatusOK {
4141
var branchProtection api.BranchProtection
4242
DecodeJSON(t, resp, &branchProtection)
4343
assert.EqualValues(t, branchName, branchProtection.BranchName)
@@ -52,7 +52,7 @@ func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTP
5252
})
5353
resp := session.MakeRequest(t, req, expectedHTTPStatus)
5454

55-
if resp.Code == 201 {
55+
if resp.Code == http.StatusCreated {
5656
var branchProtection api.BranchProtection
5757
DecodeJSON(t, resp, &branchProtection)
5858
assert.EqualValues(t, branchName, branchProtection.BranchName)
@@ -65,7 +65,7 @@ func testAPIEditBranchProtection(t *testing.T, branchName string, body *api.Bran
6565
req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/branch_protections/"+branchName+"?token="+token, body)
6666
resp := session.MakeRequest(t, req, expectedHTTPStatus)
6767

68-
if resp.Code == 200 {
68+
if resp.Code == http.StatusOK {
6969
var branchProtection api.BranchProtection
7070
DecodeJSON(t, resp, &branchProtection)
7171
assert.EqualValues(t, branchName, branchProtection.BranchName)

integrations/api_helper_for_declarative_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
227227
Title: fmt.Sprintf("create a pr from %s to %s", headBranch, baseBranch),
228228
})
229229

230-
expected := 201
230+
expected := http.StatusCreated
231231
if ctx.ExpectedCode != 0 {
232232
expected = ctx.ExpectedCode
233233
}
@@ -246,7 +246,7 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu
246246
owner, repo, index, ctx.Token)
247247
req := NewRequest(t, http.MethodGet, urlStr)
248248

249-
expected := 200
249+
expected := http.StatusOK
250250
if ctx.ExpectedCode != 0 {
251251
expected = ctx.ExpectedCode
252252
}
@@ -287,7 +287,7 @@ func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64)
287287

288288
expected := ctx.ExpectedCode
289289
if expected == 0 {
290-
expected = 200
290+
expected = http.StatusOK
291291
}
292292

293293
if !assert.EqualValues(t, expected, resp.Code,
@@ -310,7 +310,7 @@ func doAPIManuallyMergePullRequest(ctx APITestContext, owner, repo, commitID str
310310
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
311311
return
312312
}
313-
ctx.Session.MakeRequest(t, req, 200)
313+
ctx.Session.MakeRequest(t, req, http.StatusOK)
314314
}
315315
}
316316

integrations/api_pull_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestAPICreatePullSuccess(t *testing.T) {
7777
Base: "master",
7878
Title: "create a failure pr",
7979
})
80-
session.MakeRequest(t, req, 201)
80+
session.MakeRequest(t, req, http.StatusCreated)
8181
session.MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
8282
}
8383

@@ -105,7 +105,7 @@ func TestAPICreatePullWithFieldsSuccess(t *testing.T) {
105105

106106
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner10.Name, repo10.Name, token), opts)
107107

108-
res := session.MakeRequest(t, req, 201)
108+
res := session.MakeRequest(t, req, http.StatusCreated)
109109
pull := new(api.PullRequest)
110110
DecodeJSON(t, res, pull)
111111

@@ -165,20 +165,20 @@ func TestAPIEditPull(t *testing.T) {
165165
Title: "create a success pr",
166166
})
167167
pull := new(api.PullRequest)
168-
resp := session.MakeRequest(t, req, 201)
168+
resp := session.MakeRequest(t, req, http.StatusCreated)
169169
DecodeJSON(t, resp, pull)
170170
assert.EqualValues(t, "master", pull.Base.Name)
171171

172172
req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
173173
Base: "feature/1",
174174
Title: "edit a this pr",
175175
})
176-
resp = session.MakeRequest(t, req, 201)
176+
resp = session.MakeRequest(t, req, http.StatusCreated)
177177
DecodeJSON(t, resp, pull)
178178
assert.EqualValues(t, "feature/1", pull.Base.Name)
179179

180180
req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
181181
Base: "not-exist",
182182
})
183-
session.MakeRequest(t, req, 404)
183+
session.MakeRequest(t, req, http.StatusNotFound)
184184
}

integrations/api_repo_languages_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestRepoLanguages(t *testing.T) {
3333
"content": "package main",
3434
"commit_choice": "direct",
3535
})
36-
session.MakeRequest(t, req, http.StatusFound)
36+
session.MakeRequest(t, req, http.StatusSeeOther)
3737

3838
// let gitea calculate language stats
3939
time.Sleep(time.Second)

integrations/attachment_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
5959
func TestCreateAnonymousAttachment(t *testing.T) {
6060
defer prepareTestEnv(t)()
6161
session := emptyTestSession(t)
62-
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusFound)
62+
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusSeeOther)
6363
}
6464

6565
func TestCreateIssueAttachment(t *testing.T) {
@@ -83,7 +83,7 @@ func TestCreateIssueAttachment(t *testing.T) {
8383
}
8484

8585
req = NewRequestWithValues(t, "POST", link, postData)
86-
resp = session.MakeRequest(t, req, http.StatusFound)
86+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
8787
test.RedirectURL(resp) // check that redirect URL exists
8888

8989
// Validate that attachment is available

integrations/auth_ldap_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func addAuthSourceLDAP(t *testing.T, sshKeyAttribute string, groupMapParams ...s
135135
"group_team_map_removal": groupTeamMapRemoval,
136136
"user_uid": "DN",
137137
})
138-
session.MakeRequest(t, req, http.StatusFound)
138+
session.MakeRequest(t, req, http.StatusSeeOther)
139139
}
140140

141141
func TestLDAPUserSignin(t *testing.T) {
@@ -202,7 +202,7 @@ func TestLDAPAuthChange(t *testing.T) {
202202
"is_sync_enabled": "on",
203203
"is_active": "on",
204204
})
205-
session.MakeRequest(t, req, http.StatusFound)
205+
session.MakeRequest(t, req, http.StatusSeeOther)
206206

207207
req = NewRequest(t, "GET", href)
208208
resp = session.MakeRequest(t, req, http.StatusOK)

integrations/change_default_branch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestChangeDefaultBranch(t *testing.T) {
2828
"action": "default_branch",
2929
"branch": "DefaultBranch",
3030
})
31-
session.MakeRequest(t, req, http.StatusFound)
31+
session.MakeRequest(t, req, http.StatusSeeOther)
3232

3333
csrf = GetCSRF(t, session, branchesURL)
3434
req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{

integrations/create_no_session_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestSessionFileCreation(t *testing.T) {
110110
"user_name": "user2",
111111
"password": userPassword,
112112
})
113-
resp = MakeRequest(t, req, http.StatusFound)
113+
resp = MakeRequest(t, req, http.StatusSeeOther)
114114
sessionID = getSessionID(t, resp)
115115

116116
assert.FileExists(t, sessionFile(tmpDir, sessionID))

integrations/delete_user_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestUserDeleteAccount(t *testing.T) {
3636
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
3737
"_csrf": csrf,
3838
})
39-
session.MakeRequest(t, req, http.StatusFound)
39+
session.MakeRequest(t, req, http.StatusSeeOther)
4040

4141
assertUserDeleted(t, 8)
4242
unittest.CheckConsistencyFor(t, &user_model.User{})
@@ -51,7 +51,7 @@ func TestUserDeleteAccountStillOwnRepos(t *testing.T) {
5151
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
5252
"_csrf": csrf,
5353
})
54-
session.MakeRequest(t, req, http.StatusFound)
54+
session.MakeRequest(t, req, http.StatusSeeOther)
5555

5656
// user should not have been deleted, because the user still owns repos
5757
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

integrations/editor_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreateFile(t *testing.T) {
3434
"content": "Content",
3535
"commit_choice": "direct",
3636
})
37-
session.MakeRequest(t, req, http.StatusFound)
37+
session.MakeRequest(t, req, http.StatusSeeOther)
3838
})
3939
}
4040

@@ -48,7 +48,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
4848
"_csrf": csrf,
4949
"protected": "on",
5050
})
51-
session.MakeRequest(t, req, http.StatusFound)
51+
session.MakeRequest(t, req, http.StatusSeeOther)
5252
// Check if master branch has been locked successfully
5353
flashCookie := session.GetCookie("macaron_flash")
5454
assert.NotNil(t, flashCookie)
@@ -82,7 +82,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
8282
"_csrf": csrf,
8383
"protected": "off",
8484
})
85-
resp = session.MakeRequest(t, req, http.StatusFound)
85+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
8686
// Check if master branch has been locked successfully
8787
flashCookie = session.GetCookie("macaron_flash")
8888
assert.NotNil(t, flashCookie)
@@ -109,7 +109,7 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
109109
"commit_choice": "direct",
110110
},
111111
)
112-
resp = session.MakeRequest(t, req, http.StatusFound)
112+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
113113

114114
// Verify the change
115115
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
@@ -139,7 +139,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
139139
"new_branch_name": targetBranch,
140140
},
141141
)
142-
resp = session.MakeRequest(t, req, http.StatusFound)
142+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
143143

144144
// Verify the change
145145
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))

integrations/git_smart_http_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ func testGitSmartHTTP(t *testing.T, u *url.URL) {
2424
}{
2525
{
2626
p: "user2/repo1/info/refs",
27-
code: 200,
27+
code: http.StatusOK,
2828
},
2929
{
3030
p: "user2/repo1/HEAD",
31-
code: 200,
31+
code: http.StatusOK,
3232
},
3333
{
3434
p: "user2/repo1/objects/info/alternates",
35-
code: 404,
35+
code: http.StatusNotFound,
3636
},
3737
{
3838
p: "user2/repo1/objects/info/http-alternates",
39-
code: 404,
39+
code: http.StatusNotFound,
4040
},
4141
{
4242
p: "user2/repo1/../../custom/conf/app.ini",
43-
code: 404,
43+
code: http.StatusNotFound,
4444
},
4545
{
4646
p: "user2/repo1/objects/info/../../../../custom/conf/app.ini",
47-
code: 404,
47+
code: http.StatusNotFound,
4848
},
4949
{
5050
p: `user2/repo1/objects/info/..\..\..\..\custom\conf\app.ini`,
51-
code: 400,
51+
code: http.StatusBadRequest,
5252
},
5353
}
5454

integrations/git_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelist, unprotectedFil
435435
"protected": "on",
436436
"unprotected_file_patterns": unprotectedFilePatterns,
437437
})
438-
ctx.Session.MakeRequest(t, req, http.StatusFound)
438+
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
439439
} else {
440440
user, err := user_model.GetUserByName(userToWhitelist)
441441
assert.NoError(t, err)
@@ -448,7 +448,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelist, unprotectedFil
448448
"whitelist_users": strconv.FormatInt(user.ID, 10),
449449
"unprotected_file_patterns": unprotectedFilePatterns,
450450
})
451-
ctx.Session.MakeRequest(t, req, http.StatusFound)
451+
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
452452
}
453453
// Check if master branch has been locked successfully
454454
flashCookie := ctx.Session.GetCookie("macaron_flash")

integrations/integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
380380
"user_name": userName,
381381
"password": password,
382382
})
383-
resp = MakeRequest(t, req, http.StatusFound)
383+
resp = MakeRequest(t, req, http.StatusSeeOther)
384384

385385
ch := http.Header{}
386386
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
@@ -408,7 +408,7 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
408408
"_csrf": doc.GetCSRF(),
409409
"name": fmt.Sprintf("api-testing-token-%d", tokenCounter),
410410
})
411-
resp = session.MakeRequest(t, req, http.StatusFound)
411+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
412412
req = NewRequest(t, "GET", "/user/settings/applications")
413413
resp = session.MakeRequest(t, req, http.StatusOK)
414414
htmlDoc := NewHTMLParser(t, resp.Body)

integrations/issue_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
132132
"title": title,
133133
"content": content,
134134
})
135-
resp = session.MakeRequest(t, req, http.StatusFound)
135+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
136136

137137
issueURL := test.RedirectURL(resp)
138138
req = NewRequest(t, "GET", issueURL)
@@ -162,7 +162,7 @@ func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content,
162162
"content": content,
163163
"status": status,
164164
})
165-
resp = session.MakeRequest(t, req, http.StatusFound)
165+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
166166

167167
req = NewRequest(t, "GET", test.RedirectURL(resp))
168168
resp = session.MakeRequest(t, req, http.StatusOK)
@@ -334,16 +334,16 @@ func TestIssueRedirect(t *testing.T) {
334334

335335
// Test external tracker where style not set (shall default numeric)
336336
req := NewRequest(t, "GET", path.Join("org26", "repo_external_tracker", "issues", "1"))
337-
resp := session.MakeRequest(t, req, http.StatusFound)
337+
resp := session.MakeRequest(t, req, http.StatusSeeOther)
338338
assert.Equal(t, "https://tracker.com/org26/repo_external_tracker/issues/1", test.RedirectURL(resp))
339339

340340
// Test external tracker with numeric style
341341
req = NewRequest(t, "GET", path.Join("org26", "repo_external_tracker_numeric", "issues", "1"))
342-
resp = session.MakeRequest(t, req, http.StatusFound)
342+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
343343
assert.Equal(t, "https://tracker.com/org26/repo_external_tracker_numeric/issues/1", test.RedirectURL(resp))
344344

345345
// Test external tracker with alphanumeric style (for a pull request)
346346
req = NewRequest(t, "GET", path.Join("org26", "repo_external_tracker_alpha", "issues", "1"))
347-
resp = session.MakeRequest(t, req, http.StatusFound)
347+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
348348
assert.Equal(t, "/"+path.Join("org26", "repo_external_tracker_alpha", "pulls", "1"), test.RedirectURL(resp))
349349
}

integrations/links_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestRedirectsNoLogin(t *testing.T) {
5959
}
6060
for link, redirectLink := range redirects {
6161
req := NewRequest(t, "GET", link)
62-
resp := MakeRequest(t, req, http.StatusFound)
62+
resp := MakeRequest(t, req, http.StatusSeeOther)
6363
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
6464
}
6565
}

integrations/mirror_push_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func doCreatePushMirror(ctx APITestContext, address, username, password string)
8989
"push_mirror_password": password,
9090
"push_mirror_interval": "0",
9191
})
92-
ctx.Session.MakeRequest(t, req, http.StatusFound)
92+
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
9393

9494
flashCookie := ctx.Session.GetCookie("macaron_flash")
9595
assert.NotNil(t, flashCookie)
@@ -110,7 +110,7 @@ func doRemovePushMirror(ctx APITestContext, address, username, password string,
110110
"push_mirror_password": password,
111111
"push_mirror_interval": "0",
112112
})
113-
ctx.Session.MakeRequest(t, req, http.StatusFound)
113+
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
114114

115115
flashCookie := ctx.Session.GetCookie("macaron_flash")
116116
assert.NotNil(t, flashCookie)

0 commit comments

Comments
 (0)