To test JWKS fetching without a real network, implement the HTTPClient interface. The FetchJWKS function and the WithHTTPClient option both accept any type that implements the Get(string) (*http.Response, error) method.
By providing a fake client, you can simulate various network conditions, such as:
- Returning specific HTTP status codes (e.g., 401, 500).
- Returning large bodies to test
MaxJWKSSize (1 MB). - Verifying that certain URLs were (or were not) requested before an error occurred.
type recordingClient struct {
status int
body string
requested []string
}
func (c *recordingClient) Get(url string) (*http.Response, error) {
c.requested = append(c.requested, url)
status := c.status
if status == 0 {
status = http.StatusOK
}
return &http.Response{StatusCode: status,
Body: io.NopCloser(strings.NewReader(c.body))}, nil
}