-
Notifications
You must be signed in to change notification settings - Fork 120
/
client_test.go
202 lines (175 loc) · 5.17 KB
/
client_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// SPDX-License-Identifier: BSD-3-Clause
//
package gofish
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stmcginnis/gofish/common"
)
const (
errMsg = `{
"code": "Base.1.0.GeneralError",
"message": "A general error has occurred. See ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"MessageId": "Base.1.0.PropertyValueNotInList",
"Message": "The value Red for the property IndicatorLED is not in the list of acceptable values",
"MessageArgs": [
"RED",
"IndicatorLED"
],
"Severity": "Warning",
"Resolution": "Remove the property from the request body and resubmit the request if the operation failed"
},
{
"MessageId": "Base.1.0.PropertyNotWriteable",
"Message": "The property SKU is a read only property and cannot be assigned a value",
"MessageArgs": [
"SKU"
],
"Severity": "Warning",
"Resolution": "Remove the property from the request body and resubmit the request if the operation failed"
}
]
}`
expectErrorStatus = `{"error": ` + errMsg + "}"
nonErrorStructErrorStatus = "Internal Server Error"
)
func testError(code int, t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(code)
w.Write([]byte(expectErrorStatus)) //nolint
}))
defer ts.Close()
_, err := Connect(ClientConfig{Endpoint: ts.URL, HTTPClient: ts.Client()})
if err == nil {
t.Error("Update call should fail")
}
errStruct, ok := err.(*common.Error)
if !ok {
t.Errorf("%d should return known error type: %v", code, err)
}
if errStruct.HTTPReturnedStatusCode != code {
t.Errorf("The error code is different from %d", code)
}
errBody, err := json.MarshalIndent(errStruct, " ", " ")
if err != nil {
t.Errorf("Marshall error %v got: %s", errStruct, err)
}
if errMsg != string(errBody) {
t.Errorf("Expect:\n%s\nGot:\n%s", errMsg, string(errBody))
}
}
// TestError400 tests the parsing of error reply.
func TestError400(t *testing.T) {
testError(400, t)
}
// TestError404 tests the parsing of error reply.
func TestError404(t *testing.T) {
testError(404, t)
}
// TestErrorOther tests failures that do not return an Error struct
func TestErrorOther(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(500)
w.Write([]byte(nonErrorStructErrorStatus)) //nolint
}))
defer ts.Close()
_, err := Connect(ClientConfig{Endpoint: ts.URL, HTTPClient: ts.Client()})
if err == nil {
t.Error("connect should fail")
}
errStruct, ok := err.(*common.Error)
if !ok {
t.Errorf("call should return known error type: %v", err)
}
if errStruct.HTTPReturnedStatusCode != 500 {
t.Errorf("The error code is different from 500")
}
if err.Error() != "500: Internal Server Error" {
t.Errorf("Expected '500: %s', got '%s'", nonErrorStructErrorStatus, err.Error())
}
}
// TestConnectContextTimeout
func TestConnectContextTimeout(t *testing.T) {
// ctx will timeout very quickly
ctx, cancel := context.WithTimeout(
context.Background(),
1*time.Microsecond)
defer cancel()
_, err := ConnectContext(
ctx,
ClientConfig{
Endpoint: "https://testContextTimeout.com",
})
if !errors.Is(err, context.DeadlineExceeded) {
t.Error("Context should timeout")
}
}
func TestServiceGetter(t *testing.T) {
type serviceGetter interface {
GetService() *Service
}
var sg serviceGetter = &APIClient{}
if sg.GetService() != nil {
t.Errorf("Empty client should return a nil service")
}
}
// TestConnectContextCancel
func TestConnectContextCancel(t *testing.T) {
// ctx will be cancelled
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := ConnectContext(
ctx,
ClientConfig{
Endpoint: "https://testContextCancel.com",
})
if !errors.Is(err, context.Canceled) {
t.Error("Context should be cancelled")
}
}
// TestConnectDefaultContextTimeout
func TestConnectDefaultContextTimeout(t *testing.T) {
// ctx will timeout very quickly
ctx, cancel := context.WithTimeout(
context.Background(),
1*time.Microsecond)
defer cancel()
_, err := ConnectDefaultContext(
ctx,
"https://testContextTimeout.com",
)
if !errors.Is(err, context.DeadlineExceeded) {
t.Error("Context should timeout")
}
}
// TestConnectDefaultContextCancel
func TestConnectDefaultContextCancel(t *testing.T) {
// ctx will be cancelled
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := ConnectDefaultContext(
ctx,
"https://testContextCancel.com",
)
if !errors.Is(err, context.Canceled) {
t.Error("Context should be cancelled")
}
}
func TestClientRunRawRequestNoURL(t *testing.T) {
client := APIClient{sem: make(chan bool, 1)}
_, err := client.runRawRequest("", "", nil, "") //nolint:bodyclose
if err == nil {
t.Error("Request without relative path should have failed")
}
if err.Error() != "unable to execute request, no target provided" {
t.Errorf("Unexpected error response: %s", err.Error())
}
}