forked from luno/luno-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luno_internal_test.go
202 lines (172 loc) · 4.67 KB
/
luno_internal_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
package luno
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestDoGet(t *testing.T) {
type testRes struct {
Value string `json:"value"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(testRes{Value: "test"})
}))
defer srv.Close()
cl := NewClient()
cl.SetBaseURL(srv.URL)
var res testRes
err := cl.do(context.Background(), "GET", "/test", nil, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
}
func TestDoGetWithParametes(t *testing.T) {
type testReq struct {
Value string `url:"value"`
}
type testRes struct {
Value string `json:"value"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(testRes{Value: r.FormValue("value")})
}))
defer srv.Close()
cl := NewClient()
cl.SetBaseURL(srv.URL)
now := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
req := testReq{Value: now}
var res testRes
err := cl.do(context.Background(), "GET", "/test", &req, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
if res.Value != now {
t.Errorf("Expected %q, got %q", now, res.Value)
}
}
func TestDoPost(t *testing.T) {
type testReq struct {
Value string `url:"value"`
}
type testRes struct {
Value string `json:"value"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
json.NewEncoder(w).Encode(testRes{Value: r.FormValue("value")})
}))
defer srv.Close()
cl := NewClient()
cl.SetBaseURL(srv.URL)
now := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
req := testReq{Value: now}
var res testRes
err := cl.do(context.Background(), "POST", "/test", &req, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
if res.Value != now {
t.Errorf("Expected %q, got %q", now, res.Value)
}
}
func TestDoAuth(t *testing.T) {
type testRes struct {
Username string `json:"string"`
Password string `json:"password"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, _ := r.BasicAuth()
json.NewEncoder(w).Encode(testRes{
Username: username,
Password: password,
})
}))
defer srv.Close()
now := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
username := now + "_user"
password := now + "_password"
cl := NewClient()
cl.SetBaseURL(srv.URL)
cl.SetAuth(username, password)
var res testRes
// No auth provided:
err := cl.do(context.Background(), "POST", "/test", nil, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
if res.Username != "" {
t.Errorf("Expected empty username, got %q", res.Username)
}
// Auth provided:
err = cl.do(context.Background(), "POST", "/test", nil, &res, true)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
if res.Username != username || res.Password != password {
t.Errorf("Expected %s:%s, got %s:%s", username, password,
res.Username, res.Password)
}
}
func TestDoURLReplacement(t *testing.T) {
type testReq struct {
ID int64 `url:"id"`
Value string `url:"value"`
}
type testRes struct {
Path string `json:"path"`
Value string `json:"value"`
ID string `json:"id"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(testRes{
Path: r.URL.Path,
Value: r.FormValue("value"),
ID: r.FormValue("id"), // should be blank
})
}))
defer srv.Close()
cl := NewClient()
cl.SetBaseURL(srv.URL)
r := time.Now().UnixNano() / 1e6
value := fmt.Sprintf("%d_value", r)
req := testReq{ID: r, Value: value}
var res testRes
err := cl.do(context.Background(), "GET", "/test/{id}", &req, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
expPath := fmt.Sprintf("/test/%d", req.ID)
if res.Path != expPath {
t.Errorf("Expected %q, got %q", expPath, res.Path)
}
if res.ID != "" {
t.Errorf("Expected blank string, got %q", res.ID)
}
}
func TestDoJSONError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("this is invalid JSON"))
}))
defer srv.Close()
cl := NewClient()
cl.SetBaseURL(srv.URL)
var res interface{}
err := cl.do(context.Background(), "GET", "/", nil, &res, false)
if err == nil {
t.Errorf("Expected error, got nil")
}
str400 := "400 Bad Request"
if !strings.Contains(err.Error(), str400) {
t.Errorf("Expected error string to contain %q, got %q",
str400, err.Error())
}
}