Skip to content

Commit 0cc445b

Browse files
committed
Added linter specific testdata file
1 parent e23ac00 commit 0cc445b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/testdata/responsewriterlint.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//golangcitest:args -Eresponsewriterlint
2+
package testdata
3+
4+
import (
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
type notAResponseWriter struct{}
11+
12+
func (narw notAResponseWriter) Write(in []byte) (int, error) {
13+
// actually do nothing
14+
return 42, errors.New("no")
15+
}
16+
17+
func (narw notAResponseWriter) WriteHeader(code int) {
18+
// also do nothing
19+
}
20+
21+
func (narw notAResponseWriter) Header() http.Header {
22+
return http.Header{}
23+
}
24+
25+
type rwlRandom struct{}
26+
27+
func rwlExampleOne(w http.ResponseWriter, r *http.Request) {
28+
w.Header().Add("some header", "value")
29+
w.WriteHeader(http.StatusOK)
30+
_, _ = w.Write([]byte(`boys in the yard`))
31+
}
32+
33+
func rwlExampleTwo(s string, r *http.Request) error {
34+
fmt.Printf("this is a thing")
35+
36+
return nil
37+
}
38+
39+
func rwlFakeWriter(w notAResponseWriter) {
40+
w.Header().Add("something", "other")
41+
w.WriteHeader(420)
42+
_, _ = w.Write([]byte(`fooled ya`))
43+
}
44+
45+
func (b rwlRandom) method(w http.ResponseWriter, r *http.Request) {
46+
w.Header().Add("some header", "value")
47+
w.WriteHeader(http.StatusOK)
48+
_, _ = w.Write([]byte(`boys in the yard`))
49+
}
50+
51+
func (b *rwlRandom) methodPointer(w http.ResponseWriter, r *http.Request) {
52+
w.Header().Add("some header", "value")
53+
_, _ = w.Write([]byte(`boys in the yard`))
54+
w.WriteHeader(http.StatusOK) // want "function methodPointer: http.ResponseWriter.Write is called before http.ResponseWriter.WriteHeader. Headers are already sent, this has no effect."
55+
}
56+
57+
func rwlExampleThree(bloe http.ResponseWriter, r *http.Request) {
58+
_, _ = bloe.Write([]byte(`hellyea`)) // want "function rwlExampleThree: Multiple calls to http.ResponseWriter.Write in the same function body. This is most probably a bug."
59+
60+
bloe.WriteHeader(http.StatusBadRequest) // want "function rwlExampleThree: Multiple calls to http.ResponseWriter.WriteHeader in the same function body. This is most probably a bug."
61+
_, _ = bloe.Write([]byte(`hellyelamdflmda`)) // want "function rwlExampleThree: Multiple calls to http.ResponseWriter.Write in the same function body. This is most probably a bug."
62+
bloe.WriteHeader(http.StatusInternalServerError) // want "function rwlExampleThree: Multiple calls to http.ResponseWriter.WriteHeader in the same function body. This is most probably a bug."
63+
64+
bloe.Header().Set("help", "somebody") // want "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.Write. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.Write. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.WriteHeader. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.WriteHeader. This has no effect."
65+
bloe.Header().Set("dddd", "someboddaady") // want "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.Write. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.Write. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.WriteHeader. This has no effect." "function rwlExampleThree: http.ResponseWriter.Header called after calling http.ResponseWriter.WriteHeader. This has no effect."
66+
}

0 commit comments

Comments
 (0)