-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_test.go
168 lines (128 loc) · 2.64 KB
/
expect_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"net/http"
"testing"
"gopkg.in/yaml.v2"
)
func TestEvaluate(t *testing.T) {
content := []byte(`
status_code_re: 200
headers_re:
Content-Type: application.*
body_re: .*`)
var e Expected
err := yaml.Unmarshal(content, &e)
if err != nil {
t.Fatal(err)
}
r := http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
}
b := ""
err = e.Evaluate("some name", &r, b)
if e.Name != "some name" {
t.Error("Name was not set")
}
if err != nil {
t.Errorf("Should not return error on success: %s", err)
}
}
func TestEvaluateStatusCode(t *testing.T) {
e := Expected{
StatusCode: "([0-9])+",
}
err := e.EvaluateStatusCode(200)
if err != nil {
t.Errorf("Should not return error on success: %s", err)
}
}
func TestEvaluateStatusCodeMissmatch(t *testing.T) {
e := Expected{
StatusCode: "3[0-9]{2}",
}
err := e.EvaluateStatusCode(200)
if err == nil {
t.Error("Should not return nil error on failure")
}
}
func TestEvaluateStatusCodeCompileError(t *testing.T) {
e := Expected{
StatusCode: "3[}",
}
err := e.EvaluateStatusCode(200)
if err == nil {
t.Error("Should not return nil error on failure")
}
}
func TestEvaluateHeaders(t *testing.T) {
e := Expected{
Headers: map[string]string{
"Some-Header": "^[a-z]+$",
},
}
h := http.Header{
"Some-Header": []string{"abc"},
}
err := e.EvaluateHeaders(&h)
if err != nil {
t.Errorf("Should not return error on success: %s", err)
}
}
func TestEvaluateHeadersMissmatch(t *testing.T) {
e := Expected{
Headers: map[string]string{
"Some-Header": "^[a-z]+$",
},
}
h := http.Header{
"Some-Header": []string{"123"},
}
err := e.EvaluateHeaders(&h)
if err == nil {
t.Errorf("Should not return nil error on failure")
}
}
func TestEvaluateHeadersCompileError(t *testing.T) {
e := Expected{
Headers: map[string]string{
"Some-Header": "^}[a-z]+$",
},
}
h := http.Header{
"Some-Header": []string{"123"},
}
err := e.EvaluateHeaders(&h)
if err == nil {
t.Errorf("Should not return nil error on failure")
}
}
func TestEvaluateBody(t *testing.T) {
e := Expected{
Body: "a{3}",
}
err := e.EvaluateBody("hisdhaaa93")
if err != nil {
t.Errorf("Should not return error on success: %s", err)
}
}
func TestEvaluateBodyMissmatch(t *testing.T) {
e := Expected{
Body: "[0-9]+",
}
err := e.EvaluateBody("abc")
if err == nil {
t.Errorf("Should not return nil error on failure")
}
}
func TestEvaluateBodyCompileError(t *testing.T) {
e := Expected{
Body: "[0-](/+",
}
err := e.EvaluateBody("abc")
if err == nil {
t.Errorf("Should not return nil error on failure")
}
}