|
| 1 | +package hcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutil" |
| 11 | + "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGenericRequest(t *testing.T) { |
| 15 | + t.Run("get", func(t *testing.T) { |
| 16 | + ctx, server, client := makeTestUtils(t) |
| 17 | + |
| 18 | + server.Expect([]mockutil.Request{ |
| 19 | + { |
| 20 | + Method: "GET", Path: "/resource", |
| 21 | + Status: 200, |
| 22 | + JSON: schema.ActionGetResponse{Action: schema.Action{ID: 1234}}, |
| 23 | + }, |
| 24 | + }) |
| 25 | + |
| 26 | + respBody, resp, err := getRequest[schema.ActionGetResponse](ctx, client, "/resource") |
| 27 | + require.NoError(t, err) |
| 28 | + require.NotNil(t, resp) |
| 29 | + require.NotNil(t, respBody) |
| 30 | + |
| 31 | + require.Equal(t, int64(1234), respBody.Action.ID) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("post", func(t *testing.T) { |
| 35 | + ctx, server, client := makeTestUtils(t) |
| 36 | + |
| 37 | + server.Expect([]mockutil.Request{ |
| 38 | + { |
| 39 | + Method: "POST", Path: "/resource", |
| 40 | + Want: func(t *testing.T, r *http.Request) { |
| 41 | + bodyBytes, err := io.ReadAll(r.Body) |
| 42 | + require.NoError(t, err) |
| 43 | + require.JSONEq(t, `{"hello": "world"}`, string(bodyBytes)) |
| 44 | + }, |
| 45 | + Status: 200, |
| 46 | + JSON: schema.ActionGetResponse{Action: schema.Action{ID: 1234}}, |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + respBody, resp, err := postRequest[schema.ActionGetResponse](ctx, client, "/resource", map[string]string{"hello": "world"}) |
| 51 | + require.NoError(t, err) |
| 52 | + require.NotNil(t, resp) |
| 53 | + require.NotNil(t, respBody) |
| 54 | + |
| 55 | + require.Equal(t, int64(1234), respBody.Action.ID) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("put", func(t *testing.T) { |
| 59 | + ctx, server, client := makeTestUtils(t) |
| 60 | + |
| 61 | + server.Expect([]mockutil.Request{ |
| 62 | + { |
| 63 | + Method: "PUT", Path: "/resource", |
| 64 | + Want: func(t *testing.T, r *http.Request) { |
| 65 | + bodyBytes, err := io.ReadAll(r.Body) |
| 66 | + require.NoError(t, err) |
| 67 | + require.JSONEq(t, `{"hello": "world"}`, string(bodyBytes)) |
| 68 | + }, |
| 69 | + Status: 200, |
| 70 | + JSON: schema.ActionGetResponse{Action: schema.Action{ID: 1234}}, |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + respBody, resp, err := putRequest[schema.ActionGetResponse](ctx, client, "/resource", map[string]string{"hello": "world"}) |
| 75 | + require.NoError(t, err) |
| 76 | + require.NotNil(t, resp) |
| 77 | + require.NotNil(t, respBody) |
| 78 | + |
| 79 | + require.Equal(t, int64(1234), respBody.Action.ID) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("delete", func(t *testing.T) { |
| 83 | + ctx, server, client := makeTestUtils(t) |
| 84 | + |
| 85 | + server.Expect([]mockutil.Request{ |
| 86 | + { |
| 87 | + Method: "DELETE", Path: "/resource", |
| 88 | + Status: 200, |
| 89 | + JSON: schema.ActionGetResponse{Action: schema.Action{ID: 1234}}, |
| 90 | + }, |
| 91 | + }) |
| 92 | + |
| 93 | + respBody, resp, err := deleteRequest[schema.ActionGetResponse](ctx, client, "/resource") |
| 94 | + require.NoError(t, err) |
| 95 | + require.NotNil(t, resp) |
| 96 | + require.NotNil(t, respBody) |
| 97 | + |
| 98 | + require.Equal(t, int64(1234), respBody.Action.ID) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("delete no result", func(t *testing.T) { |
| 102 | + ctx, server, client := makeTestUtils(t) |
| 103 | + |
| 104 | + server.Expect([]mockutil.Request{ |
| 105 | + { |
| 106 | + Method: "DELETE", Path: "/resource", |
| 107 | + Status: 204, |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + resp, err := deleteRequestNoResult(ctx, client, "/resource") |
| 112 | + require.NoError(t, err) |
| 113 | + require.NotNil(t, resp) |
| 114 | + }) |
| 115 | +} |
0 commit comments