@@ -2,6 +2,7 @@ package middleware_test
22
33import (
44 "bytes"
5+ "context"
56 "errors"
67 "net/http"
78 "net/http/httptest"
@@ -20,6 +21,8 @@ import (
2021)
2122
2223func TestRecovery (t * testing.T ) {
24+ t .Parallel ()
25+
2326 tests := []struct {
2427 name string
2528 val interface {}
@@ -38,18 +41,20 @@ func TestRecovery(t *testing.T) {
3841 }
3942
4043 for _ , test := range tests {
41- test := test
4244 t .Run (test .name , func (t * testing.T ) {
4345 t .Parallel ()
4446
4547 buf := bytes.Buffer {}
4648 log := logger .New (& buf , logger .LogfmtFormat (), logger .Info )
4749
48- next := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
50+ next := http .HandlerFunc (func (http.ResponseWriter , * http.Request ) {
4951 panic (test .val )
5052 })
5153
52- req , _ := http .NewRequest ("GET" , "/" , nil )
54+ ctx , cancel := context .WithCancel (context .Background ())
55+ t .Cleanup (cancel )
56+
57+ req , _ := http .NewRequestWithContext (ctx , http .MethodGet , "/" , nil )
5358 resp := httptest .NewRecorder ()
5459
5560 defer func () {
@@ -66,8 +71,10 @@ func TestRecovery(t *testing.T) {
6671}
6772
6873func TestRequestID (t * testing.T ) {
74+ t .Parallel ()
75+
6976 var nextCalled bool
70- next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
77+ next := http .HandlerFunc (func (_ http.ResponseWriter , req * http.Request ) {
7178 nextCalled = true
7279
7380 got , ok := request .IDFrom (req .Context ())
@@ -76,7 +83,10 @@ func TestRequestID(t *testing.T) {
7683 assert .NotEmpty (t , got )
7784 })
7885
79- req := httptest .NewRequest (http .MethodGet , "/some/thing" , nil )
86+ ctx , cancel := context .WithCancel (context .Background ())
87+ t .Cleanup (cancel )
88+
89+ req := httptest .NewRequestWithContext (ctx , http .MethodGet , "/some/thing" , nil )
8090 rec := httptest .NewRecorder ()
8191
8292 middleware .RequestID ()(next ).ServeHTTP (rec , req )
@@ -86,6 +96,8 @@ func TestRequestID(t *testing.T) {
8696}
8797
8898func TestStats (t * testing.T ) {
99+ t .Parallel ()
100+
89101 tests := []struct {
90102 name string
91103 handlerName string
@@ -106,13 +118,12 @@ func TestStats(t *testing.T) {
106118 }
107119
108120 for _ , test := range tests {
109- test := test
110121 t .Run (test .name , func (t * testing.T ) {
111122 t .Parallel ()
112123
113124 m := & mockReporter {}
114125 m .On ("Counter" , "requests" , int64 (1 ), test .wantTags )
115- wantTags := append (test .wantTags , [][2 ]string {{"code" , "305" }, {"code-group" , "3xx" }}... )
126+ wantTags := append (test .wantTags , [][2 ]string {{"code" , "305" }, {"code-group" , "3xx" }}... ) //nolint:gocritic
116127 sort .Slice (wantTags , func (i , j int ) bool {
117128 return wantTags [i ][0 ] < wantTags [j ][0 ]
118129 })
@@ -123,12 +134,15 @@ func TestStats(t *testing.T) {
123134 s := statter .New (m , time .Second )
124135
125136 next := http .HandlerFunc (
126- func (w http.ResponseWriter , r * http.Request ) {
127- w .WriteHeader (305 )
137+ func (w http.ResponseWriter , _ * http.Request ) {
138+ w .WriteHeader (http . StatusUseProxy )
128139 })
129140
141+ ctx , cancel := context .WithCancel (context .Background ())
142+ t .Cleanup (cancel )
143+
144+ req , _ := http .NewRequestWithContext (ctx , http .MethodGet , test .path , nil )
130145 resp := httptest .NewRecorder ()
131- req , _ := http .NewRequest ("GET" , test .path , nil )
132146
133147 middleware .Stats (test .handlerName , s )(next ).ServeHTTP (resp , req )
134148
@@ -141,17 +155,22 @@ func TestStats(t *testing.T) {
141155}
142156
143157func TestWithStats_Prometheus (t * testing.T ) {
158+ t .Parallel ()
159+
144160 reporter := prometheus .New ("test" )
145161 s := statter .New (reporter , time .Second )
146162
147163 h := middleware .WithStats ("test-handler" , s , http .HandlerFunc (
148- func (w http.ResponseWriter , r * http.Request ) {
149- w .WriteHeader (305 )
164+ func (w http.ResponseWriter , _ * http.Request ) {
165+ w .WriteHeader (http . StatusUseProxy )
150166 }),
151167 )
152168
169+ ctx , cancel := context .WithCancel (context .Background ())
170+ t .Cleanup (cancel )
171+
172+ req , _ := http .NewRequestWithContext (ctx , http .MethodGet , "/foobar" , nil )
153173 resp := httptest .NewRecorder ()
154- req , _ := http .NewRequest ("GET" , "/foobar" , nil )
155174
156175 h .ServeHTTP (resp , req )
157176
0 commit comments