Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 15253cb

Browse files
committed
correctly handle cases where the returned response object is nil
1 parent 1c98b2b commit 15253cb

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

pkg/zappr/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ func (c *clientImpl) doRequest(req *http.Request) (int, *zapprErrorResponse, err
144144
zapprErrorResponse := &zapprErrorResponse{}
145145
resp, err := c.slingClient.Do(req, nil, zapprErrorResponse)
146146

147+
if resp == nil {
148+
return http.StatusInternalServerError, nil, ErrZapprServerError
149+
}
150+
147151
if resp.StatusCode == http.StatusUnauthorized {
148152
return resp.StatusCode, nil, ErrZapprUnauthorized
149153
}
@@ -153,7 +157,7 @@ func (c *clientImpl) doRequest(req *http.Request) (int, *zapprErrorResponse, err
153157
return resp.StatusCode, zapprErrorResponse, errwrap.Wrap(errors.New(zapprErrorResponse.Detail), ErrZapprServerError)
154158
}
155159

156-
return resp.StatusCode, nil, ErrZapprServerError
160+
return resp.StatusCode, nil, errwrap.Wrap(ErrZapprServerError, err)
157161
}
158162

159163
return resp.StatusCode, nil, err

pkg/zappr/client_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,23 @@ func TestDisable_RepoDeletedFromGithub_AndNotOnZappr(t *testing.T) {
275275
// Assert "already not enabled" error was returned
276276
assert.Equal(t, ErrZapprAlreadyNotEnabled, err)
277277

278-
// Assert expected calls we made to Zappr
278+
// Assert expected calls were made to Zappr
279+
zapprMock.AssertExpectations(t)
280+
}
281+
282+
func TestProblematicRequest(t *testing.T) {
283+
// Get the Zappr Client, Mock Handler and Test Server
284+
client, zapprMock, testServer := NewMockAndHandlerNilResponse()
285+
286+
// Start the test server and stop it when done
287+
testServer.Start()
288+
defer testServer.Close()
289+
290+
err := client.Enable(1)
291+
292+
// Assert "unknown zappr" error was returned
293+
assert.True(t, errwrap.Contains(err, ErrZapprServerError.Error()))
294+
295+
// Assert expected calls were made to Zappr
279296
zapprMock.AssertExpectations(t)
280297
}

pkg/zappr/zappr_mock.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zappr
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"net/url"
@@ -46,13 +47,33 @@ func newMockAndHandler() (*http.Client, *test.MockHandler, *test.Server) {
4647
return httpClient, mockHandler, mockServer
4748
}
4849

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+
4961
// NewMockAndHandler returns a Zappr Client that uses Github Token, Mockhandler, and Server. The client proxies
5062
// requests to the server and handlers can be registered on the mux to handle
5163
// requests. The caller must close the test server.
5264
func NewMockAndHandler() (Client, *test.MockHandler, *test.Server) {
5365
return NewMockAndHandlerWithGithubToken("1234567890")
5466
}
5567

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+
5677
// NewMockAndHandlerWithGithubToken returns a Zappr Client that uses Github Token, Mockhandler, and Server. The client proxies
5778
// requests to the server and handlers can be registered on the mux to handle
5879
// requests. The caller must close the test server.
@@ -88,3 +109,14 @@ func (t *RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error)
88109
}
89110
return t.Transport.RoundTrip(req)
90111
}
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

Comments
 (0)