|
| 1 | +package request |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/UpCloudLtd/upcloud-go-api/v5/upcloud" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetGatewaysRequest(t *testing.T) { |
| 13 | + r := GetGatewaysRequest{} |
| 14 | + assert.Equal(t, "/gateway", r.RequestURL()) |
| 15 | + |
| 16 | + r = GetGatewaysRequest{ |
| 17 | + Filters: []QueryFilter{ |
| 18 | + FilterLabel{ |
| 19 | + Label: upcloud.Label{ |
| 20 | + Key: "color", |
| 21 | + Value: "green", |
| 22 | + }, |
| 23 | + }, |
| 24 | + FilterLabelKey{Key: "size"}, |
| 25 | + }, |
| 26 | + } |
| 27 | + assert.Equal(t, "/gateway?label=color%3Dgreen&label=size", r.RequestURL()) |
| 28 | +} |
| 29 | + |
| 30 | +func TestGetGatewayRequest(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + r := GetGatewayRequest{UUID: "fake"} |
| 34 | + assert.Equal(t, gatewayBaseURL+"/fake", r.RequestURL()) |
| 35 | +} |
| 36 | + |
| 37 | +func TestDeleteGatewayRequest(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + r := DeleteGatewayRequest{UUID: "fake"} |
| 41 | + assert.Equal(t, gatewayBaseURL+"/fake", r.RequestURL()) |
| 42 | +} |
| 43 | + |
| 44 | +func TestCreateGatewayRequest(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + const want string = ` |
| 48 | + { |
| 49 | + "name": "example-gateway", |
| 50 | + "zone": "fi-hel1", |
| 51 | + "features": [ |
| 52 | + "nat" |
| 53 | + ], |
| 54 | + "routers": [ |
| 55 | + { |
| 56 | + "uuid": "0485d477-8d8f-4c97-9bef-731933187538" |
| 57 | + } |
| 58 | + ], |
| 59 | + "configured_status": "started" |
| 60 | + } |
| 61 | + ` |
| 62 | + r := CreateGatewayRequest{ |
| 63 | + Name: "example-gateway", |
| 64 | + Zone: "fi-hel1", |
| 65 | + Features: []upcloud.GatewayFeature{upcloud.GatewayFeatureNAT}, |
| 66 | + Routers: []GatewayRouter{ |
| 67 | + { |
| 68 | + UUID: "0485d477-8d8f-4c97-9bef-731933187538", |
| 69 | + }, |
| 70 | + }, |
| 71 | + ConfiguredStatus: upcloud.GatewayConfiguredStatusStarted, |
| 72 | + } |
| 73 | + got, err := json.Marshal(&r) |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, gatewayBaseURL, r.RequestURL()) |
| 76 | + assert.JSONEq(t, want, string(got)) |
| 77 | +} |
| 78 | + |
| 79 | +func TestModifyGatewayRequest(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + want := ` |
| 83 | + { |
| 84 | + "name": "example-gateway", |
| 85 | + "configured_status": "started" |
| 86 | + } |
| 87 | + ` |
| 88 | + r := ModifyGatewayRequest{ |
| 89 | + UUID: "fake", |
| 90 | + Name: "example-gateway", |
| 91 | + ConfiguredStatus: upcloud.GatewayConfiguredStatusStarted, |
| 92 | + } |
| 93 | + got, err := json.Marshal(&r) |
| 94 | + require.NoError(t, err) |
| 95 | + assert.JSONEq(t, want, string(got)) |
| 96 | + |
| 97 | + want = ` |
| 98 | + { |
| 99 | + "name": "example-gateway" |
| 100 | + } |
| 101 | + ` |
| 102 | + r = ModifyGatewayRequest{ |
| 103 | + UUID: "fake", |
| 104 | + Name: "example-gateway", |
| 105 | + } |
| 106 | + got, err = json.Marshal(&r) |
| 107 | + require.NoError(t, err) |
| 108 | + assert.JSONEq(t, want, string(got)) |
| 109 | + |
| 110 | + want = ` |
| 111 | + { |
| 112 | + "configured_status": "started" |
| 113 | + } |
| 114 | + ` |
| 115 | + r = ModifyGatewayRequest{ |
| 116 | + UUID: "fake", |
| 117 | + ConfiguredStatus: upcloud.GatewayConfiguredStatusStarted, |
| 118 | + } |
| 119 | + got, err = json.Marshal(&r) |
| 120 | + require.NoError(t, err) |
| 121 | + assert.JSONEq(t, want, string(got)) |
| 122 | + |
| 123 | + assert.Equal(t, gatewayBaseURL+"/fake", r.RequestURL()) |
| 124 | +} |
0 commit comments