Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new mock client to match responses by endpoint #485

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/api/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,19 @@ func NewDebugMock(o io.Writer, res ...mock.Response) *API {

return api
}

// NewMockMatchingByEndpoint creates a new api.API from a list of Responses, matching by endpoint.
// Defaults to a dummy APIKey for authentication, which is not checked
func NewMockMatchingByEndpoint(res map[string][]mock.Response) *API {
gersonsosa marked this conversation as resolved.
Show resolved Hide resolved
api, err := NewAPI(Config{
Client: mock.NewMatchingByEndpointClient(res),
Host: mockSchemaHost,
AuthWriter: auth.APIKey("dummy"),
})

if err != nil {
panic(err)
}

return api
}
48 changes: 48 additions & 0 deletions pkg/api/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,58 @@ package mock
import (
"fmt"
"net/http"
"regexp"
"sync"
"sync/atomic"
)

// NewMatchingByEndpointClient returns a pointer to http.Client with the mocked Transport.
func NewMatchingByEndpointClient(r map[string][]Response) *http.Client {
return &http.Client{
Transport: NewMatchingByEndpointRoundTripper(r),
}
}

// NewRoundTripper initializes a new roundtripper and accepts multiple Response
// structures as variadric arguments.
func NewMatchingByEndpointRoundTripper(r map[string][]Response) *MatchingByEndpointRoundTripper {
responsesByEndpoint := make(map[string]*RoundTripper)

for endpoint, responses := range r {
responsesByEndpoint[endpoint] = &RoundTripper{
Responses: responses,
}
}

return &MatchingByEndpointRoundTripper{
ResponsesByEndpoint: responsesByEndpoint,
}
}

// MatchingByEndpointRoundTripper is aimed to be used as the Transport property in an http.Client
// in order to mock the responses that it would return in the normal execution, matching by endpoint.
// If the number of responses that are mocked for a specific endpoint are not enough, an error with the
// request iteration ID, method and full URL is returned.
type MatchingByEndpointRoundTripper struct {
ResponsesByEndpoint map[string]*RoundTripper
}

// RoundTrip executes a single HTTP transaction, returning
// a Response for the provided Request, based on the Request's URL.
func (rt *MatchingByEndpointRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for endpoint, rt := range rt.ResponsesByEndpoint {
endpointRegex := regexp.MustCompile(endpoint)

if endpointRegex.MatchString(req.URL.Path) {
return rt.RoundTrip(req)
}
}

return nil, fmt.Errorf(
"failed to obtain response for request: %s %s", req.Method, req.URL,
)
}

// NewClient returns a pointer to http.Client with the mocked Transport.
func NewClient(r ...Response) *http.Client {
return &http.Client{
Expand Down
Loading
Loading