Skip to content

Commit

Permalink
Fix a nullpointer in tester.
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-tex committed Mar 16, 2022
1 parent d79e743 commit f0eaaac
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions test/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -169,22 +170,22 @@ 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)
}

// Post executes a POST request against the test service.
//
// 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{}
}
Expand All @@ -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)
Expand All @@ -211,29 +212,29 @@ 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)
}

// 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)
}

// 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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit f0eaaac

Please sign in to comment.