-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_impl_test.go
175 lines (150 loc) · 4.46 KB
/
client_impl_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
package bonsai
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"golang.org/x/time/rate"
)
// ClientImplTestSuite is responsible for testing internal facing items/behavior
// that isn't part of the exposed interface, but is hard to test via that interface.
// Things like the default HTTP Client's rate-limiter (unexposed), and other implementation
// details fall under this umbrella - these test cases should be few.
type ClientImplTestSuite struct {
// Assertions embedded here allows all tests to reach through the suite to access assertion methods
*require.Assertions
// Suite is the testify/suite used for all HTTP request tests
suite.Suite
// serveMux is the request multiplexer used for tests
serveMux *chi.Mux
// server is the testing server on some local port
server *httptest.Server
// client allows each test to have a reachable *Client for testing
client *Client
}
func (s *ClientImplTestSuite) SetupSuite() {
// Configure http client and other miscellany
s.serveMux = chi.NewRouter()
s.server = httptest.NewServer(s.serveMux)
user, err := NewAccessKey("TestUser")
if err != nil {
log.Fatal(fmt.Errorf("invalid user received: %w", err))
}
password, err := NewAccessToken("TestToken")
if err != nil {
log.Fatal(fmt.Errorf("invalid token/password received: %w", err))
}
s.client = NewClient(
WithEndpoint(s.server.URL),
WithCredentialPair(
CredentialPair{
AccessKey: user,
AccessToken: password,
},
),
)
// configure testify
s.Assertions = require.New(s.T())
}
func (s *ClientImplTestSuite) TestClientDefaultRateLimit() {
c := NewClient()
s.Equal(DefaultClientBurstAllowance, c.rateLimiter.Burst())
s.InEpsilon(float64(rate.Every(DefaultClientBurstDuration)), float64(c.rateLimiter.Limit()), Float64Epsilon)
}
func (s *ClientImplTestSuite) TestListOptsValues() {
testCases := []struct {
name string
received listOpts
expect string
}{
{
name: "with populated values",
received: listOpts{
Page: 3,
Size: 100,
},
expect: "page=3&size=100",
},
{
name: "with empty values",
received: listOpts{},
expect: "",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
s.Equal(tc.received.values().Encode(), tc.expect)
})
}
}
func (s *ClientImplTestSuite) TestClientAll() {
const expectedPageCount = 4
var (
ctx = context.Background()
expectedPage = 1
)
s.serveMux.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HTTPHeaderContentType, HTTPContentTypeJSON)
respBody, _ := NewResponse()
respBody.PaginatedResponse = PaginatedResponse{
PageNumber: 3,
PageSize: 1,
TotalRecords: 3,
}
switch page := r.URL.Query().Get("page"); page {
case "", "1":
respBody.PaginatedResponse.PageNumber = 1
case "2":
respBody.PaginatedResponse.PageNumber = 2
case "3":
respBody.PaginatedResponse.PageNumber = 3
default:
s.FailNowf("invalid page parameter", "page parameter: %v", page)
}
err := json.NewEncoder(w).Encode(respBody)
s.NoError(err, "encode response body")
})
// The caller must track results against expected count
// A reminder to the reader: this is the caller.
var resultCount = 0
err := s.client.all(context.Background(), newEmptyListOpts(), func(opt listOpts) (*Response, error) {
reqPath := "/"
if opt.Valid() {
s.Equalf(expectedPage, opt.Page, "expected page number (%d) matches actual (%d)", expectedPage, opt.Page)
reqPath = fmt.Sprintf("%s?page=%d&size=1", reqPath, opt.Page)
}
req, err := s.client.NewRequest(ctx, "GET", reqPath, nil)
s.NoError(err, "new request for path")
resp, err := s.client.Do(context.Background(), req)
s.NoError(err, "do request")
expectedPage++
// A reference of how these funcs should handle this;
// recall, the response may be shorter than max.
//
// Ideally, this count wouldn't be derived from PageSize,
// but rather, from the total count of discovered items
// unmarshaled.
resultCount += max(resp.PageSize, 0)
if resultCount >= resp.TotalRecords {
resp.MarkPaginationComplete()
}
return resp, err
})
s.NoError(err, "client.all call")
s.Equalf(
expectedPage,
expectedPageCount,
"expected page visit count (%d) matches actual visit count (%d)",
expectedPageCount-1,
expectedPage-1,
)
}
func TestClientImplTestSuite(t *testing.T) {
suite.Run(t, new(ClientImplTestSuite))
}