-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
160 lines (130 loc) · 3.7 KB
/
http_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tado
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/SebastiaanKlippert/go-tado/tadoauth"
"github.com/stretchr/testify/assert"
)
type testGet struct{}
func (tg *testGet) method() string {
return http.MethodGet
}
func (tg *testGet) path() string {
return "/v1/somepath"
}
func (tg *testGet) body() interface{} {
return nil
}
type testPost struct {
InStr string
InInt int
}
func (tp *testPost) method() string {
return http.MethodPost
}
func (tp *testPost) path() string {
return "/v1/somepath"
}
func (tp *testPost) body() interface{} {
return tp
}
type testOut struct {
OutField string
}
func TestClient_Do(t *testing.T) {
// Create mock authentication struct
tr := &tadoauth.TokenResponse{
AccessToken: "thisIsAFakeToken",
TokenType: "bearer",
RefreshToken: "refreshToken",
ExpiresIn: 588,
Scope: "scope",
Jti: "jti",
}
// Create mock Tado client
c := NewClient("", "")
c.tr = tr
c.accessTokenValidUntil = time.Now().Add(time.Minute)
// Prepare fake output
out := new(testOut)
// Start fake HTTP server
mockStatusCode := http.StatusBadRequest
mockResponseBody := "Bad request"
var incomingRequest *http.Request
var incomingRequestBody []byte
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
incomingRequest = r
incomingRequestBody, _ = ioutil.ReadAll(r.Body)
w.WriteHeader(mockStatusCode)
_, _ = fmt.Fprint(w, mockResponseBody)
}))
defer ts.Close()
c.baseURL = ts.URL
// Test a GET method returning a Bad Request status
tg := new(testGet)
err := c.do(tg, out)
if incomingRequest == nil {
t.Fatal("incomingRequest is nil")
}
assert.Equal(t, http.MethodGet, incomingRequest.Method)
assert.Equal(t, "/v1/somepath", incomingRequest.URL.String())
assert.Equal(t, "", string(incomingRequestBody))
assert.Equal(t, "Bearer thisIsAFakeToken", incomingRequest.Header.Get("Authorization"))
if assert.Error(t, err) {
assert.Equal(t, "error: HTTP status 400: Bad request", err.Error())
}
// Test a GET method returning an OK status with invalid JSON
mockStatusCode = http.StatusOK
mockResponseBody = `{ThisIsNotJSON}`
err = c.do(tg, out)
if assert.Error(t, err) {
assert.True(t, strings.HasPrefix(err.Error(), "error decoding output: "), "error has unexpected prefix, error is: %s", err)
}
// Test a GET method returning an OK status with valid JSON
mockStatusCode = http.StatusOK
mockResponseBody = `{"OutField":"AbCdEf"}`
err = c.do(tg, out)
assert.NoError(t, err)
assert.Equal(t, "AbCdEf", out.OutField)
// Test a POST method with a valid JSON response
mockResponseBody = `{"OutField":"AbCdEfGh"}`
tp := &testPost{
InStr: "string",
InInt: 999,
}
err = c.do(tp, out)
assert.NoError(t, err)
assert.Equal(t, `{"InStr":"string","InInt":999}`+"\n", string(incomingRequestBody))
assert.Equal(t, http.MethodPost, incomingRequest.Method)
assert.Equal(t, "application/json", incomingRequest.Header.Get("Content-Type"))
assert.Equal(t, "AbCdEfGh", out.OutField)
}
func TestInputMethods(t *testing.T) {
//test that body is not nil for PUT and POST methods and is nil for all other methods
testStructs := []input{
new(GetMeInput),
new(GetHomeInput),
new(GetHomeStateInput),
new(GetDevicesInput),
new(GetZonesInput),
new(GetZoneStateInput),
new(GetUsersInput),
new(GetWeatherInput),
new(GetDayReportInput),
new(PutOverlayInput),
new(DeleteOverlayInput),
}
for _, s := range testStructs {
switch s.method() {
case http.MethodPut, http.MethodPost:
assert.NotNil(t, s.body(), "body is nil on %T with method %s", s, s.method())
default:
assert.Nil(t, s.body(), "body is not nil on %T with method %s", s, s.method())
}
}
}