diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 67755e5..f4f1c8a 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -220,6 +220,16 @@ func funArgs(pass *analysis.Pass, x *ast.Ident, fun *ast.SelectorExpr, args []as checkHTTPStatusCode(pass, basicLit) } } + case "httptest": + if fun.Sel.Name == "NewRequest" { + if !lookupFlag(pass, HTTPMethodFlag) { + return + } + + if basicLit := getBasicLitFromArgs(args, 3, 0, token.STRING); basicLit != nil { + checkHTTPMethod(pass, basicLit) + } + } default: // w.WriteHeader(http.StatusOk) if fun.Sel.Name == "WriteHeader" { diff --git a/pkg/analyzer/internal/template/test-httpmethod.go.tmpl b/pkg/analyzer/internal/template/test-httpmethod.go.tmpl index cce6b13..36d2efe 100644 --- a/pkg/analyzer/internal/template/test-httpmethod.go.tmpl +++ b/pkg/analyzer/internal/template/test-httpmethod.go.tmpl @@ -2,7 +2,10 @@ package {{ .PackageName }} -import "net/http" +import ( + "net/http" + "net/http/httptest" +) var ( {{- range $key, $value := .Mapping }} @@ -103,3 +106,9 @@ func _() { } } {{ end -}} + +func _() { +{{- range $key, $value := .Mapping }} + _ = httptest.NewRequest("{{ $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` +{{- end }} +} diff --git a/pkg/analyzer/testdata/src/a/http/method.go b/pkg/analyzer/testdata/src/a/http/method.go index 5b21984..da1ceb3 100755 --- a/pkg/analyzer/testdata/src/a/http/method.go +++ b/pkg/analyzer/testdata/src/a/http/method.go @@ -2,7 +2,10 @@ package http_test -import "net/http" +import ( + "net/http" + "net/http/httptest" +) var ( _ = "CONNECT" @@ -585,3 +588,14 @@ func _() { return } } +func _() { + _ = httptest.NewRequest("CONNECT", "", http.NoBody) // want `"CONNECT" can be replaced by http\.MethodConnect` + _ = httptest.NewRequest("DELETE", "", http.NoBody) // want `"DELETE" can be replaced by http\.MethodDelete` + _ = httptest.NewRequest("GET", "", http.NoBody) // want `"GET" can be replaced by http\.MethodGet` + _ = httptest.NewRequest("HEAD", "", http.NoBody) // want `"HEAD" can be replaced by http\.MethodHead` + _ = httptest.NewRequest("OPTIONS", "", http.NoBody) // want `"OPTIONS" can be replaced by http\.MethodOptions` + _ = httptest.NewRequest("PATCH", "", http.NoBody) // want `"PATCH" can be replaced by http\.MethodPatch` + _ = httptest.NewRequest("POST", "", http.NoBody) // want `"POST" can be replaced by http\.MethodPost` + _ = httptest.NewRequest("PUT", "", http.NoBody) // want `"PUT" can be replaced by http\.MethodPut` + _ = httptest.NewRequest("TRACE", "", http.NoBody) // want `"TRACE" can be replaced by http\.MethodTrace` +}