-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_test.go
129 lines (110 loc) · 3.04 KB
/
main_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
package statuspage
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"testing"
)
type mockHTTPClient struct {
req *http.Request
statusCode int
}
func NewMockHTTPClient(statusCode int) *mockHTTPClient {
return &mockHTTPClient{
statusCode: statusCode,
}
}
func (c *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
c.req = req
return &http.Response{StatusCode: c.statusCode}, nil
}
type mockClient struct {
resp *http.Response
err error
}
func NewMockClient(resp *http.Response, err error) *mockClient {
return &mockClient{
resp: resp,
err: err,
}
}
func (client *mockClient) doHTTPRequest(method, endpoint string, item interface{}) (resp *http.Response, err error) {
return client.resp, client.err
}
func TestDoHTTPRequest(t *testing.T) {
token := "token"
client := NewClient(token)
mc := NewMockHTTPClient(200)
client.httpClient = mc
r1, _ := http.NewRequest(
"POST",
"https://api.statuspage.io/v1/endpoint",
strings.NewReader("{\"Name\":\"resource_name\"}"),
)
r2, _ := http.NewRequest(
"GET",
"https://api.statuspage.io/v1/components",
strings.NewReader("{\"Name\":\"resource_name\",\"Description\":\"description\"}"),
)
testCases := []struct {
method string
endpoint string
resource interface{}
reqShould *http.Request
}{
{
"POST",
"/endpoint",
struct{ Name string }{"resource_name"},
r1,
},
{
"GET",
"/components",
struct {
Name string
Description string
}{
"resource_name",
"description",
},
r2,
},
}
for _, testCase := range testCases {
client.doHTTPRequest(testCase.method, testCase.endpoint, testCase.resource)
if mc.req.Method != testCase.reqShould.Method {
t.Errorf("Request method should be %s, got %s", testCase.method, mc.req.Method)
}
if mc.req.URL.String() != testCase.reqShould.URL.String() {
t.Errorf("Request URL should be %s, got %s", testCase.reqShould.URL.String(), mc.req.URL.String())
}
if mc.req.Header.Get("Authorization") != "OAuth "+token {
t.Errorf("Request should have Authorization header set to OAuth %s, got %s", token, mc.req.Header.Get("Authorization"))
}
bodyWant := new(bytes.Buffer)
getBody, _ := mc.req.GetBody()
bodyWant.ReadFrom(getBody)
bodyWantS := bodyWant.String()
bodyHave := new(bytes.Buffer)
bodyHave.ReadFrom(testCase.reqShould.Body)
bodyHaveS := bodyHave.String()
if bodyWantS != bodyHaveS {
t.Errorf("Request Body should be %s, got %s", bodyWantS, bodyHaveS)
}
}
}
func TestCreateResource(t *testing.T) {
resourceName := "resource name"
r := &http.Response{StatusCode: http.StatusCreated, Body: ioutil.NopCloser(bytes.NewReader([]byte("{\"Name\":\"" + resourceName + "\"}")))}
client := NewMockClient(r, nil)
target := struct{ Name string }{}
err := createResource(client, "somePageID", "component", struct{ Name string }{resourceName}, &target)
if err != nil {
t.Errorf("Error while creating resource: %+v", err)
}
if target.Name != resourceName {
t.Errorf("Failed to read resources, body should be. %s, got: %s", resourceName, target.Name)
}
}