-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
144 lines (107 loc) · 3.69 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
package atomicasset
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClient_SendError(t *testing.T) {
client := New("http://0.0.0.0:8080")
_, err := client.send("GET", "/", nil)
assert.EqualError(t, err, "Get \"http://0.0.0.0:8080/\": dial tcp 0.0.0.0:8080: connect: connection refused")
}
func TestClient_SendEncodeParametersFail(t *testing.T) {
client := Client{}
_, err := client.send("GET", "/", "a string")
assert.EqualError(t, err, "expects struct input, got string")
}
func TestClient_SendContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
time.Sleep(time.Second * 10)
}))
client := NewWithContext(srv.URL, ctx)
_, err := client.send("GET", "/", nil)
assert.Error(t, err)
assert.True(t, strings.HasSuffix(err.Error(), "deadline exceeded"), "Error was not deadline exceeded")
}
func TestClient_SendContextCancel(t *testing.T) {
done := make(chan interface{})
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
time.Sleep(time.Second * 10)
}))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
client := NewWithContext(srv.URL, ctx)
go func() {
defer close(done)
_, err := client.send("GET", "/", nil)
assert.Error(t, err)
assert.True(t, strings.HasSuffix(err.Error(), "context canceled"), "Error was not context canceled")
}()
time.Sleep(time.Second)
cancel()
<-done
}
func TestClient_APIError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
payload := `{
"success": false,
"message": "Some internal error"
}`
res.Header().Add("Content-type", "application/json")
res.WriteHeader(500)
_, err := res.Write([]byte(payload))
assert.NoError(t, err)
}))
client := New(srv.URL)
_, err := client.GetHealth()
assert.EqualError(t, err, "API Error: Some internal error")
}
func TestClient_APIErrorEmptyPayload(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-type", "application/json")
res.WriteHeader(404)
_, err := res.Write([]byte(`{}`))
assert.NoError(t, err)
}))
client := New(srv.URL)
health, err := client.GetHealth()
assert.NoError(t, err)
assert.Equal(t, 404, health.HTTPStatusCode)
}
func TestClient_ErrorNoPayload(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-type", "application/json")
res.WriteHeader(200)
_, err := res.Write([]byte{})
assert.NoError(t, err)
}))
client := New(srv.URL)
_, err := client.GetHealth()
assert.EqualError(t, err, "unexpected end of JSON input")
}
func TestClient_HostHeader(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
assert.Equal(t, "my-custom-host", req.Host)
res.Header().Add("Content-type", "application/json")
res.WriteHeader(200)
_, err := res.Write([]byte{})
assert.NoError(t, err)
}))
client := New(srv.URL)
client.Host = "my-custom-host"
_, err := client.send("GET", "/", nil)
assert.NoError(t, err)
}
func TestClient_InvalidContentType(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-type", "some-type")
}))
client := New(srv.URL)
_, err := client.send("GET", "/", nil)
assert.EqualError(t, err, "invalid content-type 'some-type', expected 'application/json'")
}