forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fruits_test.go
105 lines (82 loc) · 2.21 KB
/
fruits_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package examples
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gavv/httpexpect/v2"
)
func TestFruits(t *testing.T) {
handler := FruitsHandler()
server := httptest.NewServer(handler)
defer server.Close()
e := httpexpect.Default(t, server.URL)
e.GET("/fruits").
Expect().
Status(http.StatusOK).JSON().Array().IsEmpty()
orange := map[string]interface{}{
"weight": 100,
}
e.PUT("/fruits/orange").WithJSON(orange).
Expect().
Status(http.StatusNoContent).NoContent()
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()
fruits := e.GET("/fruits").
Expect().
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().
Status(http.StatusOK).JSON().Object().IsEqual(orange).NotEqual(apple)
e.GET("/fruits/orange").
Expect().
Status(http.StatusOK).
JSON().Object().ContainsKey("weight").HasValue("weight", 100)
fruit := e.GET("/fruits/apple").
Expect().
Status(http.StatusOK).JSON().Object()
fruit.Keys().ContainsOnly("colors", "weight", "image")
colors := fruit.Value("colors").Array()
colors.Alias("colors")
colors.ConsistsOf("green", "red")
colors.Length().IsEqual(2)
colors.Value(0).String().IsEqual("green")
colors.Value(1).String().IsEqual("red")
colors.Value(0).String().IsASCII()
colors.Value(0).String().HasPrefix("gr")
colors.Value(0).String().HasSuffix("een")
fruit.Value("weight").Number().InList(100, 200, 300)
fruit.Value("weight").Number().NotInList(400, 500, 600)
for _, element := range fruit.Value("image").Array().Iter() {
element.Object().
ContainsKey("type").
ContainsValue("fruit").
ContainsSubset(map[string]interface{}{
"type": "fruit",
})
}
e.GET("/fruits/melon").
Expect().
Status(http.StatusNotFound)
}