Skip to content

Commit

Permalink
Merge pull request #205 from Thiht/validate-matchers
Browse files Browse the repository at this point in the history
validate matchers on unmarshal
  • Loading branch information
Thiht authored Jul 11, 2021
2 parents ef9d9f9 + d5ed157 commit 9150326
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
18 changes: 16 additions & 2 deletions server/types/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ type StringMatcher struct {
Value string `json:"value" yaml:"value,flow"`
}

func (sm StringMatcher) Validate() error {
if _, ok := asserts[sm.Matcher]; !ok {
return fmt.Errorf("invalid matcher %q", sm.Matcher)
}

// Try to compile ShouldMatch regular expressions
if sm.Matcher == "ShouldMatch" || sm.Matcher == "ShouldNotMatch" {
if _, err := regexp.Compile(sm.Value); err != nil {
return fmt.Errorf("invalid regular expression provided to %q operator: %v", sm.Matcher, sm.Value)
}
}
return nil
}

func (sm StringMatcher) Match(value string) bool {
matcher := asserts[sm.Matcher]
if matcher == nil {
Expand Down Expand Up @@ -125,7 +139,7 @@ func (sm *StringMatcher) UnmarshalJSON(data []byte) error {

sm.Matcher = res.Matcher
sm.Value = res.Value
return nil
return sm.Validate()
}

func (sm *StringMatcher) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand All @@ -147,7 +161,7 @@ func (sm *StringMatcher) UnmarshalYAML(unmarshal func(interface{}) error) error

sm.Matcher = res.Matcher
sm.Value = res.Value
return nil
return sm.Validate()
}

type StringMatcherSlice []StringMatcher
Expand Down
22 changes: 11 additions & 11 deletions server/types/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func TestStringMatcherJSON(t *testing.T) {
t.Fatalf("serialized value %s should be equal to %s", string(b), test)
}

test = `{"matcher":"test","value":"test2"}`
test = `{"matcher":"ShouldEqual","value":"test2"}`
serialized = test
res = StringMatcher{}
if err = json.Unmarshal([]byte(test), &res); err != nil {
t.Fatal(err)
}

if res.Matcher != "test" {
t.Fatalf("matcher %s should be equal to %s", res.Matcher, "test")
if res.Matcher != "ShouldEqual" {
t.Fatalf("matcher %s should be equal to %s", res.Matcher, "ShouldEqual")
}
if res.Value != "test2" {
t.Fatalf("value %s should be equal to %s", res.Value, "test2")
Expand Down Expand Up @@ -74,14 +74,14 @@ func TestStringMatcherYAML(t *testing.T) {
t.Fatal(err)
}

test = `{"matcher":"test","value":"test2"}`
test = `{"matcher":"ShouldEqual","value":"test2"}`
res = StringMatcher{}
if err := yaml.Unmarshal([]byte(test), &res); err != nil {
t.Fatal(err)
}

if res.Matcher != "test" {
t.Fatalf("matcher %s should be equal to %s", res.Matcher, "test")
if res.Matcher != "ShouldEqual" {
t.Fatalf("matcher %s should be equal to %s", res.Matcher, "ShouldEqual")
}
if res.Value != "test2" {
t.Fatalf("value %s should be equal to %s", res.Value, "test2")
Expand Down Expand Up @@ -128,20 +128,20 @@ func TestMultiMapMatcherJSON(t *testing.T) {
t.Fatalf("serialized value %s should be equal to %s", string(b), test)
}

test = `{"test":{"matcher":"test2","value":"test3"}}`
serialized = `{"test":[{"matcher":"test2","value":"test3"}]}`
test = `{"test":{"matcher":"ShouldEqual","value":"test3"}}`
serialized = `{"test":[{"matcher":"ShouldEqual","value":"test3"}]}`
res = MultiMapMatcher{}
if err = json.Unmarshal([]byte(test), &res); err != nil {
t.Fatal(err)
}

if res["test"][0].Matcher != "test2" {
t.Fatalf("matcher %s should be equal to %s", res["test"][0].Matcher, "test")
if res["test"][0].Matcher != "ShouldEqual" {
t.Fatalf("matcher %s should be equal to %s", res["test"][0].Matcher, "ShouldEqual")
}

expected = MultiMapMatcher{
"test": {
{Matcher: "test2", Value: "test3"},
{Matcher: "ShouldEqual", Value: "test3"},
},
}

Expand Down
1 change: 0 additions & 1 deletion server/types/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (m *Mock) Validate() error {
if m.Request.Path.Value == "" {
m.Request.Path.Matcher = "ShouldMatch"
m.Request.Path.Value = ".*"

}

m.Request.Method.Value = strings.TrimSpace(m.Request.Method.Value)
Expand Down

0 comments on commit 9150326

Please sign in to comment.