Skip to content

Commit 804c61e

Browse files
Fix unittests for draft mock http client
1 parent 6ed3ab7 commit 804c61e

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

mock/client.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ import (
1212
type EndpointPattern = *regexp.Regexp
1313

1414
// Users
15-
var UsersGetEndpoint EndpointPattern = regexp.MustCompile(`^/users/[a-z]+`)
15+
var UsersGetEndpoint EndpointPattern = regexp.MustCompile(`^\/users\/[a-zA-Z]+`)
1616

1717
// Orgs
1818
var OrgsListEndpoint = regexp.MustCompile(`^\/users\/([a-z]+\/orgs|orgs)$`)
19-
var OrgsGetEndpoint = regexp.MustCompile(`^/orgs/[a-z]+`)
19+
var OrgsGetEndpoint = regexp.MustCompile(`^\/orgs\/[a-z]+`)
2020

2121
type RequestMatch struct {
2222
EndpointPattern EndpointPattern
2323
Method string // GET or POST
2424
}
2525

2626
func (rm *RequestMatch) Match(r *http.Request) bool {
27-
if r.Method == rm.Method && rm.EndpointPattern.MatchString(r.URL.Path) {
27+
if (r.Method == rm.Method) &&
28+
r.URL.Path == rm.EndpointPattern.FindString(r.URL.Path) {
2829
return true
2930
}
3031

@@ -49,6 +50,35 @@ type MockRoundTripper struct {
4950
func (mrt *MockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
5051
for requestMatch, respBodies := range mrt.RequestMocks {
5152
if requestMatch.Match(r) {
53+
if len(respBodies) == 0 {
54+
fmt.Printf(
55+
"no more available mocked responses for endpoit %s\n",
56+
r.URL.Path,
57+
)
58+
59+
fmt.Println("please add the required RequestMatch to the MockHttpClient. Eg.")
60+
fmt.Println(`
61+
mockedHttpClient := NewMockHttpClient(
62+
WithRequestMatch(
63+
RequestMatchUsersGet,
64+
MustMarshall(github.User{
65+
Name: github.String("foobar"),
66+
}),
67+
),
68+
WithRequestMatch(
69+
RequestMatchOrganizationsList,
70+
MustMarshall([]github.Organization{
71+
{
72+
Name: github.String("foobar123"),
73+
},
74+
}),
75+
),
76+
)
77+
`)
78+
79+
panic(nil)
80+
}
81+
5282
resp := respBodies[0]
5383

5484
defer func(mrt *MockRoundTripper, rm RequestMatch) {

mock/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestMockClient(t *testing.T) {
4141

4242
orgs, _, err := c.Organizations.List(
4343
ctx,
44-
*user.Name,
44+
*(user.Name),
4545
nil,
4646
)
4747

0 commit comments

Comments
 (0)