-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
120 lines (91 loc) · 2.82 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
package expensify
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
partnerUserID = "testUserID"
partnerUserSecret = "testUserSecret-123-abc"
)
func TestNewClient(t *testing.T) {
client, err := NewClient(partnerUserID, partnerUserSecret)
require.NoError(t, err)
require.NotNil(t, client)
// Are endpoints/resources present?
assert.Equal(t, &expenseService{client: client}, client.Expense)
// Is default configuration present?
if assert.NotNil(t, client.baseURL) {
assert.Equal(t, baseURL, client.baseURL.String())
}
assert.NotEmpty(t, client.userAgent)
assert.Equal(t, partnerUserID, client.partnerUserID)
assert.Equal(t, partnerUserSecret, client.partnerUserSecret)
assert.Equal(t, defaultHTTPClient, client.httpClient)
}
func TestDo(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
_, _ = fmt.Fprint(w, `{"A":"a"}`)
}
client, teardown := setup(t, hf)
defer teardown()
type foo struct {
A string
}
req, err := client.newRequest(context.Background(), "", "", nil)
require.NoError(t, err)
var body foo
err = client.do(req, &body)
require.NoError(t, err)
assert.Equal(t, foo{"a"}, body)
}
func TestDo_HTTPError(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
httpErr := Error{
StatusCode: http.StatusBadRequest,
Message: "This was a bad request!",
}
err := json.NewEncoder(w).Encode(httpErr)
require.NoError(t, err)
}
client, teardown := setup(t, hf)
defer teardown()
req, err := client.newRequest(context.Background(), "", "", nil)
require.NoError(t, err)
err = client.do(req, nil)
require.EqualError(t, err, "Bad Request: This was a bad request!")
}
func TestDo_RedirectLoop(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}
client, teardown := setup(t, hf)
defer teardown()
req, err := client.newRequest(context.Background(), "", "", nil)
require.NoError(t, err)
err = client.do(req, nil)
require.Error(t, err)
assert.IsType(t, err, &url.Error{})
}
// setup sets up a test HTTP server along with a client that is configured to
// talk to that test server. Tests should pass a handler function which provides
// the response for the API method being tested.
func setup(t *testing.T, hf http.HandlerFunc) (*Client, func()) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hf.ServeHTTP(w, r)
}))
client, err := NewClient(partnerUserID, partnerUserSecret, SetClient(srv.Client()))
require.NoError(t, err)
srvURL, err := url.ParseRequestURI(srv.URL)
require.NoError(t, err)
client.baseURL = srvURL
return client, func() { srv.Close() }
}