Skip to content

Commit f8972dd

Browse files
committed
enable usestdlibvars
fix ctx.HTML manually
1 parent 689493e commit f8972dd

32 files changed

+56
-52
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ linters:
2222
- unconvert
2323
- unparam
2424
- unused
25+
- usestdlibvars
2526
- usetesting
2627
- wastedassign
2728
settings:

cmd/web_acme.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func runACME(listenAddr string, m http.Handler) error {
136136
}
137137

138138
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {
139-
if r.Method != "GET" && r.Method != "HEAD" {
139+
if r.Method != http.MethodGet && r.Method != http.MethodHead {
140140
http.Error(w, "Use HTTPS", http.StatusBadRequest)
141141
return
142142
}

modules/httplib/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ func (r *Request) getResponse() (*http.Response, error) {
143143
paramBody = paramBody[0 : len(paramBody)-1]
144144
}
145145

146-
if r.req.Method == "GET" && len(paramBody) > 0 {
146+
if r.req.Method == http.MethodGet && len(paramBody) > 0 {
147147
if strings.Contains(r.url, "?") {
148148
r.url += "&" + paramBody
149149
} else {
150150
r.url = r.url + "?" + paramBody
151151
}
152-
} else if r.req.Method == "POST" && r.req.Body == nil && len(paramBody) > 0 {
152+
} else if r.req.Method == http.MethodPost && r.req.Body == nil && len(paramBody) > 0 {
153153
r.Header("Content-Type", "application/x-www-form-urlencoded")
154154
r.Body(paramBody) // string
155155
}

modules/updatechecker/update_checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func GiteaUpdateChecker(httpEndpoint string) error {
3434
},
3535
}
3636

37-
req, err := http.NewRequest("GET", httpEndpoint, nil)
37+
req, err := http.NewRequest(http.MethodGet, httpEndpoint, nil)
3838
if err != nil {
3939
return err
4040
}

modules/validation/binding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func performValidationTest(t *testing.T, testCase validationTestCase) {
4747
assert.Equal(t, testCase.expectedErrors, actual)
4848
})
4949

50-
req, err := http.NewRequest("POST", testRoute, nil)
50+
req, err := http.NewRequest(http.MethodPost, testRoute, nil)
5151
if err != nil {
5252
panic(err)
5353
}

modules/web/routemock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestRouteMock(t *testing.T) {
3030

3131
// normal request
3232
recorder := httptest.NewRecorder()
33-
req, err := http.NewRequest("GET", "http://localhost:8000/foo", nil)
33+
req, err := http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
3434
assert.NoError(t, err)
3535
r.ServeHTTP(recorder, req)
3636
assert.Len(t, recorder.Header(), 3)
@@ -45,7 +45,7 @@ func TestRouteMock(t *testing.T) {
4545
resp.WriteHeader(http.StatusOK)
4646
})
4747
recorder = httptest.NewRecorder()
48-
req, err = http.NewRequest("GET", "http://localhost:8000/foo", nil)
48+
req, err = http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
4949
assert.NoError(t, err)
5050
r.ServeHTTP(recorder, req)
5151
assert.Len(t, recorder.Header(), 2)
@@ -59,7 +59,7 @@ func TestRouteMock(t *testing.T) {
5959
resp.WriteHeader(http.StatusOK)
6060
})
6161
recorder = httptest.NewRecorder()
62-
req, err = http.NewRequest("GET", "http://localhost:8000/foo", nil)
62+
req, err = http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
6363
assert.NoError(t, err)
6464
r.ServeHTTP(recorder, req)
6565
assert.Len(t, recorder.Header(), 3)

modules/web/router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestRouteNormalizePath(t *testing.T) {
224224
actualPaths.Path = req.URL.Path
225225
})
226226

227-
req, err := http.NewRequest("GET", reqPath, nil)
227+
req, err := http.NewRequest(http.MethodGet, reqPath, nil)
228228
assert.NoError(t, err)
229229
r.ServeHTTP(recorder, req)
230230
assert.Equal(t, expectedPaths, actualPaths, "req path = %q", reqPath)

routers/api/packages/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ func CommonRoutes() *web.Router {
552552

553553
r.Methods("HEAD,GET,PUT,DELETE", "*", func(ctx *context.Context) {
554554
path := ctx.PathParam("*")
555-
isHead := ctx.Req.Method == "HEAD"
556-
isGetHead := ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET"
557-
isPut := ctx.Req.Method == "PUT"
558-
isDelete := ctx.Req.Method == "DELETE"
555+
isHead := ctx.Req.Method == http.MethodHead
556+
isGetHead := ctx.Req.Method == http.MethodHead || ctx.Req.Method == http.MethodGet
557+
isPut := ctx.Req.Method == http.MethodPut
558+
isDelete := ctx.Req.Method == http.MethodDelete
559559

560560
m := repoPattern.FindStringSubmatch(path)
561561
if len(m) == 2 && isGetHead {

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func tokenRequiresScopes(requiredScopeCategories ...auth_model.AccessTokenScopeC
307307

308308
// use the http method to determine the access level
309309
requiredScopeLevel := auth_model.Read
310-
if ctx.Req.Method == "POST" || ctx.Req.Method == "PUT" || ctx.Req.Method == "PATCH" || ctx.Req.Method == "DELETE" {
310+
if ctx.Req.Method == http.MethodPost || ctx.Req.Method == http.MethodPut || ctx.Req.Method == http.MethodPatch || ctx.Req.Method == http.MethodDelete {
311311
requiredScopeLevel = auth_model.Write
312312
}
313313

routers/install/routes_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package install
55

66
import (
7+
"net/http"
78
"net/http/httptest"
89
"testing"
910

@@ -17,18 +18,18 @@ func TestRoutes(t *testing.T) {
1718
assert.NotNil(t, r)
1819

1920
w := httptest.NewRecorder()
20-
req := httptest.NewRequest("GET", "/", nil)
21+
req := httptest.NewRequest(http.MethodGet, "/", nil)
2122
r.ServeHTTP(w, req)
2223
assert.Equal(t, 200, w.Code)
2324
assert.Contains(t, w.Body.String(), `class="page-content install"`)
2425

2526
w = httptest.NewRecorder()
26-
req = httptest.NewRequest("GET", "/no-such", nil)
27+
req = httptest.NewRequest(http.MethodGet, "/no-such", nil)
2728
r.ServeHTTP(w, req)
2829
assert.Equal(t, 404, w.Code)
2930

3031
w = httptest.NewRecorder()
31-
req = httptest.NewRequest("GET", "/assets/img/gitea.svg", nil)
32+
req = httptest.NewRequest(http.MethodGet, "/assets/img/gitea.svg", nil)
3233
r.ServeHTTP(w, req)
3334
assert.Equal(t, 200, w.Code)
3435
}

0 commit comments

Comments
 (0)