-
Notifications
You must be signed in to change notification settings - Fork 8
/
apps_test.go
133 lines (121 loc) · 3.53 KB
/
apps_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package scalingo
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAppsClient_Update(t *testing.T) {
ctx := context.Background()
const appName = "my-app"
runs := map[string]struct {
testedClientCall func(c AppsService) error
expectedEndpoint string
expectedMethod string
expectedParams string
response interface{}
responseStatus int
}{
"it should enable the app router_logs attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsRouterLogs(ctx, appName, true)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"router_logs":true}`,
response: &AppResponse{},
responseStatus: 200,
},
"it should disable the app router_logs attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsRouterLogs(ctx, appName, false)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"router_logs":false}`,
response: &AppResponse{},
responseStatus: 200,
},
"it should enable the app force_https attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsForceHTTPS(ctx, appName, true)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"force_https":true}`,
response: &AppResponse{},
responseStatus: 200,
},
"it should disable the app force_https attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsForceHTTPS(ctx, appName, false)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"force_https":false}`,
response: &AppResponse{},
responseStatus: 200,
},
"it should enable the app sticky_session attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsStickySession(ctx, appName, true)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"sticky_session":true}`,
response: &AppResponse{},
responseStatus: 200,
},
"it should disable the app sticky_session attribute": {
testedClientCall: func(c AppsService) error {
_, err := c.AppsStickySession(ctx, appName, false)
return err
},
expectedEndpoint: "/v1/apps/my-app",
expectedMethod: "PUT",
expectedParams: `{"sticky_session":false}`,
response: &AppResponse{},
responseStatus: 200,
},
}
for msg, run := range runs {
t.Run(msg, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, run.expectedMethod, r.Method)
assert.Equal(t, run.expectedEndpoint, r.URL.Path)
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
require.NoError(t, err)
assert.Equal(t, run.expectedParams, buf.String())
if run.responseStatus != 0 {
w.WriteHeader(run.responseStatus)
}
if run.response != nil {
err := json.NewEncoder(w).Encode(&run.response)
assert.NoError(t, err)
}
}))
defer ts.Close()
c, err := New(ctx, ClientConfig{
APIEndpoint: ts.URL,
APIToken: "test",
})
require.NoError(t, err)
c.authClient = MockAuth(ctrl)
err = run.testedClientCall(c)
require.NoError(t, err)
})
}
}