@@ -20,7 +20,6 @@ import (
2020 "bytes"
2121 "fmt"
2222 "net/http"
23- "os"
2423 "regexp"
2524 "strconv"
2625 "strings"
@@ -31,19 +30,19 @@ import (
3130 "github.com/hashicorp/go-cleanhttp"
3231)
3332
34- func ProcessRequests (t * testing.T , data []byte , addr string , methods ... string ) {
35- t .Helper ()
33+ func ProcessRequests (tb * testing.T , data []byte , addr string , methods ... string ) {
34+ tb .Helper ()
3635
3736 if len (methods ) == 0 {
38- t .Fatalf ("no methods specified" )
37+ tb .Fatalf ("no methods specified" )
3938 }
4039 for _ , method := range methods {
41- ProcessRequest (t , data , addr , method )
40+ ProcessRequest (tb , data , addr , method )
4241 }
4342}
4443
45- func ProcessRequest (t testing.TB , data []byte , addr , method string ) {
46- t .Helper ()
44+ func ProcessRequest (tb testing.TB , data []byte , addr , method string ) {
45+ tb .Helper ()
4746
4847 switch method {
4948 case
@@ -57,26 +56,26 @@ func ProcessRequest(t testing.TB, data []byte, addr, method string) {
5756 http .MethodOptions ,
5857 http .MethodTrace :
5958
60- req := newFuzzRequest ().Fuzz (t , data , method , addr )
59+ req := newFuzzRequest ().Fuzz (tb , data , method , addr )
6160 defer req .Body .Close ()
6261
63- resp := fuzzHTTPRequest (t , req )
62+ resp := fuzzHTTPRequest (tb , req )
6463 if resp != nil {
6564 if resp .StatusCode > 500 {
66- t .Errorf ("resp: %v" , resp )
65+ tb .Errorf ("resp: %v" , resp )
6766 }
6867 defer resp .Body .Close ()
6968 }
7069 default :
71- t .Errorf ("Unsupported HTTP method: %s" , method )
70+ tb .Errorf ("Unsupported HTTP method: %s" , method )
7271 }
7372}
7473
75- func fuzzHTTPRequest (t testing.TB , fuzzReq * http.Request ) * http.Response {
76- t .Helper ()
74+ func fuzzHTTPRequest (tb testing.TB , fuzzReq * http.Request ) * http.Response {
75+ tb .Helper ()
7776
7877 if fuzzReq == nil {
79- t .Skip ("Skipping test because fuzzReq is nil" )
78+ tb .Skip ("Skipping test because fuzzReq is nil" )
8079 }
8180 client := cleanhttp .DefaultClient ()
8281 client .Timeout = time .Second
@@ -98,11 +97,11 @@ func fuzzHTTPRequest(t testing.TB, fuzzReq *http.Request) *http.Response {
9897 return nil
9998 }
10099
101- t .Logf ("fuzzing request, %s, %s" , fuzzReq .Method , fuzzReq .URL )
100+ tb .Logf ("fuzzing request, %s, %s" , fuzzReq .Method , fuzzReq .URL )
102101
103102 resp , err := client .Do (fuzzReq )
104103 if err != nil && ! strings .Contains (err .Error (), "checkRedirect disabled for test" ) {
105- t .Logf ("err: %s" , err )
104+ tb .Logf ("err: %s" , err )
106105 }
107106
108107 return resp
@@ -114,14 +113,14 @@ func newFuzzRequest() *fuzzRequest {
114113 return & fuzzRequest {}
115114}
116115
117- func (s * fuzzRequest ) Fuzz (t testing.TB , data []byte , method , addr string ) * http.Request {
118- t .Helper ()
116+ func (s * fuzzRequest ) Fuzz (tb testing.TB , data []byte , method , addr string ) * http.Request {
117+ tb .Helper ()
119118
120119 bodyReader := bytes .NewBuffer (data )
121120
122121 req , err := http .NewRequest (method , addr , bodyReader )
123122 if err != nil {
124- t .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
123+ tb .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
125124 }
126125
127126 // Get the address of the local listener in order to attach it to an Origin header.
@@ -130,10 +129,13 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http
130129
131130 fuzzConsumer := fuzz .NewConsumer (data )
132131 var headersMap map [string ]string
133- fuzzConsumer .FuzzMap (& headersMap )
132+ err = fuzzConsumer .FuzzMap (& headersMap )
133+ if err != nil {
134+ tb .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
135+ }
134136
135137 for k , v := range headersMap {
136- for i := 0 ; i < len (v ); i ++ {
138+ for range len (v ) {
137139 req .Header .Add (k , v )
138140 }
139141 }
@@ -144,16 +146,3 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http
144146
145147 return req
146148}
147-
148- func GetPortFromEnv (env string , defaultPort int ) (port int , err error ) {
149- portEnv := os .Getenv (env )
150- if portEnv == "" {
151- return defaultPort , nil
152- }
153-
154- port , err = strconv .Atoi (portEnv )
155- if err != nil {
156- return 0 , fmt .Errorf ("failed to parse port env var %s: %w" , env , err )
157- }
158- return port , nil
159- }
0 commit comments