|
1 | 1 | package zappr |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "net/http" |
6 | 7 | "net/url" |
@@ -46,13 +47,33 @@ func newMockAndHandler() (*http.Client, *test.MockHandler, *test.Server) { |
46 | 47 | return httpClient, mockHandler, mockServer |
47 | 48 | } |
48 | 49 |
|
| 50 | +func newMockAndHandlerWithNilResponseTransport() (*http.Client, *test.MockHandler, *test.Server) { |
| 51 | + mockHandler := &test.MockHandler{} |
| 52 | + mockServer := test.NewUnstartedServer(mockHandler) |
| 53 | + |
| 54 | + httpClient := &http.Client{ |
| 55 | + Transport: &NilResponseTransport{}, |
| 56 | + } |
| 57 | + |
| 58 | + return httpClient, mockHandler, mockServer |
| 59 | +} |
| 60 | + |
49 | 61 | // NewMockAndHandler returns a Zappr Client that uses Github Token, Mockhandler, and Server. The client proxies |
50 | 62 | // requests to the server and handlers can be registered on the mux to handle |
51 | 63 | // requests. The caller must close the test server. |
52 | 64 | func NewMockAndHandler() (Client, *test.MockHandler, *test.Server) { |
53 | 65 | return NewMockAndHandlerWithGithubToken("1234567890") |
54 | 66 | } |
55 | 67 |
|
| 68 | +// NewMockAndHandlerNilResponse returns a Zappr Client that uses Github Token, Mockhandler, and Server. |
| 69 | +// The internal http client always returns a nil response object. |
| 70 | +func NewMockAndHandlerNilResponse() (Client, *test.MockHandler, *test.Server) { |
| 71 | + httpClient, mockHandler, mockServer := newMockAndHandlerWithNilResponseTransport() |
| 72 | + client := NewWithGithubToken("https://fake.zappr/", "1234567890", httpClient) |
| 73 | + |
| 74 | + return client, mockHandler, mockServer |
| 75 | +} |
| 76 | + |
56 | 77 | // NewMockAndHandlerWithGithubToken returns a Zappr Client that uses Github Token, Mockhandler, and Server. The client proxies |
57 | 78 | // requests to the server and handlers can be registered on the mux to handle |
58 | 79 | // requests. The caller must close the test server. |
@@ -88,3 +109,14 @@ func (t *RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) |
88 | 109 | } |
89 | 110 | return t.Transport.RoundTrip(req) |
90 | 111 | } |
| 112 | + |
| 113 | +// NilResponseTransport always returns a nil response object |
| 114 | +type NilResponseTransport struct { |
| 115 | + Transport http.RoundTripper |
| 116 | +} |
| 117 | + |
| 118 | +// RoundTrip rewrites the request scheme to http and calls through to the |
| 119 | +// composed RoundTripper or if it is nil, to the http.DefaultTransport. |
| 120 | +func (t *NilResponseTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 121 | + return nil, errors.New("some error") |
| 122 | +} |
0 commit comments