-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
executable file
·381 lines (323 loc) · 12.1 KB
/
tests.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Unittests for restretto
"""
import unittest
import restretto
import restretto.cli
class ResourceTestCase(unittest.TestCase):
def test_parse_from_str(self):
spec = '/some/uri'
res = restretto.Resource(spec)
expected = {'method': 'get', 'url': spec}
self.assertEqual(res.request, expected)
def test_expand_resource_method(self):
spec = {'url': '/some/url', 'headers': {}}
action = restretto.Resource(spec)
expected = {'method': 'get', 'url': '/some/url', 'headers': {}}
self.assertEqual(action.request, expected)
def test_expand_resource_url(self):
spec = {'put': '/url', 'json': [1, 2]}
expanded = restretto.Resource(spec)
expected = {'method': 'put', 'url': '/url', 'json': [1, 2]}
self.assertEqual(expanded.request, expected)
def test_expanded_resource(self):
spec = {'url': '/url', 'method': 'delete'}
expanded = restretto.Resource(spec)
expected = spec
self.assertEqual(expected, expanded.request)
def test_empty_resource(self):
spec = {'headers': {}, 'body': ''}
with self.assertRaises(restretto.errors.ParseError):
restretto.Resource(spec)
def test_missing_url(self):
spec = {'method': 'options'}
with self.assertRaises(restretto.errors.ParseError):
restretto.Resource(spec)
def test_invalid_method(self):
spec = {'url': '/url', 'method': 'bad_method'}
with self.assertRaises(restretto.errors.ParseError):
restretto.Resource(spec)
def test_assertion_conflict(self):
spec = {
'url': '/',
'expect': [{'status': '4xx'}],
'assert': [{'header': 'Content-Type'}]
}
with self.assertRaises(restretto.errors.ParseError):
restretto.Resource(spec)
def test_get_asserts(self):
spec = {
'url': '/url',
'assert': [
{'status': '200'}
]
}
action = restretto.Resource(spec)
expected_request = {
'url': '/url',
'method': 'get',
}
expected_asserts = [
{'status': '200'}
]
self.assertEqual(action.request, expected_request)
self.assertEqual(action.asserts, expected_asserts)
def test_get_expect(self):
spec = {
'url': '/url',
'expect': [
{'status': '500'}
]
}
action = restretto.Resource(spec)
expected_request = {
'url': '/url',
'method': 'get',
}
expected_asserts = [
{'status': '500'}
]
self.assertEqual(action.request, expected_request)
self.assertEqual(action.asserts, expected_asserts)
class SessionTestCase(unittest.TestCase):
def test_context(self):
ctx = {'server': 'httpbin.org', 'token': 'secret'}
spec = {
'title': 'Sample',
'baseUri': 'http://{{server}}',
'headers': {
"X-Auth-Token": "{{token}}"
},
'actions': []
}
session = restretto.Session(spec, ctx)
self.assertEqual(session.baseUri, 'http://httpbin.org')
self.assertEqual(session.headers, {'X-Auth-Token': 'secret'})
class AssertsTestCase(unittest.TestCase):
class Response(object):
def __init__(self, status_code, headers={}, text=None, json=None):
self.status_code = status_code
self.headers = headers
self.text = text
self.json = json
self.ok = status_code in range(200, 299)
self.reason = 'reason'
def __bool__(self):
return self.ok
def test_response_ok(self):
assertion = restretto.assertions.Assert()
resp = self.Response(200)
self.assertTrue(assertion.test(resp))
def test_response_not_ok(self):
assertion = restretto.assertions.Assert()
resp = self.Response(404)
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_status(self):
spec = [{'status': '500'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(500)
self.assertTrue(assertion.test(resp))
resp = self.Response(501)
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_status_match(self):
spec = [{'status': '4xx'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(403)
self.assertTrue(assertion.test(resp))
resp = self.Response(501)
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_status_in(self):
spec = [{'status': ['401']}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(401)
self.assertTrue(assertion.test(resp))
resp = self.Response(404)
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_header_exists(self):
spec = [{'header': 'Content-Type'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, headers={'Content-Type': 'text/plain'})
self.assertTrue(assertion.test(resp))
resp = self.Response(404, headers={'x-bar': 'y-foo'})
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_header_is(self):
spec = [{'header': 'Content-Type', 'is': 'text/html'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, headers={'Content-Type': 'text/html'})
self.assertTrue(assertion.test(resp))
resp = self.Response(200, headers={'Content-Type': 'text/plain'})
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_header_contains(self):
spec = [{'header': 'Content-Type', 'contains': 'xml'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, headers={'Content-Type': 'application/xml+xhtml'})
self.assertTrue(assertion.test(resp))
resp = self.Response(200, headers={'Content-Type': 'text/html'})
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_body_text(self):
spec = [{'body': 'text'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, text='Sample')
self.assertTrue(assertion.test(resp))
resp = self.Response(200, text=None)
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_body_text_is(self):
spec = [{'body': 'text', 'is': 'sample'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, text='sample')
self.assertTrue(assertion.test(resp))
resp = self.Response(200, text='other')
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
def test_body_text_contains(self):
spec = [{'body': 'text', 'contains': 'llo wo'}]
assertion = restretto.assertions.Assert(spec)
resp = self.Response(200, text='hello world')
self.assertTrue(assertion.test(resp))
resp = self.Response(200, text='ehlo world')
with self.assertRaises(restretto.errors.ExpectError):
assertion.test(resp)
class TemplatingTestCase(unittest.TestCase):
VARS = {
'server': 'httpbin.org',
'scheme': 'http',
'extra': {
'header': 'X-Custom',
'value': 'custom-value',
'payload_data': 'hello from vars'
},
'val': 100,
'accept': 'application/json',
"nested": {
"simple": "string",
"items": [1, 2, 3],
"obj": {
"inner": True,
"nested_items": ["a", "b"],
"nested_obj": {
"x": "y"
}
}
}
}
def test_template_complex(self):
src = {
'url': '{{scheme}}://{{server}}/base',
'headers': {
'Accept': '{{accept}}'
},
'expect': [
{'header': '{{extra.header}}', 'is': '{{extra.value}}'},
{'body': 'text', 'is': '{{extra.payload_data}}'}
],
'json': {
'option': 12,
'content': ['{{val}}']
}
}
result = restretto.utils.apply_context(src, self.VARS)
expected = {
'url': 'http://httpbin.org/base',
'headers': {
'Accept': 'application/json'
},
'expect': [
{'header': 'X-Custom', 'is': 'custom-value'},
{'body': 'text', 'is': 'hello from vars'}
],
'json': {
'option': 12,
'content': [100]
}
}
self.assertEqual(result, expected)
def test_nested_and_list(self):
src = {
"simple": "{{ server }}",
"concatenated": "{{scheme}}://{{server}}",
"items": [
"{{accept}}",
"{{extra.header}}:{{extra.value}}"
],
"nested": "{{nested}}"
}
result = restretto.utils.apply_context(src, self.VARS)
expected = {
"simple": "httpbin.org",
"concatenated": "http://httpbin.org",
"items": [
"application/json",
"X-Custom:custom-value"
],
"nested": self.VARS["nested"]
}
self.assertEqual(result, expected)
class LoaderFileLoadTestCase(unittest.TestCase):
def test_load_unexisting_file(self):
with self.assertRaises(FileNotFoundError):
restretto.load("test-data/unesixtant_file.yml")
def test_load_empty_file(self):
data = restretto.load("test-data/empty.yml")
self.assertFalse(data)
def test_load_bad_file(self):
with self.assertRaises(Exception):
restretto.load("test-data/broken/bad.yml")
def test_load_valid_file(self):
data = restretto.load("test-data/valid/simple.yml")
self.assertEqual(len(data), 1)
def test_empty_resources(self):
data = restretto.load('test-data/empty-resources.yml')
self.assertFalse(data)
def test_missing_resources(self):
data = restretto.load('test-data/missing-resources.yml')
self.assertFalse(data)
class LoaderDirLoadTestCase(unittest.TestCase):
def test_load_from_dir(self):
data = restretto.load("test-data/valid")
self.assertEqual(len(data), 3)
def test_load_from_unexistant_dir(self):
with self.assertRaises(FileNotFoundError):
restretto.load("test-data/missing-dir")
def test_load_from_bad_dir(self):
with self.assertRaises(Exception):
restretto.load("test-data/broken")
class OptionsTestCase(unittest.TestCase):
def test_convert_single(self):
encoded = "key=val"
options = restretto.cli.options(encoded)
self.assertEqual(options, {"key": "val"})
def test_convert_multi(self):
encoded = "key=val,other=second, yet=another again "
options = restretto.cli.options(encoded)
expected = {
"key": "val",
"other": "second",
"yet": "another again"
}
self.assertEqual(options, expected)
def test_convert_empty_val(self):
encoded = "oops=, second=two"
expected = {
"oops": "",
"second": "two"
}
options = restretto.cli.options(encoded)
self.assertEqual(options, expected)
def test_convert_empty_key(self):
with self.assertRaises(restretto.cli.ArgumentTypeError):
restretto.cli.options("=value")
def test_convert_empty_keyval(self):
with self.assertRaises(restretto.cli.ArgumentTypeError):
restretto.cli.options(" = ")
if __name__ == "__main__":
unittest.main()