Skip to content

Commit

Permalink
added monitor UI support for mock mode
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Nov 18, 2023
1 parent 0fe90f6 commit d3989d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion daemon/build_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func BuildHttpTransaction(build HttpTransactionConfig) *HttpTransaction {
replaced := config.RewritePath(build.NewRequest.URL.Path, cf)
var newUrl = build.NewRequest.URL
if replaced != "" {
newUrl, _ = url.Parse(replaced)
var e error
newUrl, e = url.Parse(replaced)
if e != nil {
newUrl = build.NewRequest.URL
pterm.Error.Printf("major configuration problem: cannot parse URL: `%s`: %s", replaced, e.Error())
}
if build.NewRequest.URL.RawQuery != "" {
newUrl.RawQuery = build.NewRequest.URL.RawQuery
}
Expand All @@ -155,6 +160,12 @@ func BuildHttpTransaction(build HttpTransactionConfig) *HttpTransaction {
}

func ReconstructURL(r *http.Request, protocol, host, basepath string, port string) string {
if host == "" {
host = r.Host
}
if protocol == "" {
protocol = "http"
}
url := fmt.Sprintf("%s://%s", protocol, host)
if port != "" {
url += fmt.Sprintf(":%s", port)
Expand Down
14 changes: 14 additions & 0 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package daemon

import (
"bytes"
_ "embed"
"fmt"
"io"
Expand Down Expand Up @@ -248,9 +249,19 @@ func (ws *WiretapService) handleMockRequest(
setCORSHeaders(headers)
headers["Content-Type"] = "application/json"

buff := bytes.NewBuffer(mock)

// create a simulated response to send up to the monitor UI.
resp := &http.Response{
StatusCode: mockStatus,
Body: io.NopCloser(buff),
}
header := http.Header{}
resp.Header = header
// write headers
for k, v := range headers {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(v))
header.Add(k, fmt.Sprint(v))
}

// if there was an error building the mock, return a 404
Expand All @@ -262,6 +273,9 @@ func (ws *WiretapService) handleMockRequest(
return
}

// validate response async
ws.broadcastResponse(request, resp)

// if the mock is empty
request.HttpResponseWriter.WriteHeader(mockStatus)
_, errs := request.HttpResponseWriter.Write(mock)
Expand Down

0 comments on commit d3989d6

Please sign in to comment.