Skip to content

Commit

Permalink
Merge pull request #18 from samikoskinen/master
Browse files Browse the repository at this point in the history
Fix a logic error in validation of responses with both data and included
  • Loading branch information
Derek Dowling committed Feb 15, 2016
2 parents 514b565 + 1a106c3 commit a56d001
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion document.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (d *Document) Validate(r *http.Request, response bool) *Error {
if d.HasErrors() && d.HasData() {
return ISE("Both `errors` and `data` cannot be set for a JSON response")
}
if d.HasData() && d.Included != nil {
if !d.HasData() && d.Included != nil {
return ISE("'included' should only be set for a response if 'data' is as well")
}

Expand Down
31 changes: 31 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,44 @@ func TestDocument(t *testing.T) {
Status: http.StatusAccepted,
}

testObjectForInclusion := &Object{
ID: "1",
Type: "Included",
}

req := &http.Request{Method: "GET"}

Convey("should accept an object", func() {
doc := Build(testObject)

So(doc.Data, ShouldResemble, List{testObject})
So(doc.Status, ShouldEqual, http.StatusAccepted)
})

Convey("should not accept an included object without objects in data", func() {
doc := New()
doc.Included = append(doc.Included, testObjectForInclusion)
doc.Status = 200

validationErrors := doc.Validate(req, true)

So(validationErrors, ShouldNotBeNil)
})

Convey("should accept an object in data and an included object", func() {
doc := Build(testObject)
doc.Included = append(doc.Included, testObjectForInclusion)

validationErrors := doc.Validate(req, true)

So(validationErrors, ShouldBeNil)
So(doc.Data, ShouldResemble, List{testObject})
So(doc.Included, ShouldNotBeEmpty)
So(doc.Included[0], ShouldResemble, testObjectForInclusion)
So(doc.Status, ShouldEqual, http.StatusAccepted)
})


Convey("should accept a list", func() {
list := List{testObject}
doc := Build(list)
Expand Down

0 comments on commit a56d001

Please sign in to comment.