-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor WireMock mappings/files, reset before tests
- Loading branch information
Showing
16 changed files
with
173 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package wiremock | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
// Client provides communciation with the admin API of a WireMock server | ||
// instance. | ||
type Client struct { | ||
host string | ||
cl *retryablehttp.Client | ||
} | ||
|
||
// NewManagementAPIClient creates a Go Auth0 management API client configured to | ||
// talk to this WireMock server instance. | ||
func (c *Client) NewManagementAPIClient() (*management.Management, error) { | ||
return management.New(c.host, management.WithInsecure()) | ||
} | ||
|
||
// ResetAllMappings causes the WireMock server to remove transient mappings and | ||
// reload static mappings from disk. | ||
func (c *Client) ResetAllMappings(ctx context.Context) (err error) { | ||
resp, err := c.doRequest(ctx, http.MethodPost, "__admin/mappings/reset") | ||
if err != nil { | ||
return fmt.Errorf("failed to reset WireMock mappings: %w", err) | ||
} | ||
defer func() { | ||
err = multierror.Append(err, resp.Body.Close()).ErrorOrNil() | ||
}() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("the WireMock server returned %s when trying to reset mappings", resp.Status) | ||
} | ||
return nil | ||
} | ||
|
||
// ResetAllScenarios causes the WireMock server to put all currently defined | ||
// mappings' scenarios back into their initial state. | ||
func (c *Client) ResetAllScenarios(ctx context.Context) (err error) { | ||
resp, err := c.doRequest(ctx, http.MethodPost, "__admin/scenarios/reset") | ||
if err != nil { | ||
return fmt.Errorf("failed to reset WireMock scenarios: %w", err) | ||
} | ||
defer func() { | ||
err = multierror.Append(err, resp.Body.Close()).ErrorOrNil() | ||
}() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("the WireMock server returned %s when trying to reset scenarios", resp.Status) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) doRequest(ctx context.Context, method, path string) (*http.Response, error) { | ||
u := &url.URL{ | ||
Scheme: "http", | ||
Host: c.host, | ||
Path: path, | ||
} | ||
|
||
req, err := retryablehttp.NewRequest(method, u.String(), nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create HTTP request: %w", err) | ||
} | ||
|
||
resp, err := c.cl.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to send HTTP request to WireMock: %w", err) | ||
} | ||
return resp, nil | ||
} | ||
|
||
// NewClient constructs a WireMock client that communicates with the WireMock | ||
// server on the given host and port. | ||
func NewClient(host string) *Client { | ||
cl := retryablehttp.NewClient() | ||
cl.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy | ||
|
||
return &Client{ | ||
host: host, | ||
cl: cl, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...emock/__files/custom_domain_verified.json → ...h0_managed_certs/custom_domain_ready.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ck/mappings/delete_custom_domain_204.json → ...naged_certs/delete_custom_domain_204.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ain_verification/with_auth0_managed_certs/get_custom_domain_pending_verification_200.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scenarioName": "Custom domain verification with Auth0-managed certificates", | ||
"requiredScenarioState": "Started", | ||
"request": { | ||
"method": "GET", | ||
"url": "/api/v2/custom-domains/cd_auth0managed" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_pending_verification.json", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ings/custom_domain_verification/with_auth0_managed_certs/get_custom_domain_ready_200.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scenarioName": "Custom domain verification with Auth0-managed certificates", | ||
"requiredScenarioState": "Verified", | ||
"request": { | ||
"method": "GET", | ||
"url": "/api/v2/custom-domains/cd_auth0managed" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_ready.json", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...gs/custom_domain_verification/with_auth0_managed_certs/post_custom_domain_verify_200.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"scenarioName": "Custom domain verification with Auth0-managed certificates", | ||
"requiredScenarioState": "Started", | ||
"newScenarioState": "Verified", | ||
"request": { | ||
"method": "POST", | ||
"url": "/api/v2/custom-domains/cd_auth0managed/verify" | ||
}, | ||
"response": { | ||
"status": 201, | ||
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_ready.json", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
} | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
dockerfiles/wiremock/mappings/get_custom_domain_pending_200.json
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
dockerfiles/wiremock/mappings/get_custom_domain_ready_200.json
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
dockerfiles/wiremock/mappings/post_custom_domain_verify_200.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters