-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from quii/regex
Regex
- Loading branch information
Showing
11 changed files
with
274 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
- name: Regex example | ||
request: | ||
uri: /hello/chris | ||
regexuri: "\\/hello\\/[a-z]+" | ||
method: GET | ||
response: | ||
code: 200 | ||
body: '{"message": "hello, there"}' | ||
headers: | ||
content-type: text/json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mockingjay | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// RegexYAML allows you to work with regex fields in YAML | ||
type RegexYAML struct { | ||
regexp.Regexp | ||
} | ||
|
||
// UnmarshalYAML will unhmarshal a YAML field into regexp | ||
func (r *RegexYAML) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var stringFromYAML string | ||
err := unmarshal(&stringFromYAML) | ||
if err != nil { | ||
return err | ||
} | ||
reg, err := regexp.Compile(stringFromYAML) | ||
if err != nil { | ||
return err | ||
} | ||
r.Regexp = *reg | ||
return nil | ||
} | ||
|
||
// MarshalJSON returns a string for the regex | ||
func (r *RegexYAML) MarshalJSON() ([]byte, error) { | ||
asString := fmt.Sprintf(`"%s"`, r.Regexp.String()) | ||
return []byte(asString), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mockingjay | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type testRegexDataType struct { | ||
Regex *RegexYAML | ||
} | ||
|
||
func TestItCanUnmarshalRegex(t *testing.T) { | ||
rawYAML := `regex: "\\/hello\\/[a-z]+"` | ||
|
||
var d testRegexDataType | ||
err := yaml.Unmarshal([]byte(rawYAML), &d) | ||
|
||
if err != nil { | ||
t.Error("Couldnt unmarshal regex from YAML", err) | ||
} | ||
|
||
if d.Regex == nil { | ||
t.Error("Regex was not extracted from YAML") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package mockingjay | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
var ( | ||
incomingRequest = Request{ | ||
URI: "/hello/chris", | ||
Method: "GET", | ||
} | ||
) | ||
|
||
func TestMatchingWithRegex(t *testing.T) { | ||
|
||
uriPathRegex, err := regexp.Compile(`\/hello\/[a-z]+`) | ||
regexURI := &RegexYAML{Regexp: *uriPathRegex} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
serverConfig := Request{ | ||
URI: "/hello/world", | ||
RegexURI: regexURI, | ||
Method: "GET", | ||
} | ||
|
||
if !requestMatches(serverConfig, incomingRequest) { | ||
t.Error("Requests didnt match when we expected them to", incomingRequest, serverConfig) | ||
} | ||
} | ||
|
||
func TestItMatchesOnURL(t *testing.T) { | ||
serverConfig := Request{ | ||
URI: "/hello/bob", | ||
Method: "GET", | ||
} | ||
|
||
if requestMatches(serverConfig, incomingRequest) { | ||
t.Error("Should not match", serverConfig, incomingRequest) | ||
} | ||
} | ||
|
||
func TestItMatchesWildcardBodies(t *testing.T) { | ||
incomingRequest := Request{ | ||
URI: "/x", | ||
Method: "POST", | ||
Body: "Doesn't matter", | ||
} | ||
|
||
serverConfig := Request{ | ||
URI: "/x", | ||
Method: "POST", | ||
Body: "*", | ||
} | ||
|
||
if !requestMatches(serverConfig, incomingRequest) { | ||
t.Error("Expected wildcards to match", incomingRequest, serverConfig) | ||
} | ||
} |
Oops, something went wrong.