From d3989d65eecfa2dc7f3c4903bced6bd32536743b Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 18 Nov 2023 15:12:11 -0500 Subject: [PATCH] added monitor UI support for mock mode Signed-off-by: quobix --- daemon/build_request.go | 13 ++++++++++++- daemon/handle_request.go | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/daemon/build_request.go b/daemon/build_request.go index c1790ce..a607042 100644 --- a/daemon/build_request.go +++ b/daemon/build_request.go @@ -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 } @@ -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) diff --git a/daemon/handle_request.go b/daemon/handle_request.go index 765a782..09fe216 100644 --- a/daemon/handle_request.go +++ b/daemon/handle_request.go @@ -4,6 +4,7 @@ package daemon import ( + "bytes" _ "embed" "fmt" "io" @@ -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 @@ -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)