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