Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai committed Feb 28, 2024
2 parents a54b444 + b1424df commit 9ff2ee2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
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
22 changes: 16 additions & 6 deletions request_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ func doPOSTRequest[T, O any](
) (*T, error) {
var resultType T

numOpts := len(options)

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

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

// `null` is not accepted by the API
if len(options) > 0 {
if numOpts > 0 {
body, err := json.Marshal(options[0])
if err != nil {
return nil, err
}

req = req.SetBody(body)
req.SetBody(string(body))
}

r, err := coupleAPIErrors(req.Post(endpoint))
Expand All @@ -152,16 +157,21 @@ func doPUTRequest[T, O any](
) (*T, error) {
var resultType T

numOpts := len(options)

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

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

// `null` is not accepted by the API
if len(options) > 0 {
if numOpts > 0 {
body, err := json.Marshal(options[0])
if err != nil {
return nil, err
}

req = req.SetBody(body)
req.SetBody(string(body))
}

r, err := coupleAPIErrors(req.Put(endpoint))
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

0 comments on commit 9ff2ee2

Please sign in to comment.