Skip to content

Commit

Permalink
Mock mode works!
Browse files Browse the repository at this point in the history
I cannot wait to try this out.,

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Sep 2, 2023
1 parent 4808a61 commit 888df2b
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 549 deletions.
29 changes: 25 additions & 4 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var (
var hardErrorCode int
var hardErrorReturnCode int

// mock mode
var mockMode bool

certFlag, _ := cmd.Flags().GetString("cert")
if certFlag != "" {
cert = certFlag
Expand All @@ -64,6 +67,7 @@ var (
certKey = keyFlag
}

mockMode, _ = cmd.Flags().GetBool("mock-mode")
hardError, _ = cmd.Flags().GetBool("hard-validation")
hardErrorCode, _ = cmd.Flags().GetInt("hard-validation-code")
hardErrorReturnCode, _ = cmd.Flags().GetInt("hard-validation-return-code")
Expand Down Expand Up @@ -159,12 +163,21 @@ var (
if spec == "" {
pterm.Println()
pterm.Warning.Println("No OpenAPI specification provided. " +
"Please provide a path to an OpenAPI specification using the --spec or -s flags.")
pterm.Warning.Println("Without an OpenAPI specification, wiretap will not be able to validate " +
"Please provide a path to an OpenAPI specification using the --spec or -s flags. \n" +
"Without an OpenAPI specification, wiretap will not be able to validate " +
"requests and responses")
pterm.Println()
}

if mockMode && spec == "" {
pterm.Println()
pterm.Error.Println("Cannot enable mock mode, no OpenAPI specification provided!\n" +
"Please provide a path to an OpenAPI specification using the --spec or -s flags.\n" +
"Without an OpenAPI specification, wiretap will not be able to generate mock responses")
pterm.Println()
return nil
}

if redirectURL == "" {
pterm.Println()
pterm.Error.Println("No redirect URL provided. " +
Expand Down Expand Up @@ -285,10 +298,17 @@ var (
pterm.Println()
}

// mock mode
if config.MockMode {
pterm.Printf("Ⓜ️ %s. All responses will be mocked and no traffic will be sent to the target API.\n",
pterm.LightCyan("Mock mode enabled"))
pterm.Println()
}

// using TLS?
if config.CertificateKey != "" && config.Certificate != "" {
pterm.Printf("🔐 Running over %s using certificate: %s and key: %s\n",
pterm.LightYellow("TLS/HTTPS"), pterm.LightMagenta(config.Certificate), pterm.LightCyan(config.CertificateKey))
pterm.LightYellow("TLS/HTTPS & HTTP/2"), pterm.LightMagenta(config.Certificate), pterm.LightCyan(config.CertificateKey))
pterm.Println()
}

Expand Down Expand Up @@ -323,9 +343,10 @@ func Execute(version, commit, date string, fs embed.FS) {
rootCmd.Flags().StringP("static-index", "i", "index.html", "Set the index filename for static file serving (default is index.html)")
rootCmd.Flags().StringP("cert", "n", "", "Set the path to the TLS certificate to use for TLS/HTTPS")
rootCmd.Flags().StringP("key", "k", "", "Set the path to the TLS certificate key to use for TLS/HTTPS")
rootCmd.Flags().BoolP("hard-validation", "e", false, "Return a hard error for non-compliant request/response, default code is 400")
rootCmd.Flags().BoolP("hard-validation", "e", false, "Return a HTTP error for non-compliant request/response")
rootCmd.Flags().IntP("hard-validation-code", "q", 400, "Set a custom http error code for non-compliant requests when using the hard-error flag")
rootCmd.Flags().IntP("hard-validation-return-code", "y", 502, "Set a custom http error code for non-compliant responses when using the hard-error flag")
rootCmd.Flags().BoolP("mock-mode", "x", false, "Run in mock mode, responses are mocked and no traffic is sent to the target API (requires OpenAPI spec)")

rootCmd.Flags().StringP("config", "c", "",
"Location of wiretap configuration file to use (default is .wiretap in current directory)")
Expand Down
2 changes: 1 addition & 1 deletion daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (ws *WiretapService) callAPI(req *http.Request) (*http.Response, error) {
if req.Header.Get("Referer") != "" {
// retain original referer for logging
req.Header.Set("X-Original-Referer", req.Header.Get("Referer"))
req.Header.Set("Referer", reconstructURL(req,
req.Header.Set("Referer", ReconstructURL(req,
wiretapConfig.RedirectProtocol,
wiretapConfig.RedirectHost,
wiretapConfig.RedirectPort))
Expand Down
128 changes: 0 additions & 128 deletions daemon/build_mock.go

This file was deleted.

Loading

0 comments on commit 888df2b

Please sign in to comment.