Skip to content

Commit

Permalink
Merge pull request #70 from sashamelentyev/feat/httptest
Browse files Browse the repository at this point in the history
feat: add check method in httptest.NewRequest
  • Loading branch information
sashamelentyev authored Sep 3, 2022
2 parents a0264fb + d47ba32 commit 1e41542
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
11 changes: 10 additions & 1 deletion pkg/analyzer/internal/template/test-httpmethod.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package {{ .PackageName }}

import "net/http"
import (
"net/http"
"net/http/httptest"
)

var (
{{- range $key, $value := .Mapping }}
Expand Down Expand Up @@ -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 }}
}
16 changes: 15 additions & 1 deletion pkg/analyzer/testdata/src/a/http/method.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e41542

Please sign in to comment.