-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
formatter_test.go
109 lines (87 loc) · 2.41 KB
/
formatter_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
106
107
108
109
package examples
import (
"html/template"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gavv/httpexpect/v2"
)
var failureTemplate = `
Test Failure!
test name: {{ .TestName | underscore }}
request name: {{ .RequestName }}
error details: {{ .Errors }}
actual value: {{ .Actual }}
reference value: {{ .Reference }}
`
var successTemplate = `
Test Success!
test name: {{ .TestName | underscore }}
request name: {{ .RequestName }}
`
var templateFuncs = template.FuncMap{
"underscore": func(s string) string {
var sb strings.Builder
elems := strings.Split(s, " ")
sb.WriteString(strings.Join(elems, "_"))
return sb.String()
},
}
// TestFailureTemplate tests with custom `FailureTemplate`
func TestFailureTemplate(t *testing.T) {
t.Skip() // remove this line to see test failure
handler := FruitsHandler()
server := httptest.NewServer(handler)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
TestName: "Check Fruits List",
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
Formatter: &httpexpect.DefaultFormatter{
// customize error message with `FailureTemplate`
FailureTemplate: failureTemplate,
TemplateFuncs: templateFuncs,
},
})
orange := map[string]interface{}{
"weight": 100,
}
e.PUT("/fruits/orange").WithJSON(orange).
Expect().
Status(http.StatusNoContent).NoContent()
fruits := e.GET("/fruits").WithName("Get Fruits").
Expect().
Status(http.StatusOK).JSON().Array()
fruits.ContainsAny("melon")
}
// TestSuccessTemplate tests with custom `SuccessTemplate`
func TestSuccessTemplate(t *testing.T) {
handler := FruitsHandler()
server := httptest.NewServer(handler)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
TestName: "Check Fruits List",
BaseURL: server.URL,
AssertionHandler: &httpexpect.DefaultAssertionHandler{
Formatter: &httpexpect.DefaultFormatter{
// customize success message with `SuccessTemplate`
SuccessTemplate: successTemplate,
TemplateFuncs: templateFuncs,
},
Reporter: t,
// to enable printing of success messages, we need to set `Logger`
Logger: t,
},
})
orange := map[string]interface{}{
"weight": 100,
}
e.PUT("/fruits/orange").WithJSON(orange).
Expect().
Status(http.StatusNoContent).NoContent()
fruits := e.GET("/fruits").WithName("Get Fruits").
Expect().
Status(http.StatusOK).JSON().Array()
fruits.ContainsAny("orange")
}