Skip to content

Commit

Permalink
Make mock response field example usage configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsirbe authored and daveshanley committed Jun 14, 2024
1 parent 97a717c commit 0ac01b6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 46 deletions.
11 changes: 11 additions & 0 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (

// mock mode
var mockMode bool
var useAllMockResponseFields bool

certFlag, _ := cmd.Flags().GetString("cert")
if certFlag != "" {
Expand All @@ -82,6 +83,7 @@ var (

debug, _ := cmd.Flags().GetBool("debug")
mockMode, _ = cmd.Flags().GetBool("mock-mode")
useAllMockResponseFields, _ = cmd.Flags().GetBool("enable-all-mock-response-fields")
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 @@ -182,6 +184,11 @@ var (
config.MockMode = true
}
}
if useAllMockResponseFields {
if !config.UseAllMockResponseFields {
config.UseAllMockResponseFields = true
}
}
if streamReport {
if !config.StreamReport {
config.StreamReport = true
Expand Down Expand Up @@ -217,6 +224,9 @@ var (
if mockMode {
config.MockMode = true
}
if useAllMockResponseFields {
config.UseAllMockResponseFields = true
}
if streamReport {
config.StreamReport = true
}
Expand Down Expand Up @@ -654,6 +664,7 @@ func Execute(version, commit, date string, fs embed.FS) {
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().BoolP("enable-all-mock-response-fields", "o", true, "Enable usage of all property examples in mock responses. When set to false, only required field examples will be used.")
rootCmd.Flags().StringP("config", "c", "", "Location of wiretap configuration file to use (default is .wiretap in current directory)")
rootCmd.Flags().StringP("base", "b", "", "Set a base path to resolve relative file references from, or a overriding base URL to resolve remote references from")
rootCmd.Flags().BoolP("debug", "l", false, "Enable debug logging")
Expand Down
3 changes: 2 additions & 1 deletion daemon/wiretap_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func NewWiretapService(document libopenapi.Document, config *shared.WiretapConfi
}

// create a new mock engine
wts.mockEngine = mock.NewMockEngine(wts.docModel, config.MockModePretty)
wts.mockEngine = mock.NewMockEngine(wts.docModel, config.MockModePretty,
config.UseAllMockResponseFields)

// hard-wire the config, change this later if needed.
wts.config = config
Expand Down
7 changes: 5 additions & 2 deletions mock/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ type ResponseMockEngine struct {
pretty bool
}

func NewMockEngine(document *v3.Document, pretty bool) *ResponseMockEngine {
func NewMockEngine(document *v3.Document, pretty, useAllPropertyExamples bool) *ResponseMockEngine {
me := renderer.NewMockGenerator(renderer.JSON)
if pretty {
me.SetPretty()
}
me.DisableRequiredCheck()

if useAllPropertyExamples {
me.DisableRequiredCheck()
}

return &ResponseMockEngine{
doc: document,
Expand Down
Loading

0 comments on commit 0ac01b6

Please # to comment.