-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile_test.go
275 lines (264 loc) · 10.3 KB
/
profile_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"encoding/xml"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"strings"
"testing"
"time"
"github.com/franela/goreq"
. "github.com/smartystreets/goconvey/convey"
)
func TestURL(t *testing.T) {
Convey("The URL and URL Token tests, ", t, func() {
Convey("No tokens should return the given base URL", func() {
out := URL{}
xml.Unmarshal([]byte(`<url base="http://example.org:7789/stress/PUT" />`), &out)
So(out.Generate(), ShouldEqual, "http://example.org:7789/stress/PUT")
So(out.String(), ShouldEqual, "http://example.org:7789/stress/PUT")
So(out.Tokens, ShouldEqual, nil)
})
Convey("Invalid tokens", func() {
Convey("Choices have no separator should fail validation", func() {
u := URLToken{Choices: "Val1Val2", Token: "test"}
So(u.Validate, ShouldPanic)
})
Convey("Choices have a min and max", func() {
u := URLToken{Choices: "Val1|Val2", Token: "test", Min: 1}
So(u.Validate, ShouldNotPanic)
u = URLToken{Choices: "Val1|Val2", Token: "test", Max: 1}
So(u.Validate, ShouldNotPanic)
u = URLToken{Choices: "Val1|Val2", Token: "test", Min: 1, Max: 2}
So(u.Validate, ShouldNotPanic)
})
Convey("Token is not defined", func() {
u := URLToken{}
So(u.Validate, ShouldPanic)
})
Convey("Choices and pattern not defined", func() {
u := URLToken{Token: "test"}
So(u.Validate, ShouldPanic)
})
Convey("Pattern does not exist", func() {
u := URLToken{Pattern: "Val1Val2", Token: "test"}
So(u.Validate, ShouldPanic)
})
Convey("Min is greater than max", func() {
u := URLToken{Pattern: "alpha", Min: 10, Max: 5, Token: "test"}
So(u.Validate, ShouldPanic)
})
Convey("Min and max are negative", func() {
u := URLToken{Pattern: "alpha", Min: -10, Max: 5, Token: "test"}
So(u.Validate, ShouldPanic)
u = URLToken{Pattern: "alpha", Min: 10, Max: -5, Token: "test"}
So(u.Validate, ShouldPanic)
u = URLToken{Pattern: "alpha", Min: 10, Max: -5, Token: "test"}
So(u.Validate, ShouldPanic)
})
})
Convey("Valid tokens of all types should be deserialized correctly", func() {
example := `<url base="http://example.org:1598/expensive/token1-token2/token3/token4">
<token token="token1" choices="Val1|Val2" />
<token token="token2" pattern="alpha" min="5" max="10" />
<token token="token3" pattern="num" min="5" max="1000" />
<token token="token4" pattern="alphanum" min="5" max="10" />
</url>`
out := URL{}
xml.Unmarshal([]byte(example), &out)
So(out.Tokens, ShouldNotEqual, nil)
for _, tok := range *out.Tokens {
So(tok.Validate, ShouldNotPanic)
switch tok.Token {
case "token1":
So(tok.Choices, ShouldEqual, "Val1|Val2")
So(tok.Pattern, ShouldEqual, "")
So(tok.Min, ShouldEqual, 0)
So(tok.Max, ShouldEqual, 0)
So(tok.Generate(), ShouldBeIn, []string{"Val1", "Val2"})
case "token2":
So(tok.Choices, ShouldEqual, "")
So(tok.Pattern, ShouldEqual, "alpha")
So(tok.Min, ShouldEqual, 5)
So(tok.Max, ShouldEqual, 10)
matched, err := regexp.MatchString("[A-Za-z]{5,10}", tok.Generate())
So(err, ShouldEqual, nil)
So(matched, ShouldEqual, true)
So(tok.Generate(), ShouldNotEqual, tok.Generate())
case "token3":
So(tok.Choices, ShouldEqual, "")
So(tok.Pattern, ShouldEqual, "num")
So(tok.Min, ShouldEqual, 5)
So(tok.Max, ShouldEqual, 1000)
matched, err := regexp.MatchString("[0-9]{1,4}", tok.Generate())
So(err, ShouldEqual, nil)
So(matched, ShouldEqual, true)
So(tok.Generate(), ShouldNotEqual, tok.Generate())
numTok, convErr := strconv.Atoi(tok.Generate())
So(convErr, ShouldBeNil)
So(numTok, ShouldBeGreaterThanOrEqualTo, 5)
So(numTok, ShouldBeLessThan, 1000)
case "token4":
So(tok.Choices, ShouldEqual, "")
So(tok.Pattern, ShouldEqual, "alphanum")
So(tok.Min, ShouldEqual, 5)
So(tok.Max, ShouldEqual, 10)
matched, err := regexp.MatchString("[A-Za-z0-9]{5,10}", tok.Generate())
So(err, ShouldEqual, nil)
So(matched, ShouldEqual, true)
So(tok.Generate(), ShouldNotEqual, tok.Generate())
}
}
So(out.Validate, ShouldNotPanic)
pattern := "http://example.org:1598/expensive/(Val1|Val2)-[A-Za-z]{5,10}/[0-9]{1,4}/[A-Za-z0-9]{5,10}"
matched, err := regexp.MatchString(pattern, out.Generate())
So(err, ShouldEqual, nil)
So(matched, ShouldEqual, true)
So(out.String(), ShouldEqual, pattern)
})
Convey("Non existing tokens should panic", func() {
example := `<url base="http://example.org:1598/expensive/ToKeN1/">
<token token="token1" choices="Val1|Val2" /></url>`
out := URL{}
xml.Unmarshal([]byte(example), &out)
So(out.Tokens, ShouldNotEqual, nil)
for _, tok := range *out.Tokens {
// The token is valid so it should not panic.
So(tok.Validate, ShouldNotPanic)
}
// However, if the token is not found in the URL, its validation should fail.
So(out.Validate, ShouldPanic)
})
})
}
func TestLoadProfile(t *testing.T) {
Convey("Loading profiles works as expected", t, func() {
Convey("Loading nothing", func() {
err := loadProfile("")
So(err, ShouldNotEqual, nil)
})
Convey("Loading a non existing file", func() {
err := loadProfile("this_file_does_not_exist")
So(err, ShouldNotEqual, nil)
})
Convey("Loading the basic example", func() {
err := loadProfile("./docs/examples/basic.xml")
So(err, ShouldEqual, nil)
// Let's now check that the basic example was loaded correctly.
So(profile.Name, ShouldEqual, "Basic example")
So(profile.UID, ShouldEqual, "1")
numTests := 0
for _, test := range profile.Tests {
numTests++
switch test.Name {
case "Example 1":
So(test.String(), ShouldEqual, "Example 1 (critical=1s, warning=750ms)")
So(test.CriticalTh.Duration, ShouldEqual, time.Second*1)
So(test.WarningTh.Duration, ShouldEqual, time.Millisecond*750)
So(len(test.Requests), ShouldEqual, 1)
So(test.Requests[0].Method, ShouldEqual, "POST")
So(test.Requests[0].Repeat, ShouldEqual, 20)
So(test.Requests[0].Concurrency, ShouldEqual, 10)
So(test.Requests[0].RespType, ShouldEqual, "json")
So(test.Requests[0].Headers, ShouldBeNil)
So(test.Requests[0].Data, ShouldBeNil)
case "Example 2":
So(test.String(), ShouldEqual, "Example 2 (critical=1s, warning=750ms)")
So(test.CriticalTh.Duration, ShouldEqual, time.Second*1)
So(test.WarningTh.Duration, ShouldEqual, time.Millisecond*750)
So(len(test.Requests), ShouldEqual, 1)
So(test.Requests[0].Method, ShouldEqual, "POST")
So(test.Requests[0].Repeat, ShouldEqual, 1)
So(test.Requests[0].Concurrency, ShouldEqual, 1)
So(test.Requests[0].RespType, ShouldEqual, "json")
So(test.Requests[0].Headers.Data, ShouldEqual, "Cookie: example=true;")
So(test.Requests[0].Data.Data, ShouldEqual, `{"username": "admin", "password": "superstrong"}`)
So(test.Requests[0].FwdCookies, ShouldEqual, false)
// Checking the subrequests.
for pos, child := range test.Requests[0].Children {
So(child.Parent, ShouldNotEqual, nil)
So(child.Concurrency, ShouldEqual, 5)
So(child.Repeat, ShouldEqual, 25)
if pos == 0 {
So(child.Method, ShouldEqual, "GET")
So(child.FwdCookies, ShouldEqual, true)
} else {
So(child.Method, ShouldEqual, "PUT")
So(child.FwdCookies, ShouldEqual, false)
}
}
}
}
})
})
}
func TestTokenized(t *testing.T) {
Convey("Tokenizing data from a request works as expected", t, func() {
// Let's setup a test server.
var ts *httptest.Server
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/test" {
defer r.Body.Close()
w.Header().Add("X-Custom-Hdr", "Custom Header")
w.Header().Add("Set-Cookie", "session_id=42 ; Path=/")
w.WriteHeader(200)
fmt.Fprint(w, fmt.Sprintf(`{"URL": "%s", "json": true, "foolMeOnce": "shame on you"}`, r.URL))
}
}))
Convey("Given a Tokenized objects, confirm that it formats the right information if does not format anything.", func() {
t := Tokenized{Data: "test"}
So(t.Format(nil), ShouldEqual, "test")
So(t.String(), ShouldEqual, "{Tokenized with data}")
})
Convey("Given a Tokenized objects, confirm that it formats the right information if response is nil.", func() {
t := Tokenized{Data: "test", Cookie: "ChocChip"}
So(t.Format(nil), ShouldEqual, "test")
So(t.String(), ShouldEqual, "{Tokenized with cookie with data}")
})
Convey("Given a Tokenized objects, confirm that it formats the right information.", func() {
example := `<headers responseToken="resp" headerToken="hdr" cookieToken="cke">
X-Fool:NotAMonkey resp/foolMeOnce
Cookie:test=true;session_id=cke/session_id
Some-Header:hdr/X-Custom-Hdr
X-Cannot-Decode: resp/json
</headers>`
out := Tokenized{}
xml.Unmarshal([]byte(example), &out)
gresp, _ := goreq.Request{Uri: ts.URL + "/test"}.Do()
resp := Response{}
resp.FromGoResp(gresp, nil, time.Now())
expectations := []string{"", "X-Fool:NotAMonkey shame on you", "Cookie:test=true;session_id=42", "Some-Header:Custom Header", "X-Cannot-Decode:", ""}
for pos, line := range strings.Split(out.Format(&resp), "\n") {
So(strings.TrimSpace(line), ShouldEqual, expectations[pos])
}
So(out.String(), ShouldEqual, "{Tokenized with cookie with header with data}")
})
})
}
func TestProfileConstraints(t *testing.T) {
Convey("Profile validation should not be nominal", t, func() {
Convey("there are no tests", func() {
profileData := `<?xml version="1.0" encoding="UTF-8"?><sg name="Basic example" uid="1"><test name="Profile test" critical="1s" warning="750ms"/></sg>`
profile := Profile{}
xml.Unmarshal([]byte(profileData), &profile)
So(profile.Validate(), ShouldNotBeNil)
})
Convey("there cookie forwaring is enabled on top request", func() {
profileData := `<?xml version="1.0" encoding="UTF-8"?>
<sg name="Basic example" uid="1">
<test name="SG test" critical="1s" warning="750ms">
<description>This is the test for SG.</description>
<request method="get" responseType="json" repeat="20"
useParentCookies="true" concurrency="10">
<url base="http://google.com/search" />
</request>
</test>
</sg>`
profile := Profile{}
xml.Unmarshal([]byte(profileData), &profile)
So(func() { profile.Validate() }, ShouldNotPanic)
})
})
}