diff --git a/core/action/action.go b/core/action/action.go index 3416a0862..508353ed0 100644 --- a/core/action/action.go +++ b/core/action/action.go @@ -14,7 +14,9 @@ import ( "time" v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2" + "github.com/SpectoLabs/hoverfly/core/journal" "github.com/SpectoLabs/hoverfly/core/models" + "github.com/pborman/uuid" log "github.com/sirupsen/logrus" ) @@ -117,7 +119,7 @@ func (action *Action) GetActionView(actionName string) v2.ActionView { } } -func (action *Action) Execute(pair *models.RequestResponsePair) error { +func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel chan string, journal *journal.Journal) error { pairViewBytes, err := json.Marshal(pair.ConvertToRequestResponsePairView()) if err != nil { @@ -131,6 +133,9 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error { //adding 200 ms to include some buffer for it to return response time.Sleep(time.Duration(200+action.DelayInMs) * time.Millisecond) + journalID := <-journalIDChannel + log.Info("Journal ID received ", journalID) + //if it is remote callback if action.Remote != "" { @@ -142,7 +147,10 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error { return err } + correlationID := uuid.New() + invokedTime := time.Now() req.Header.Add("Content-Type", "application/json") + req.Header.Add("X-CORRELATION-ID", correlationID) resp, err := http.DefaultClient.Do(req) if err != nil { @@ -152,16 +160,24 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error { return err } + completionTime := time.Now() + journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, correlationID, invokedTime, completionTime, resp.StatusCode) if resp.StatusCode != 200 { log.Error("Remote post serve action did not process payload") + return nil } log.Info("Remote post serve action invoked successfully") return nil } + invokedTime := time.Now() actionCommand := exec.Command(action.Binary, action.Script.Name()) actionCommand.Stdin = bytes.NewReader(pairViewBytes) + completionTime := time.Now() + + journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, "", invokedTime, completionTime, 0) + var stdout bytes.Buffer var stderr bytes.Buffer actionCommand.Stdout = &stdout diff --git a/core/action/action_test.go b/core/action/action_test.go index 4d48d1383..3eced5196 100644 --- a/core/action/action_test.go +++ b/core/action/action_test.go @@ -1,6 +1,7 @@ package action_test import ( + "github.com/SpectoLabs/hoverfly/core/journal" "github.com/gorilla/mux" "net/http" "net/http/httptest" @@ -110,7 +111,11 @@ func Test_ExecuteLocalPostServeAction(t *testing.T) { originalPair := models.RequestResponsePair{Response: resp, Request: req} - err = newAction.Execute(&originalPair) + //not adding entry as update journal method will be tested in its file + journalIDChannel := make(chan string) + newJournal := journal.NewJournal() + journalIDChannel <- "1" + err = newAction.Execute(&originalPair, journalIDChannel, newJournal) Expect(err).To(BeNil()) } @@ -126,10 +131,14 @@ func Test_ExecuteRemotePostServeAction(t *testing.T) { Body: "Normal body", }, } + //not adding entry as update journal method will be tested in its file + journalIDChannel := make(chan string) + newJournal := journal.NewJournal() + journalIDChannel <- "1" newAction, err := action.NewRemoteAction("test-callback", server.URL+"/process", 0) Expect(err).To(BeNil()) - err = newAction.Execute(&originalPair) + err = newAction.Execute(&originalPair, journalIDChannel, newJournal) Expect(err).To(BeNil()) } @@ -143,7 +152,11 @@ func Test_ExecuteRemotePostServeAction_WithUnReachableHost(t *testing.T) { newAction, err := action.NewRemoteAction("test-callback", "http://test", 0) Expect(err).To(BeNil()) - err = newAction.Execute(&originalPair) + //not adding entry as update journal method will be tested in its file + journalIDChannel := make(chan string) + newJournal := journal.NewJournal() + journalIDChannel <- "1" + err = newAction.Execute(&originalPair, journalIDChannel, newJournal) Expect(err).NotTo(BeNil()) } diff --git a/core/benchmark_test.go b/core/benchmark_test.go index b00476c73..bdf681f76 100644 --- a/core/benchmark_test.go +++ b/core/benchmark_test.go @@ -181,7 +181,7 @@ func BenchmarkProcessRequest(b *testing.B) { b.Run(bm.name, func(b *testing.B) { for n := 0; n < b.N; n++ { - resp = hoverfly.processRequest(request) + resp, _ = hoverfly.processRequest(request) } }) } diff --git a/core/handlers/v2/views.go b/core/handlers/v2/views.go index c973a111e..abfd11756 100644 --- a/core/handlers/v2/views.go +++ b/core/handlers/v2/views.go @@ -93,12 +93,21 @@ type JournalView struct { } type JournalEntryView struct { - Request RequestDetailsView `json:"request"` - Response ResponseDetailsView `json:"response"` - Mode string `json:"mode"` - TimeStarted string `json:"timeStarted"` - Latency float64 `json:"latency"` - Id string `json:"id"` + Request RequestDetailsView `json:"request"` + Response ResponseDetailsView `json:"response"` + Mode string `json:"mode"` + TimeStarted string `json:"timeStarted"` + Latency float64 `json:"latency"` + Id string `json:"id"` + PostServeActionEntry *PostServeActionEntryView `json:"postServeAction,omitEmpty"` +} + +type PostServeActionEntryView struct { + ActionName string `json:"actionName"` + InvokedTime string `json:"invoked"` + CompletedTime string `json:"completed"` + CorrelationId string `json:"correlationId,omitempty"` + HttpStatus int `json:"status,omitempty"` } type JournalEntryFilterView struct { diff --git a/core/hoverfly.go b/core/hoverfly.go index bb7ade90b..2dcd22abe 100644 --- a/core/hoverfly.go +++ b/core/hoverfly.go @@ -185,16 +185,16 @@ func (hf *Hoverfly) StopProxy() { // processRequest - processes incoming requests and based on proxy state (record/playback) // returns HTTP response. -func (hf *Hoverfly) processRequest(req *http.Request) *http.Response { +func (hf *Hoverfly) processRequest(req *http.Request) (*http.Response, chan string) { if hf.Cfg.CORS.Enabled { response := hf.Cfg.CORS.InterceptPreflightRequest(req) if response != nil { - return response + return response, nil } } requestDetails, err := models.NewRequestDetailsFromHttpRequest(req) if err != nil { - return modes.ErrorResponse(req, err, "Could not interpret HTTP request").Response + return modes.ErrorResponse(req, err, "Could not interpret HTTP request").Response, nil } modeName := hf.Cfg.GetMode() @@ -208,7 +208,7 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response { // and definitely don't delay people in capture mode // Don't delete the error if err != nil || modeName == modes.Capture { - return result.Response + return result.Response, nil } if result.IsResponseDelayable() { @@ -220,12 +220,15 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response { } if result.PostServeActionInputDetails != nil { + + journalIDChannel := make(chan string) if postServeAction, ok := hf.PostServeActionDetails.Actions[result.PostServeActionInputDetails.PostServeAction]; ok { - go postServeAction.Execute(result.PostServeActionInputDetails.Pair) + go postServeAction.Execute(result.PostServeActionInputDetails.Pair, journalIDChannel, hf.Journal) } + return result.Response, journalIDChannel } - return result.Response + return result.Response, nil } func (hf *Hoverfly) applyResponseDelay(result modes.ProcessResult) { diff --git a/core/hoverfly_service.go b/core/hoverfly_service.go index 0ebe60124..6c2b12563 100644 --- a/core/hoverfly_service.go +++ b/core/hoverfly_service.go @@ -481,6 +481,7 @@ func (hf *Hoverfly) SetLocalPostServeAction(actionName string, binary string, sc if err != nil { return err } + log.Info("Local post serve action is set") return nil } @@ -494,6 +495,7 @@ func (hf *Hoverfly) SetRemotePostServeAction(actionName, remote string, delayInM if err != nil { return err } + log.Info("Remote post serve action is set") return nil } diff --git a/core/hoverfly_test.go b/core/hoverfly_test.go index 807f3be24..8a387155e 100644 --- a/core/hoverfly_test.go +++ b/core/hoverfly_test.go @@ -23,53 +23,37 @@ const pythonMiddlewareBasic = "import sys\nprint(sys.stdin.readlines()[0])" const pythonModifyResponse = "#!/usr/bin/env python\n" + "import sys\n" + "import json\n" + - "def main():\n" + " data = sys.stdin.readlines()\n" + " payload = data[0]\n" + - " payload_dict = json.loads(payload)\n" + - " payload_dict['response']['status'] = 201\n" + " payload_dict['response']['body'] = \"body was replaced by middleware\"\n" + - " print(json.dumps(payload_dict))\n" + - "if __name__ == \"__main__\":\n" + " main()\n" const rubyModifyResponse = "#!/usr/bin/env ruby\n" + "# encoding: utf-8\n\n" + - "require 'rubygems'\n" + "require 'json'\n\n" + - "while payload = STDIN.gets\n" + " next unless payload\n\n" + - " jsonPayload = JSON.parse(payload)\n\n" + - " jsonPayload[\"response\"][\"body\"] = \"body was replaced by middleware\\n\"\n\n" + - " STDOUT.puts jsonPayload.to_json\n\n" + - "end" const pythonReflectBody = "#!/usr/bin/env python\n" + "import sys\n" + "import json\n" + - "def main():\n" + " data = sys.stdin.readlines()\n" + " payload = data[0]\n" + - " payload_dict = json.loads(payload)\n" + - " payload_dict['response']['status'] = 201\n" + " payload_dict['response']['body'] = payload_dict['request']['body']\n" + - " print(json.dumps(payload_dict))\n" + - "if __name__ == \"__main__\":\n" + " main()\n" @@ -152,7 +136,7 @@ func Test_Hoverfly_processRequest_CaptureModeReturnsResponseAndSavesIt(t *testin unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -172,14 +156,14 @@ func Test_Hoverfly_processRequest_CanSimulateRequest(t *testing.T) { // capturing unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) // virtualizing unit.Cfg.SetMode("simulate") - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp).ToNot(BeNil()) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -196,14 +180,14 @@ func Test_Hoverfly_processRequest_CanSimulateRequestInSpyMode(t *testing.T) { // capturing unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) // virtualizing unit.Cfg.SetMode("spy") - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp).ToNot(BeNil()) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -221,7 +205,7 @@ func Test_Hoverfly_processRequest_CanSpyRequest(t *testing.T) { // virtualizing unit.Cfg.SetMode("spy") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -246,7 +230,7 @@ func Test_Hoverfly_processRequest_CanUseMiddlewareToSynthesizeResponse(t *testin Expect(err).To(BeNil()) unit.Cfg.SetMode("synthesize") - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp).ToNot(BeNil()) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -271,7 +255,7 @@ func Test_Hoverfly_processRequest_CanModifyResponse(t *testing.T) { Expect(err).To(BeNil()) unit.Cfg.SetMode("modify") - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp).ToNot(BeNil()) @@ -332,7 +316,7 @@ func Test_Hoverfly_processRequest_DelayAppliedToSuccessfulSimulateRequest(t *tes // capturing unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -344,7 +328,7 @@ func Test_Hoverfly_processRequest_DelayAppliedToSuccessfulSimulateRequest(t *tes stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -369,7 +353,7 @@ func Test_Hoverfly_processRequest_DelayNotAppliedToFailedSimulateRequest(t *test stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusBadGateway)) @@ -393,7 +377,7 @@ func Test_Hoverfly_processRequest_DelayNotAppliedToCaptureRequest(t *testing.T) stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -423,7 +407,7 @@ func Test_Hoverfly_processRequest_DelayAppliedToSynthesizeRequest(t *testing.T) unit.Simulation.ResponseDelays = &stub stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -454,7 +438,7 @@ func Test_Hoverfly_processRequest_DelayNotAppliedToFailedSynthesizeRequest(t *te unit.Simulation.ResponseDelays = &stub stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusBadGateway)) @@ -483,7 +467,7 @@ func Test_Hoverfly_processRequest_DelayAppliedToSuccessfulMiddleware(t *testing. unit.Simulation.ResponseDelays = &stub stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -511,7 +495,7 @@ func Test_Hoverfly_processRequest_DelayNotAppliedToFailedModifyRequest(t *testin unit.Simulation.ResponseDelays = &stub stubLogNormal := ResponseDelayLogNormalListStub{} unit.Simulation.ResponseDelaysLogNormal = &stubLogNormal - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp.StatusCode).To(Equal(http.StatusBadGateway)) @@ -529,7 +513,7 @@ func Test_Hoverfly_processRequest_CanHandleResponseDiff(t *testing.T) { // capturing expectedUnit.Cfg.SetMode("capture") - resp := expectedUnit.processRequest(r) + resp, _ := expectedUnit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -541,7 +525,7 @@ func Test_Hoverfly_processRequest_CanHandleResponseDiff(t *testing.T) { // comparing actualUnit.Cfg.SetMode("diff") actualUnit.Simulation = expectedUnit.Simulation - newResp := actualUnit.processRequest(r) + newResp, _ := actualUnit.processRequest(r) Expect(newResp).ToNot(BeNil()) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -569,7 +553,7 @@ func Test_Hoverfly_processRequest_CanHandlePreflightRequestWhenCORSEnabled(t *te r.Header.Set("Access-Control-Request-Headers", "X-PINGOTHER,Content-Type") unit.Cfg.CORS = *cors.DefaultCORSConfigs() - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusOK)) @@ -596,7 +580,7 @@ func Test_Hoverfly_processRequest_IgnoreInvalidPreflightRequestWhenCORSEnabled(t unit.Cfg.CORS = *cors.DefaultCORSConfigs() unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -616,7 +600,7 @@ func Test_Hoverfly_processRequest_AddCORSHeadersToResponseWhenCORSEnabled(t *tes // capturing unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -624,7 +608,7 @@ func Test_Hoverfly_processRequest_AddCORSHeadersToResponseWhenCORSEnabled(t *tes // virtualizing unit.Cfg.CORS = *cors.DefaultCORSConfigs() unit.Cfg.SetMode("simulate") - newResp := unit.processRequest(r) + newResp, _ := unit.processRequest(r) Expect(newResp).ToNot(BeNil()) Expect(newResp.StatusCode).To(Equal(http.StatusCreated)) @@ -645,7 +629,7 @@ func Test_Hoverfly_processRequest_ShouldNotAddCORSHeadersIfRequestHasNoOriginWhe // capturing unit.Cfg.CORS = *cors.DefaultCORSConfigs() unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) @@ -665,7 +649,7 @@ func Test_Hoverfly_processRequest_ShouldNotCaptureCORSHeadersAddedByHoverfly(t * // capturing unit.Cfg.CORS = *cors.DefaultCORSConfigs() unit.Cfg.SetMode("capture") - resp := unit.processRequest(r) + resp, _ := unit.processRequest(r) Expect(resp).ToNot(BeNil()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) diff --git a/core/journal/journal.go b/core/journal/journal.go index 0c26ec3ce..c164b3e2e 100644 --- a/core/journal/journal.go +++ b/core/journal/journal.go @@ -11,7 +11,7 @@ import ( sorting "sort" "strings" - "github.com/SpectoLabs/hoverfly/core/handlers/v2" + v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2" "github.com/SpectoLabs/hoverfly/core/matching" "github.com/SpectoLabs/hoverfly/core/models" "github.com/SpectoLabs/hoverfly/core/util" @@ -21,12 +21,21 @@ import ( const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" type JournalEntry struct { - Request *models.RequestDetails - Response *models.ResponseDetails - Mode string - TimeStarted time.Time - Latency time.Duration - Id string + Request *models.RequestDetails + Response *models.ResponseDetails + Mode string + TimeStarted time.Time + Latency time.Duration + Id string + PostServeActionEntry *PostServeActionEntry +} + +type PostServeActionEntry struct { + ActionName string + InvokedTime time.Time + CompletedTime time.Time + CorrelationId string + HttpStatus int } type Journal struct { @@ -88,9 +97,9 @@ func (this *Journal) GetAllIndexes() []v2.JournalIndexView { return journalIndexViews } -func (this *Journal) NewEntry(request *http.Request, response *http.Response, mode string, started time.Time) error { +func (this *Journal) NewEntry(request *http.Request, response *http.Response, mode string, started time.Time) (string, error) { if this.EntryLimit == 0 { - return fmt.Errorf("Journal disabled") + return "", fmt.Errorf("Journal disabled") } payloadRequest, _ := models.NewRequestDetailsFromHttpRequest(request) @@ -141,7 +150,7 @@ func (this *Journal) NewEntry(request *http.Request, response *http.Response, mo this.mutex.Unlock() - return nil + return entry.Id, nil } func (this *Journal) GetEntries(offset int, limit int, from *time.Time, to *time.Time, sort string) (v2.JournalView, error) { @@ -296,18 +305,33 @@ func convertJournalEntries(entries []JournalEntry) []v2.JournalEntryView { for _, journalEntry := range entries { journalEntryViews = append(journalEntryViews, v2.JournalEntryView{ - Request: journalEntry.Request.ConvertToRequestDetailsView(), - Response: journalEntry.Response.ConvertToResponseDetailsView(), - Mode: journalEntry.Mode, - TimeStarted: journalEntry.TimeStarted.Format(RFC3339Milli), - Latency: journalEntry.Latency.Seconds() * 1e3, - Id: journalEntry.Id, + Request: journalEntry.Request.ConvertToRequestDetailsView(), + Response: journalEntry.Response.ConvertToResponseDetailsView(), + PostServeActionEntry: getPostServeActionEntryView(journalEntry.PostServeActionEntry), + Mode: journalEntry.Mode, + TimeStarted: journalEntry.TimeStarted.Format(RFC3339Milli), + Latency: journalEntry.Latency.Seconds() * 1e3, + Id: journalEntry.Id, }) } return journalEntryViews } +func getPostServeActionEntryView(entry *PostServeActionEntry) *v2.PostServeActionEntryView { + + if entry != nil { + return &v2.PostServeActionEntryView{ + ActionName: entry.ActionName, + InvokedTime: entry.InvokedTime.Format(RFC3339Milli), + CompletedTime: entry.CompletedTime.Format(RFC3339Milli), + CorrelationId: entry.CorrelationId, + HttpStatus: entry.HttpStatus, + } + } + return nil +} + func convertJournalEntry(entry JournalEntry) v2.JournalEntryView { return v2.JournalEntryView{ @@ -342,3 +366,20 @@ func getSortParameters(sort string) (string, string, error) { return sortKey, sortOrder, nil } + +func (journal *Journal) UpdatePostServeActionDetailsInJournal(id string, actionName, correlationID string, invokedTime, completedTime time.Time, httpStatus int) { + + for i, _ := range journal.entries { + + if journal.entries[i].Id == id { + journal.entries[i].PostServeActionEntry = &PostServeActionEntry{ + ActionName: actionName, + CorrelationId: correlationID, + InvokedTime: invokedTime, + CompletedTime: completedTime, + HttpStatus: httpStatus, + } + + } + } +} diff --git a/core/journal/journal_test.go b/core/journal/journal_test.go index f8e357838..9d00c8d4f 100644 --- a/core/journal/journal_test.go +++ b/core/journal/journal_test.go @@ -44,7 +44,7 @@ func Test_Journal_NewEntry_AddsJournalEntryToEntries(t *testing.T) { nowTime := time.Now() - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -89,7 +89,7 @@ func Test_JournalIndex_NewEntryAfterAddingIndex_AddsJournalIndexEntryToIndexes(t indexErr := unit.AddIndex(indexName) Expect(indexErr).To(BeNil()) - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -116,7 +116,7 @@ func Test_JournalIndex_NewEntryBeforeAddingIndex_AddsJournalIndexEntryToIndexes( nowTime := time.Now() - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -183,7 +183,7 @@ func Test_Journal_NewEntry_RespectsEntryLimit(t *testing.T) { request, _ := http.NewRequest("GET", "http://hoverfly.io", nil) for i := 1; i < 8; i++ { - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -219,7 +219,7 @@ func Test_Journal_NewEntry_KeepsOrder(t *testing.T) { nowTime := time.Now() - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -231,7 +231,7 @@ func Test_Journal_NewEntry_KeepsOrder(t *testing.T) { Expect(err).To(BeNil()) request.Method = "DELETE" - err = unit.NewEntry(request, &http.Response{ + _, err = unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -260,7 +260,7 @@ func Test_Journal_NewEntry_WhenDisabledReturnsError(t *testing.T) { unit.EntryLimit = 0 request, _ := http.NewRequest("GET", "http://hoverfly.io", nil) - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ @@ -319,7 +319,7 @@ func Test_Journal_GetEntries_TurnsTimeDurationToMilliseconds(t *testing.T) { unit := journal.NewJournal() request, _ := http.NewRequest("GET", "http://hoverfly.io", nil) - err := unit.NewEntry(request, &http.Response{ + _, err := unit.NewEntry(request, &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test body")), Header: http.Header{ diff --git a/core/proxy.go b/core/proxy.go index 14bfaa8ac..31480456a 100644 --- a/core/proxy.go +++ b/core/proxy.go @@ -59,8 +59,11 @@ func NewProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer { proxy.OnRequest(matchesFilter(hoverfly.Cfg.Destination)).DoFunc( func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { startTime := time.Now() - resp := hoverfly.processRequest(r) - hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime) + resp, journalIDChannel := hoverfly.processRequest(r) + id, _ := hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime) + if journalIDChannel != nil { + journalIDChannel <- id + } return r, resp }) @@ -104,8 +107,11 @@ func NewWebserverProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer { proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { startTime := time.Now() r.URL.Scheme = "http" - resp := hoverfly.processRequest(r) - hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime) + resp, journalIDChannel := hoverfly.processRequest(r) + id, _ := hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime) + if journalIDChannel != nil { + journalIDChannel <- id + } body, err := util.GetResponseBody(resp) if err != nil {