From f0eaaace2d7ef6eb8918e83f18798869a227ff7d Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 16 Mar 2022 11:34:30 +0100 Subject: [PATCH] Fix a nullpointer in tester. --- test/tester.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/test/tester.go b/test/tester.go index 4b843e7f..e2007c3d 100644 --- a/test/tester.go +++ b/test/tester.go @@ -58,6 +58,7 @@ func ExtractCookie(r *http.Response) *http.Cookie { func NewAccountsTester(dbName string) (*AccountsTester, error) { ctx := context.Background() logger := logrus.New() + logger.Out = ioutil.Discard // Initialise the environment. jwt.PortalName = testPortalAddr @@ -116,8 +117,8 @@ func NewAccountsTester(dbName string) (*AccountsTester, error) { } // Wait for the accounts tester to be fully ready. err = build.Retry(50, time.Millisecond, func() error { - _, _, err = at.HealthGet() - return err + _, _, e := at.HealthGet() + return e }) if err != nil { return nil, errors.AddContext(err, "failed to start accounts tester in the given time") @@ -169,14 +170,14 @@ func (at *AccountsTester) SetToken(t string) { // Get executes a GET request against the test service. // // NOTE: The Body of the returned response is already read and closed. -func (at *AccountsTester) Get(endpoint string, params url.Values) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) Get(endpoint string, params url.Values) (*http.Response, []byte, error) { return at.request(http.MethodGet, endpoint, params, nil, nil) } // Delete executes a DELETE request against the test service. // // NOTE: The Body of the returned response is already read and closed. -func (at *AccountsTester) Delete(endpoint string, params url.Values) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) Delete(endpoint string, params url.Values) (*http.Response, []byte, error) { return at.request(http.MethodDelete, endpoint, params, nil, nil) } @@ -184,7 +185,7 @@ func (at *AccountsTester) Delete(endpoint string, params url.Values) (r *http.Re // // NOTE: The Body of the returned response is already read and closed. // TODO Remove the url.Values in favour of a simple map. -func (at *AccountsTester) Post(endpoint string, params url.Values, bodyParams url.Values) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) Post(endpoint string, params url.Values, bodyParams url.Values) (*http.Response, []byte, error) { if params == nil { params = url.Values{} } @@ -197,12 +198,12 @@ func (at *AccountsTester) Post(endpoint string, params url.Values, bodyParams ur } bodyBytes, err := json.Marshal(bodyMap) if err != nil { - return + return &http.Response{}, nil, err } serviceURL := testPortalAddr + ":" + testPortalPort + endpoint + "?" + params.Encode() req, err := http.NewRequest(http.MethodPost, serviceURL, bytes.NewBuffer(bodyBytes)) if err != nil { - return nil, nil, err + return &http.Response{}, nil, err } req.Header.Set("Content-Type", "application/json") return at.executeRequest(req) @@ -211,10 +212,10 @@ func (at *AccountsTester) Post(endpoint string, params url.Values, bodyParams ur // Put executes a PUT request against the test service. // // NOTE: The Body of the returned response is already read and closed. -func (at *AccountsTester) Put(endpoint string, params url.Values, bodyParams url.Values) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) Put(endpoint string, params url.Values, bodyParams url.Values) (*http.Response, []byte, error) { b, err := json.Marshal(bodyParams) if err != nil { - return nil, nil, errors.AddContext(err, "failed to marshal the body JSON") + return &http.Response{}, nil, errors.AddContext(err, "failed to marshal the body JSON") } return at.request(http.MethodPut, endpoint, params, b, nil) } @@ -222,10 +223,10 @@ func (at *AccountsTester) Put(endpoint string, params url.Values, bodyParams url // Patch executes a PATCH request against the test service. // // NOTE: The Body of the returned response is already read and closed. -func (at *AccountsTester) Patch(endpoint string, params url.Values, bodyParams url.Values) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) Patch(endpoint string, params url.Values, bodyParams url.Values) (*http.Response, []byte, error) { b, err := json.Marshal(bodyParams) if err != nil { - return nil, nil, errors.AddContext(err, "failed to marshal the body JSON") + return &http.Response{}, nil, errors.AddContext(err, "failed to marshal the body JSON") } return at.request(http.MethodPatch, endpoint, params, b, nil) } @@ -233,7 +234,7 @@ func (at *AccountsTester) Patch(endpoint string, params url.Values, bodyParams u // CreateUserPost is a helper method that creates a new user. // // NOTE: The Body of the returned response is already read and closed. -func (at *AccountsTester) CreateUserPost(emailAddr, password string) (r *http.Response, body []byte, err error) { +func (at *AccountsTester) CreateUserPost(emailAddr, password string) (*http.Response, []byte, error) { params := url.Values{} params.Set("email", emailAddr) params.Set("password", password) @@ -251,11 +252,11 @@ func (at *AccountsTester) UserPUT(email, password, stipeID string) (*http.Respon "stripeCustomerId": stipeID, }) if err != nil { - return nil, nil, errors.AddContext(err, "failed to marshal the body JSON") + return &http.Response{}, nil, errors.AddContext(err, "failed to marshal the body JSON") } req, err := http.NewRequest(http.MethodPut, serviceURL, bytes.NewBuffer(b)) if err != nil { - return nil, nil, err + return &http.Response{}, nil, err } return at.executeRequest(req) } @@ -271,7 +272,7 @@ func (at *AccountsTester) request(method string, endpoint string, queryParams ur serviceURL := testPortalAddr + ":" + testPortalPort + endpoint + "?" + queryParams.Encode() req, err := http.NewRequest(method, serviceURL, bytes.NewBuffer(body)) if err != nil { - return nil, nil, err + return &http.Response{}, nil, err } for name, val := range headers { req.Header.Set(name, val) @@ -285,7 +286,7 @@ func (at *AccountsTester) request(method string, endpoint string, queryParams ur // NOTE: The Body of the returned response is already read and closed. func (at *AccountsTester) executeRequest(req *http.Request) (*http.Response, []byte, error) { if req == nil { - return nil, nil, errors.New("invalid request") + return &http.Response{}, nil, errors.New("invalid request") } if at.APIKey != "" { req.Header.Set(api.APIKeyHeader, at.APIKey) @@ -299,7 +300,7 @@ func (at *AccountsTester) executeRequest(req *http.Request) (*http.Response, []b client := http.Client{} r, err := client.Do(req) if err != nil { - return nil, nil, err + return &http.Response{}, nil, err } return processResponse(r) }