@@ -6,31 +6,43 @@ import (
66 "net/http"
77)
88
9- type MockRoundTripper func (r * http.Request ) * http.Response
10-
11- func InjectHTTPClient (httpClient * http.Client ) {
12- Client = httpClient
13- }
9+ type MockRoundTripper func (r * http.Request ) (* http.Response , error )
1410
1511func (f MockRoundTripper ) RoundTrip (r * http.Request ) (* http.Response , error ) {
16- return f (r ), nil
12+ return f (r )
1713}
1814
19- func MockResponse (statusCode int , body string ) {
20- MockResponseWithRequestInspect (statusCode , body , nil )
15+ func MockResponse (statusCode int , body string , requestInspect func (r * http.Request )) {
16+ Client = & http.Client {
17+ Transport : MockRoundTripper (func (r * http.Request ) (* http.Response , error ) {
18+ if requestInspect != nil {
19+ requestInspect (r )
20+ }
21+
22+ return & http.Response {
23+ StatusCode : statusCode ,
24+ Body : io .NopCloser (bytes .NewReader ([]byte (body ))),
25+ ContentLength : int64 (len ([]byte (body ))),
26+ Request : r ,
27+ }, nil
28+ }),
29+ }
2130}
2231
23- func MockResponseWithRequestInspect (statusCode int , body string , requestInspect func (r * http.Request )) {
24- InjectHTTPClient ( & http.Client {
25- Transport : MockRoundTripper (func (r * http.Request ) * http.Response {
32+ func MockResponseFunc (statusCode int , bodyFn func ( string ) string , requestInspect func (r * http.Request )) {
33+ Client = & http.Client {
34+ Transport : MockRoundTripper (func (r * http.Request ) ( * http.Response , error ) {
2635 if requestInspect != nil {
2736 requestInspect (r )
2837 }
2938
39+ body := bodyFn (r .URL .String ())
3040 return & http.Response {
31- StatusCode : statusCode ,
32- Body : io .NopCloser (bytes .NewReader ([]byte (body ))),
33- }
41+ StatusCode : statusCode ,
42+ Body : io .NopCloser (bytes .NewReader ([]byte (body ))),
43+ ContentLength : int64 (len ([]byte (body ))),
44+ Request : r ,
45+ }, nil
3446 }),
35- })
47+ }
3648}
0 commit comments