Skip to content

Commit

Permalink
Merge branch 'master' into regex
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Jan 21, 2016
2 parents 298b15a + 289f6d1 commit 7be6ad3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/regex.yaml
@@ -1,7 +1,7 @@
---
- name: Regex example
request:
uri: /hello
uri: /hello/chris
regexuri: "\\/hello\\/[a-z]+"
method: GET
response:
Expand Down
27 changes: 19 additions & 8 deletions mockingjay/fakeendpoint.go
Expand Up @@ -32,10 +32,13 @@ func (f FakeEndpoint) isValid() error {
}

var (
errDuplicateRequestsError = errors.New("There were duplicated requests in YAML")
errResponseInvalid = errors.New("Response is not configured correctly")
errResponseInvalid = errors.New("Response is not configured correctly")
)

func errDuplicateRequestsError(duplicates []string) error {
return fmt.Errorf("There were duplicated requests in YAML %v", duplicates)
}

// NewFakeEndpoints returns an array of Endpoints from a YAML byte array. Returns an error if YAML cannot be parsed
func NewFakeEndpoints(data []byte) (endpoints []FakeEndpoint, err error) {
err = yaml.Unmarshal(data, &endpoints)
Expand All @@ -52,19 +55,27 @@ func NewFakeEndpoints(data []byte) (endpoints []FakeEndpoint, err error) {
}
}

if isDuplicates(endpoints) {
return nil, errDuplicateRequestsError
if duplicates := findDuplicates(endpoints); len(duplicates) > 0 {
return nil, errDuplicateRequestsError(duplicates)
}

return
}

func isDuplicates(endpoints []FakeEndpoint) bool {
requests := make(map[string]bool)
func findDuplicates(endpoints []FakeEndpoint) []string {
requests := make(map[string]int)

for _, e := range endpoints {
requests[e.Request.hash()] = true
requests[e.Request.hash()] = requests[e.Request.hash()] + 1
}

var duplicates []string

for k, v := range requests {
if v > 1 {
duplicates = append(duplicates, k)
}
}

return len(requests) != len(endpoints)
return duplicates
}
4 changes: 2 additions & 2 deletions mockingjay/fakeendpoint_test.go
Expand Up @@ -200,8 +200,8 @@ func TestItReturnsErrorWhenRequestsAreDuplicated(t *testing.T) {
t.Fatal("Expected an error to be returned for duplicated requests")
}

if err != errDuplicateRequestsError {
t.Error("Expected", errDuplicateRequestsError, "but got", err)
if !strings.Contains(err.Error(), "duplicated") {
t.Error("Unexpeted error message", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion mockingjay/request.go
Expand Up @@ -87,7 +87,7 @@ func (r Request) String() string {
}

func (r Request) hash() string {
return fmt.Sprintf("%v%v%v%v", r.URI, r.Method, r.Headers, r.Body)
return fmt.Sprintf("URI: %v | METHOD: %v | HEADERS: %v | BODY: %v", r.URI, r.Method, r.Headers, r.Body)
}

func requestMatches(a, b Request) bool {
Expand Down

0 comments on commit 7be6ad3

Please sign in to comment.