Skip to content

Commit

Permalink
Improving output of duplicated requests
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Jan 4, 2016
1 parent 1736ec7 commit 289f6d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
27 changes: 19 additions & 8 deletions mockingjay/fakeendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func (f FakeEndpoint) isValid() bool {
}

var (
errInvalidConfigError = errors.New("Config YAML structure is invalid")
errDuplicateRequestsError = errors.New("There were duplicated requests in YAML")
errInvalidConfigError = errors.New("Config YAML structure is invalid")
)

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 @@ -46,19 +49,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
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestItReturnsErrorWhenRequestsAreDuplicated(t *testing.T) {
t.Error("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)
}
}
2 changes: 1 addition & 1 deletion mockingjay/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ 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)
}

0 comments on commit 289f6d1

Please sign in to comment.