-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
187 lines (137 loc) · 4.19 KB
/
Copy pathclient_test.go
File metadata and controls
187 lines (137 loc) · 4.19 KB
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
package splitwise
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Client_IfTokenIsDefinedItShouldBeInHeader(t *testing.T) {
const testToken = "testtoken"
client, cancel := testClientWithHandler(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader != fmt.Sprintf("Bearer %s", testToken) {
assert.Failf(t, "authorization header is not correct", "authorization header is [%s], should be [Bearer %s]", authHeader, testToken)
}
w.WriteHeader(http.StatusOK)
}))
defer cancel()
ctx := context.Background()
client.Token = testToken
res, err := client.do(ctx, http.MethodGet, "/generic", nil, nil)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func Test_Client_IfNoClientIsDefinedUseADefaultOne(t *testing.T) {
wasCalled := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wasCalled = true
w.Write([]byte("success"))
}))
defer server.Close()
url := server.URL
client := Client{
BaseUrl: url,
}
ctx := context.Background()
res, err := client.do(ctx, http.MethodGet, "", nil, nil)
require.NoError(t, err)
rawBody, err := io.ReadAll(res.Body)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, "success", string(rawBody))
assert.True(t, wasCalled)
}
func Test_Client_BaseURL(t *testing.T) {
client := Client{}
assert.Equal(t, DefaultBaseUrl, client.baseUrl())
const url = "https://fakesite.com"
client.BaseUrl = url
assert.Equal(t, url, client.baseUrl())
}
func Test_Client_ApiVersionPath(t *testing.T) {
client := Client{}
assert.Equal(t, DefaultApiVersionPath, client.apiVersionPath())
const path = "/path"
client.ApiVersionPath = path
assert.Equal(t, path, client.apiVersionPath())
}
func Test_JsonMarshal(t *testing.T) {
client := Client{}
f1ptr := reflect.ValueOf(json.Marshal)
f2ptr := reflect.ValueOf(client.marshal())
assert.Equal(t, f1ptr.Pointer(), f2ptr.Pointer())
marshal := func(_ interface{}) ([]byte, error) {
return nil, nil
}
client.JsonMarshaler = marshal
f1ptr = reflect.ValueOf(marshal)
f2ptr = reflect.ValueOf(client.marshal())
assert.Equal(t, f1ptr.Pointer(), f2ptr.Pointer())
}
func Test_JsonUnmarshal(t *testing.T) {
client := Client{}
f1ptr := reflect.ValueOf(json.Unmarshal)
f2ptr := reflect.ValueOf(client.unmarshal())
assert.Equal(t, f1ptr.Pointer(), f2ptr.Pointer())
unmarshal := func(_ []byte, _ interface{}) error {
return nil
}
client.JsonUnmarshaler = unmarshal
f1ptr = reflect.ValueOf(unmarshal)
f2ptr = reflect.ValueOf(client.unmarshal())
assert.Equal(t, f1ptr.Pointer(), f2ptr.Pointer())
}
func Test_Logger(t *testing.T) {
client := Client{}
assert.Equal(t, log.Default(), client.getLogger())
var buf bytes.Buffer
logger := log.New(io.Writer(&buf), "", 0)
client.Logger = logger
const data = "test"
client.getLogger().Printf(data)
assert.Equal(t, len(data)+1, buf.Len())
}
func Test_Client_Do_FailsIfPathIsWrong(t *testing.T) {
client := Client{
BaseUrl: "https:// ////",
ApiVersionPath: " ",
}
ctx := context.Background()
_, err := client.do(ctx, http.MethodGet, "", nil, nil)
require.Error(t, err)
var parseErr *url.Error
assert.ErrorAs(t, err, &parseErr)
}
func Test_Client_Do_shouldFailIfMarshalingFails(t *testing.T) {
expectedErr := errors.New("this is expected")
stubMarshal := func(v interface{}) ([]byte, error) {
return nil, expectedErr
}
client := Client{
JsonMarshaler: stubMarshal,
}
ctx := context.Background()
_, err := client.do(ctx, http.MethodGet, "", nil, nil)
require.Error(t, err)
assert.ErrorIs(t, err, expectedErr)
}
func Test_Client_Do_shouldFailIfRequestCannotBeCreated(t *testing.T) {
client, cancel := testClientWithHandler(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Fatal("this should not have been reached")
}))
defer cancel()
_, err := client.do(nil, http.MethodGet, "", nil, nil)
assert.Error(t, err)
_, err = client.do(context.Background(), "INVALID\n", "", nil, nil)
assert.Error(t, err)
}