Skip to content

Commit

Permalink
Merge pull request #71 from sashamelentyev/feat/responserec
Browse files Browse the repository at this point in the history
feat: add check status code in httptest.ResponseRecorder
  • Loading branch information
sashamelentyev authored Sep 3, 2022
2 parents 1e41542 + c5e07fe commit 57bc8c0
Show file tree
Hide file tree
Showing 3 changed files with 780 additions and 3 deletions.
13 changes: 12 additions & 1 deletion pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ func funArgs(pass *analysis.Pass, x *ast.Ident, fun *ast.SelectorExpr, args []as

// typeElts checks elements of type.
func typeElts(pass *analysis.Pass, x *ast.Ident, typ *ast.SelectorExpr, elts []ast.Expr) {
if x.Name == "http" {
switch x.Name {
case "http":
switch typ.Sel.Name {
// http.Request{Method: http.MethodGet}
case "Request":
Expand All @@ -268,6 +269,16 @@ func typeElts(pass *analysis.Pass, x *ast.Ident, typ *ast.SelectorExpr, elts []a
checkHTTPStatusCode(pass, basicLit)
}
}
case "httptest":
if typ.Sel.Name == "ResponseRecorder" {
if !lookupFlag(pass, HTTPStatusCodeFlag) {
return
}

if basicLit := getBasicLitFromElts(elts, "Code"); basicLit != nil {
checkHTTPStatusCode(pass, basicLit)
}
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion pkg/analyzer/internal/template/test-httpstatuscode.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 @@ -186,3 +189,19 @@ func _() {
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
_ = httptest.ResponseRecorder{
Code: {{ $key }}, // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
_ = httptest.ResponseRecorder{
Code: {{ $value }},
}
}
{{ end -}}
Loading

0 comments on commit 57bc8c0

Please sign in to comment.