Skip to content

Commit

Permalink
Extend fruits server example (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
kairemor committed Jan 14, 2023
1 parent 6420280 commit 40ee19c
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions _examples/fruits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,33 @@ func TestFruits(t *testing.T) {
apple := map[string]interface{}{
"colors": []interface{}{"green", "red"},
"weight": 200,
"image": []map[string]string{
{
"id": " 1",
"url": "http://example.com",
"type": "fruit",
},
{
"id": "2",
"url": "http://example2.com",
"type": "fruit",
},
},
}

e.PUT("/fruits/apple").WithJSON(apple).
Expect().
Status(http.StatusNoContent).NoContent()

e.GET("/fruits").
fruits := e.GET("/fruits").
Expect().
Status(http.StatusOK).JSON().Array().ContainsOnly("orange", "apple")
Status(http.StatusOK).JSON().Array()

fruits.Every(func(index int, value *httpexpect.Value) {
value.String().NotEmpty()
})
fruits.ContainsAny("orange", "melon")
fruits.ContainsOnly("orange", "apple")

e.GET("/fruits/orange").
Expect().
Expand All @@ -54,14 +72,25 @@ func TestFruits(t *testing.T) {
Expect().
Status(http.StatusOK).JSON().Object()

obj.Keys().ContainsOnly("colors", "weight")
obj.Keys().ContainsOnly("colors", "weight", "image")

obj.Value("colors").Array().Elements("green", "red")
obj.Value("colors").Array().Element(0).String().Equal("green")
obj.Value("colors").Array().Element(1).String().Equal("red")
obj.Value("colors").Array().Element(1).String().IsASCII()
obj.Value("colors").Array().Element(1).String().HasPrefix("re")
obj.Value("colors").Array().Element(1).String().HasSuffix("ed")

obj.Value("weight").Number().Equal(200)

for _, element := range obj.Value("image").Array().Iter() {
element.Object().ContainsKey("id")
element.Object().ContainsValue("fruit")
element.Object().ContainsSubset(map[string]interface{}{
"type": "fruit",
})
}

e.GET("/fruits/melon").
Expect().
Status(http.StatusNotFound)
Expand Down

0 comments on commit 40ee19c

Please sign in to comment.