From b133acbc39848df097123874af312963788b73a5 Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Thu, 11 Jul 2024 21:53:22 +0100 Subject: [PATCH] Add labels to request response pair --- core/handlers/v2/schema.json | 6 ++ core/handlers/v2/simulation_views.go | 2 +- core/handlers/v2/simulation_views_v5.go | 1 + core/models/request_matcher.go | 2 + core/models/simulation_test.go | 96 ++++++++++++------------- 5 files changed, 58 insertions(+), 49 deletions(-) diff --git a/core/handlers/v2/schema.json b/core/handlers/v2/schema.json index fad335059..b71232fbf 100644 --- a/core/handlers/v2/schema.json +++ b/core/handlers/v2/schema.json @@ -161,6 +161,12 @@ }, "request-response-pair": { "properties": { + "labels": { + "items": { + "type": "string" + }, + "type": "array" + }, "request": { "$ref": "#/definitions/request" }, diff --git a/core/handlers/v2/simulation_views.go b/core/handlers/v2/simulation_views.go index 9f8c1f8cd..ae682d9dd 100644 --- a/core/handlers/v2/simulation_views.go +++ b/core/handlers/v2/simulation_views.go @@ -32,7 +32,7 @@ func NewSimulationViewFromRequestBody(requestBody []byte) (SimulationViewV5, err schemaVersion := jsonMap["meta"].(map[string]interface{})["schemaVersion"].(string) - if schemaVersion == "v5" || schemaVersion == "v5.1" || schemaVersion == "v5.2" { + if strings.HasPrefix(schemaVersion, "v5") { err := ValidateSimulationSchemaFromFile(jsonMap, SimulationViewV5Schema) if err != nil { diff --git a/core/handlers/v2/simulation_views_v5.go b/core/handlers/v2/simulation_views_v5.go index 4b5579994..d0f67b385 100644 --- a/core/handlers/v2/simulation_views_v5.go +++ b/core/handlers/v2/simulation_views_v5.go @@ -17,6 +17,7 @@ type DataViewV5 struct { } type RequestMatcherResponsePairViewV5 struct { + Labels []string `json:"labels,omitempty"` RequestMatcher RequestMatcherViewV5 `json:"request"` Response ResponseDetailsViewV5 `json:"response"` } diff --git a/core/models/request_matcher.go b/core/models/request_matcher.go index b5aaac5cd..11c745f1f 100644 --- a/core/models/request_matcher.go +++ b/core/models/request_matcher.go @@ -142,6 +142,7 @@ func getViewFromRequestFieldMatcher(matcher *RequestFieldMatchers) *v2.MatcherVi } type RequestMatcherResponsePair struct { + Labels []string RequestMatcher RequestMatcher Response ResponseDetails } @@ -229,6 +230,7 @@ func (this *RequestMatcherResponsePair) BuildView() v2.RequestMatcherResponsePai } return v2.RequestMatcherResponsePairViewV5{ + Labels: this.Labels, RequestMatcher: v2.RequestMatcherViewV5{ Path: path, Method: method, diff --git a/core/models/simulation_test.go b/core/models/simulation_test.go index edeed3569..6aca4dba5 100644 --- a/core/models/simulation_test.go +++ b/core/models/simulation_test.go @@ -15,7 +15,7 @@ func Test_Simulation_AddPair_CanAddAPairToTheArray(t *testing.T) { unit := models.NewSimulation() unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -23,7 +23,7 @@ func Test_Simulation_AddPair_CanAddAPairToTheArray(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(unit.GetMatchingPairs()).To(HaveLen(1)) @@ -37,7 +37,7 @@ func Test_Simulation_AddPair_CanAddAFullPairToTheArray(t *testing.T) { unit := models.NewSimulation() unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Body: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -75,7 +75,7 @@ func Test_Simulation_AddPair_CanAddAFullPairToTheArray(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "testresponsebody", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -112,7 +112,7 @@ func Test_Simulation_AddPairInSequence_CanAddAFullPairToTheArray(t *testing.T) { unit := models.NewSimulation() unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Body: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -150,7 +150,7 @@ func Test_Simulation_AddPairInSequence_CanAddAFullPairToTheArray(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "testresponsebody", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -187,7 +187,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { unit := models.NewSimulation() unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -195,7 +195,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -203,7 +203,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { }, &state.State{State: map[string]string{}}) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -211,7 +211,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -219,7 +219,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { }, &state.State{State: map[string]string{}}) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -227,7 +227,7 @@ func Test_Simulation_AddPairInSequence_CanSequence(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "3", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -264,7 +264,7 @@ func Test_Simulation_AddPairInSequence_CanBeUsedWithAddPair(t *testing.T) { unit := models.NewSimulation() unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -272,7 +272,7 @@ func Test_Simulation_AddPairInSequence_CanBeUsedWithAddPair(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -280,7 +280,7 @@ func Test_Simulation_AddPairInSequence_CanBeUsedWithAddPair(t *testing.T) { }) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -288,7 +288,7 @@ func Test_Simulation_AddPairInSequence_CanBeUsedWithAddPair(t *testing.T) { }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -319,7 +319,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi state := state.NewState() unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -327,7 +327,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -335,7 +335,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -343,7 +343,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -351,7 +351,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -359,7 +359,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "different1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -367,7 +367,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -375,7 +375,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceTwoDifferentSequences(t *testi }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "different2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -419,7 +419,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes state := state.NewState() unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -427,7 +427,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -435,7 +435,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -443,7 +443,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -451,7 +451,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -459,7 +459,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "different1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -467,7 +467,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -475,7 +475,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "different2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -483,7 +483,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -491,7 +491,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "third1", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -499,7 +499,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, state) unit.AddPairInSequence(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -507,7 +507,7 @@ func Test_Simulation_AddPairInSequence_CanSequenceThreeDifferentSequences(t *tes }, }, }, - models.ResponseDetails{ + Response: models.ResponseDetails{ Body: "third2", Headers: map[string][]string{"testheader": {"testvalue"}}, Status: 200, @@ -562,7 +562,7 @@ func Test_Simulation_AddPair_WillNotSaveDuplicates(t *testing.T) { unit := models.NewSimulation() isAdded := unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -570,13 +570,13 @@ func Test_Simulation_AddPair_WillNotSaveDuplicates(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(isAdded).To(BeTrue()) isAdded = unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -584,7 +584,7 @@ func Test_Simulation_AddPair_WillNotSaveDuplicates(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(isAdded).To(BeFalse()) @@ -597,7 +597,7 @@ func Test_Simulation_AddPair_WillSaveTwoWhenNotDuplicates(t *testing.T) { unit := models.NewSimulation() isAdded := unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -605,12 +605,12 @@ func Test_Simulation_AddPair_WillSaveTwoWhenNotDuplicates(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(isAdded).To(BeTrue()) isAdded = unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -618,7 +618,7 @@ func Test_Simulation_AddPair_WillSaveTwoWhenNotDuplicates(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(isAdded).To(BeTrue()) @@ -633,7 +633,7 @@ func Test_Simulation_GetMatchingPairs(t *testing.T) { unit := models.NewSimulation() unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -641,7 +641,7 @@ func Test_Simulation_GetMatchingPairs(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) Expect(unit.GetMatchingPairs()).To(HaveLen(1)) @@ -654,7 +654,7 @@ func Test_Simulation_DeleteMatchingPairs(t *testing.T) { unit := models.NewSimulation() unit.AddPair(&models.RequestMatcherResponsePair{ - models.RequestMatcher{ + RequestMatcher: models.RequestMatcher{ Destination: []models.RequestFieldMatchers{ { Matcher: matchers.Exact, @@ -662,7 +662,7 @@ func Test_Simulation_DeleteMatchingPairs(t *testing.T) { }, }, }, - models.ResponseDetails{}, + Response: models.ResponseDetails{}, }) unit.DeleteMatchingPairsAlongWithCustomData()