Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exclude request body in POST and PUT helpers when options are unspecified #458

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -450,8 +449,10 @@ func (c *Client) MigrateInstance(ctx context.Context, linodeID int, opts Instanc
// simpleInstanceAction is a helper for Instance actions that take no parameters
// and return empty responses `{}` unless they return a standard error
func (c *Client) simpleInstanceAction(ctx context.Context, action string, linodeID int) error {
action = url.PathEscape(action)
e := fmt.Sprintf("linode/instances/%d/%s", linodeID, action)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
_, err := doPOSTRequest[any, any](
ctx,
c,
fmt.Sprintf("linode/instances/%d/%s", linodeID, action),
)
return err
}
14 changes: 13 additions & 1 deletion internal/testutil/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func MockRequestURL(path string) *regexp.Regexp {
return regexp.MustCompile(fmt.Sprintf("/[a-zA-Z0-9]+/%s", strings.TrimPrefix(path, "/")))
}

func MockRequestBodyValidate(t *testing.T, expected interface{}, response interface{}) httpmock.Responder {
func MockRequestBodyValidate(t *testing.T, expected any, response any) httpmock.Responder {
t.Helper()

return func(request *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -54,6 +54,18 @@ func MockRequestBodyValidate(t *testing.T, expected interface{}, response interf
}
}

func MockRequestBodyValidateNoBody(t *testing.T, response any) httpmock.Responder {
t.Helper()

return func(request *http.Request) (*http.Response, error) {
if request.Body != nil {
t.Fatal("got request body when no request body was expected")
}

return httpmock.NewJsonResponse(200, response)
}
}

// CreateMockClient is generic because importing the linodego package will result
// in a cyclic dependency. This pattern isn't ideal but works for now.
func CreateMockClient[T any](t *testing.T, createFunc func(*http.Client) T) *T {
Expand Down
42 changes: 32 additions & 10 deletions request_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,27 @@ func doPOSTRequest[T, O any](
ctx context.Context,
client *Client,
endpoint string,
options O,
options ...O,
) (*T, error) {
var resultType T

body, err := json.Marshal(options)
if err != nil {
return nil, err
numOpts := len(options)

if numOpts > 1 {
return nil, fmt.Errorf("invalid number of options: %d", len(options))
}

req := client.R(ctx).SetResult(&resultType)

if numOpts > 0 {
body, err := json.Marshal(options[0])
if err != nil {
return nil, err
}

req.SetBody(string(body))
}

req := client.R(ctx).SetResult(&resultType).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(endpoint))
if err != nil {
return nil, err
Expand All @@ -142,16 +153,27 @@ func doPUTRequest[T, O any](
ctx context.Context,
client *Client,
endpoint string,
options O,
options ...O,
) (*T, error) {
var resultType T

body, err := json.Marshal(options)
if err != nil {
return nil, err
numOpts := len(options)

if numOpts > 1 {
return nil, fmt.Errorf("invalid number of options: %d", len(options))
}

req := client.R(ctx).SetResult(&resultType)

if numOpts > 0 {
body, err := json.Marshal(options[0])
if err != nil {
return nil, err
}

req.SetBody(string(body))
}

req := client.R(ctx).SetResult(&resultType).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(endpoint))
if err != nil {
return nil, err
Expand Down
46 changes: 46 additions & 0 deletions request_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ func TestRequestHelpers_post(t *testing.T) {
}
}

func TestRequestHelpers_postNoOptions(t *testing.T) {
client := testutil.CreateMockClient(t, NewClient)

httpmock.RegisterRegexpResponder(
"POST",
testutil.MockRequestURL("/foo/bar"),
testutil.MockRequestBodyValidateNoBody(t, testResponse),
)

result, err := doPOSTRequest[testResultType, any](
context.Background(),
client,
"/foo/bar",
)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(*result, testResponse) {
t.Fatalf("actual response does not equal desired response: %s", cmp.Diff(result, testResponse))
}
}

func TestRequestHelpers_put(t *testing.T) {
client := testutil.CreateMockClient(t, NewClient)

Expand All @@ -96,6 +119,29 @@ func TestRequestHelpers_put(t *testing.T) {
}
}

func TestRequestHelpers_putNoOptions(t *testing.T) {
client := testutil.CreateMockClient(t, NewClient)

httpmock.RegisterRegexpResponder(
"PUT",
testutil.MockRequestURL("/foo/bar"),
testutil.MockRequestBodyValidateNoBody(t, testResponse),
)

result, err := doPUTRequest[testResultType, any](
context.Background(),
client,
"/foo/bar",
)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(*result, testResponse) {
t.Fatalf("actual response does not equal desired response: %s", cmp.Diff(result, testResponse))
}
}

func TestRequestHelpers_delete(t *testing.T) {
client := testutil.CreateMockClient(t, NewClient)

Expand Down