Skip to content

Commit cd51f9d

Browse files
Andrei Savitskytommysitu
authored andcommitted
#855 Use copyState map as the parameter in StateMatcher function
1 parent 5e95196 commit cd51f9d

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

core/matching/matching_strategy_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func MatchingStrategyRunner(req models.RequestDetails, webserver bool, simulatio
3737

3838
strategy.Matching(QueryMatching(requestMatcher, req.Query), "queries")
3939

40-
strategy.Matching(StateMatcher(state, requestMatcher.RequiresState), "state")
40+
strategy.Matching(StateMatcher(copyState, requestMatcher.RequiresState), "state")
4141

4242
if result := strategy.PostMatching(req, requestMatcher, matchingPair, copyState); result != nil {
4343
return result

core/matching/state_matcher.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package matching
22

3-
import (
4-
"github.com/SpectoLabs/hoverfly/core/state"
5-
)
6-
7-
func StateMatcher(currentState *state.State, requiredState map[string]string) *FieldMatch {
3+
func StateMatcher(copyState map[string]string, requiredState map[string]string) *FieldMatch {
84

95
score := 0
106
matched := true
@@ -17,11 +13,10 @@ func StateMatcher(currentState *state.State, requiredState map[string]string) *F
1713
}
1814

1915
for key, value := range requiredState {
20-
currentStateValue, ok := currentState.GetState(key)
21-
if !ok {
16+
if _, ok := copyState[key]; !ok {
2217
matched = false
2318
}
24-
if currentStateValue != value {
19+
if copyState[key] != value {
2520
matched = false
2621
} else {
2722
score++

core/matching/state_matcher_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package matching
33
import (
44
"testing"
55

6-
"github.com/SpectoLabs/hoverfly/core/state"
76
. "github.com/onsi/gomega"
87
)
98

@@ -28,7 +27,7 @@ func Test_StateMatcher_ShouldMatchIfCurrentStateIsNilAndRequiredStateIsEmpty(t *
2827
func Test_StateMatcher_ShouldMatchIfCurrentStateIEmptyAndRequiredStateIsNil(t *testing.T) {
2928
RegisterTestingT(t)
3029

31-
match := StateMatcher(&state.State{State: make(map[string]string)}, nil)
30+
match := StateMatcher(make(map[string]string), nil)
3231

3332
Expect(match.Matched).To(BeTrue())
3433
Expect(match.Score).To(Equal(0))
@@ -37,7 +36,7 @@ func Test_StateMatcher_ShouldMatchIfCurrentStateIEmptyAndRequiredStateIsNil(t *t
3736
func Test_StateMatcher_ShouldNotMatchIfRequiredStateLengthIsGreaterThanActualStateLength(t *testing.T) {
3837
RegisterTestingT(t)
3938

40-
match := StateMatcher(&state.State{State: make(map[string]string)}, map[string]string{"foo": "bar"})
39+
match := StateMatcher(make(map[string]string), map[string]string{"foo": "bar"})
4140

4241
Expect(match.Matched).To(BeFalse())
4342
Expect(match.Score).To(Equal(0))
@@ -47,7 +46,7 @@ func Test_StateMatcher_ShouldNotMatchIfLengthsAreTheSameButKeysAreDifferent(t *t
4746
RegisterTestingT(t)
4847

4948
match := StateMatcher(
50-
&state.State{State: map[string]string{"foo": "bar", "cheese": "ham"}},
49+
map[string]string{"foo": "bar", "cheese": "ham"},
5150
map[string]string{"adasd": "bar", "sadsad": "ham"})
5251

5352
Expect(match.Matched).To(BeFalse())
@@ -58,7 +57,7 @@ func Test_StateMatcher_ShouldNotMatchIfKeysAreTheSameButValuesAreDifferent(t *te
5857
RegisterTestingT(t)
5958

6059
match := StateMatcher(
61-
&state.State{State: map[string]string{"foo": "bar", "cheese": "ham"}},
60+
map[string]string{"foo": "bar", "cheese": "ham"},
6261
map[string]string{"foo": "adsad", "cheese": "ham"})
6362

6463
Expect(match.Matched).To(BeFalse())
@@ -69,7 +68,7 @@ func Test_StateMatcher_ShouldMatchIsKeysAndValuesAreTheSame(t *testing.T) {
6968
RegisterTestingT(t)
7069

7170
match := StateMatcher(
72-
&state.State{State: map[string]string{"foo": "bar", "cheese": "ham"}},
71+
map[string]string{"foo": "bar", "cheese": "ham"},
7372
map[string]string{"foo": "bar", "cheese": "ham"})
7473

7574
Expect(match.Matched).To(BeTrue())

0 commit comments

Comments
 (0)