Skip to content

Commit

Permalink
review comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik authored and tommysitu committed Oct 10, 2024
1 parent cd8a005 commit c81caa0
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/handlers/v2/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ModeArgumentsView struct {
Stateful bool `json:"stateful,omitempty"`
OverwriteDuplicate bool `json:"overwriteDuplicate,omitempty"`
CaptureOnMiss bool `json:"captureOnMiss,omitempty"`
CaptureDelay bool `json:"captureDelay,omitempty"`
}

type IsWebServerView struct {
Expand Down
2 changes: 1 addition & 1 deletion core/hoverfly_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (hf *Hoverfly) DoRequest(request *http.Request) (*http.Response, *time.Dura
}
start := time.Now()
resp, err := client.Do(request)
elapsed := time.Since(start)

if err != nil {
return nil, nil, err
Expand All @@ -42,7 +43,6 @@ func (hf *Hoverfly) DoRequest(request *http.Request) (*http.Response, *time.Dura
resp.Header.Add("Hoverfly", "Forwarded")
}

elapsed := time.Since(start)
return resp, &elapsed, nil
}

Expand Down
1 change: 1 addition & 0 deletions core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (hf *Hoverfly) SetModeWithArguments(modeView v2.ModeView) error {
Stateful: modeView.Arguments.Stateful,
OverwriteDuplicate: modeView.Arguments.OverwriteDuplicate,
CaptureOnMiss: modeView.Arguments.CaptureOnMiss,
CaptureDelay: modeView.Arguments.CaptureDelay,
}

hf.modeMap[hf.Cfg.GetMode()].SetArguments(modeArguments)
Expand Down
8 changes: 7 additions & 1 deletion core/modes/capture_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (this *CaptureMode) View() v2.ModeView {
Headers: this.Arguments.Headers,
Stateful: this.Arguments.Stateful,
OverwriteDuplicate: this.Arguments.OverwriteDuplicate,
CaptureDelay: this.Arguments.CaptureDelay,
},
}
}
Expand Down Expand Up @@ -68,11 +69,16 @@ func (this CaptureMode) Process(request *http.Request, details models.RequestDet
respBody, _ := util.GetResponseBody(response)
respHeaders := util.GetResponseHeaders(response)

delayInMs := 0
if this.Arguments.CaptureDelay {
delayInMs = int(duration.Milliseconds())
}

responseObj := &models.ResponseDetails{
Status: response.StatusCode,
Body: respBody,
Headers: respHeaders,
FixedDelay: int(duration.Milliseconds()),
FixedDelay: delayInMs,
}

if this.Arguments.Headers == nil {
Expand Down
9 changes: 4 additions & 5 deletions core/modes/diff_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (this *DiffMode) Process(request *http.Request, details models.RequestDetai
return ReturnErrorAndLog(request, err, &actualPair, "There was an error when reconstructing the request.", Diff)
}

actualResponse, duration, err := this.Hoverfly.DoRequest(modifiedRequest)
actualResponse, _, err := this.Hoverfly.DoRequest(modifiedRequest)
if err != nil {
return ReturnErrorAndLog(request, err, &actualPair, "There was an error when forwarding the request to the intended destination", Diff)
}
Expand All @@ -74,10 +74,9 @@ func (this *DiffMode) Process(request *http.Request, details models.RequestDetai
respHeaders := util.GetResponseHeaders(actualResponse)

actualResponseDetails := &models.ResponseDetails{
Status: actualResponse.StatusCode,
Body: respBody,
Headers: respHeaders,
FixedDelay: int(duration.Milliseconds()),
Status: actualResponse.StatusCode,
Body: respBody,
Headers: respHeaders,
}

this.diffResponse(simResponse, actualResponseDetails, this.Arguments.Headers)
Expand Down
1 change: 1 addition & 0 deletions core/modes/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ModeArguments struct {
Stateful bool
OverwriteDuplicate bool
CaptureOnMiss bool
CaptureDelay bool
}

type ProcessResult struct {
Expand Down
9 changes: 4 additions & 5 deletions core/modes/modify_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (this ModifyMode) Process(request *http.Request, details models.RequestDeta
return ReturnErrorAndLog(request, err, &pair, "There was an error when rebuilding the modified http request", Modify)
}

resp, duration, err := this.Hoverfly.DoRequest(modifiedRequest)
resp, _, err := this.Hoverfly.DoRequest(modifiedRequest)
if err != nil {
return ReturnErrorAndLog(request, err, &pair, "There was an error when forwarding the request to the intended destination", Modify)
}
Expand All @@ -48,10 +48,9 @@ func (this ModifyMode) Process(request *http.Request, details models.RequestDeta
}

pair.Response = models.ResponseDetails{
Status: resp.StatusCode,
Body: string(bodyBytes),
Headers: resp.Header,
FixedDelay: int(duration.Milliseconds()),
Status: resp.StatusCode,
Body: string(bodyBytes),
Headers: resp.Header,
}

pair, err = this.Hoverfly.ApplyMiddleware(pair)
Expand Down
9 changes: 7 additions & 2 deletions core/modes/spy_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (this *SpyMode) View() v2.ModeView {
Stateful: this.Arguments.Stateful,
Headers: this.Arguments.Headers,
OverwriteDuplicate: this.Arguments.OverwriteDuplicate,
CaptureDelay: this.Arguments.CaptureDelay,
},
}
}
Expand All @@ -51,6 +52,7 @@ func (this *SpyMode) SetArguments(arguments ModeArguments) {
Stateful: arguments.Stateful,
OverwriteDuplicate: arguments.OverwriteDuplicate,
CaptureOnMiss: arguments.CaptureOnMiss,
CaptureDelay: arguments.CaptureDelay,
}
}

Expand All @@ -74,12 +76,15 @@ func (this SpyMode) Process(request *http.Request, details models.RequestDetails
if this.Arguments.CaptureOnMiss {
respBody, _ := util.GetResponseBody(response)
respHeaders := util.GetResponseHeaders(response)

delayInMs := 0
if this.Arguments.CaptureDelay {
delayInMs = int(duration.Milliseconds())
}
responseObj := &models.ResponseDetails{
Status: response.StatusCode,
Body: respBody,
Headers: respHeaders,
FixedDelay: int(duration.Milliseconds()),
FixedDelay: delayInMs,
}
if this.Arguments.Headers == nil {
this.Arguments.Headers = []string{}
Expand Down
10 changes: 10 additions & 0 deletions hoverctl/cmd/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var stateful bool
var overwriteDuplicate bool
var matchingStrategy string
var captureOnMiss bool
var captureDelay bool

var modeCmd = &cobra.Command{
Use: "mode [capture|diff|simulate|spy|modify|synthesize (optional)]",
Expand Down Expand Up @@ -51,6 +52,7 @@ mode is shown.
case modes.Capture:
modeView.Arguments.Stateful = stateful
modeView.Arguments.OverwriteDuplicate = overwriteDuplicate
modeView.Arguments.CaptureDelay = captureDelay
setHeaderArgument(modeView)
break
case modes.Diff:
Expand All @@ -61,6 +63,7 @@ mode is shown.
modeView.Arguments.Stateful = stateful
modeView.Arguments.OverwriteDuplicate = overwriteDuplicate
modeView.Arguments.CaptureOnMiss = captureOnMiss
modeView.Arguments.CaptureDelay = captureDelay
setHeaderArgument(modeView)
break
}
Expand Down Expand Up @@ -98,6 +101,9 @@ func getExtraInfo(mode *v2.ModeView) string {
extraInfo = fmt.Sprintf("and will capture the following request headers: %s", mode.Arguments.Headers)
}
}
if captureDelay {
extraInfo = fmt.Sprintf(" and will also capture the delay")
}
break
case modes.Diff:
if len(mode.Arguments.Headers) > 0 {
Expand All @@ -119,6 +125,9 @@ func getExtraInfo(mode *v2.ModeView) string {
extraInfo = fmt.Sprintf("and also will capture the following request headers: %s", mode.Arguments.Headers)
}
}
if captureDelay {
extraInfo = fmt.Sprintf(" and will also capture the delay")
}
break
}

Expand All @@ -139,4 +148,5 @@ func init() {
modeCmd.PersistentFlags().BoolVar(&overwriteDuplicate, "overwrite-duplicate", false,
"Overwrite duplicate requests in capture mode")
modeCmd.PersistentFlags().BoolVar(&captureOnMiss, "capture-on-miss", false, "Capture the request on miss in spy mode")
modeCmd.PersistentFlags().BoolVar(&captureDelay, "capture-delay", false, "Capture the request delay in capture and spy mode")
}

0 comments on commit c81caa0

Please sign in to comment.