/
test_hello.py
119 lines (82 loc) · 3.6 KB
/
test_hello.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
import io
from testtools.matchers import Contains
import falcon
from . import helpers
class HelloResource:
sample_status = '200 OK'
sample_body = 'Hello World! ' + helpers.rand_string(0, 256 * 1024)
def __init__(self, mode):
self.called = False
self.mode = mode
def on_get(self, req, resp):
self.called = True
self.req, self.resp = req, resp
resp.status = falcon.HTTP_200
if 'stream' in self.mode:
raw_body = self.sample_body.encode('utf-8')
resp.stream = io.BytesIO(raw_body)
if 'stream_len' in self.mode:
resp.stream_len = len(raw_body)
if 'body' in self.mode:
resp.body = self.sample_body
def on_head(self, req, resp):
self.on_get(req, resp)
class NoStatusResource:
def on_get(self, req, resp):
pass
class TestHelloWorld(helpers.TestSuite):
def prepare(self):
self.resource = HelloResource('body')
self.api.add_route(self.test_route, self.resource)
self.chunked_resource = HelloResource('stream')
self.api.add_route('/chunked-stream', self.chunked_resource)
self.stream_resource = HelloResource('stream, stream_len')
self.api.add_route('/stream', self.stream_resource)
self.no_status_resource = NoStatusResource()
self.api.add_route('/nostatus', self.no_status_resource)
self.root_resource = helpers.TestResource()
self.api.add_route('', self.root_resource)
def test_empty_route(self):
self._simulate_request('')
self.assertTrue(self.root_resource.called)
def test_route_negative(self):
bogus_route = self.test_route + 'x'
self._simulate_request(bogus_route)
# Ensure the request was NOT routed to resource
self.assertFalse(self.resource.called)
self.assertEquals(self.srmock.status, falcon.HTTP_404)
def test_body(self):
body = self._simulate_request(self.test_route)
resp = self.resource.resp
self.assertEquals(self.srmock.status, self.resource.sample_status)
self.assertEquals(resp.status, self.resource.sample_status)
self.assertEquals(resp.body, self.resource.sample_body)
self.assertEquals(body, [self.resource.sample_body])
def test_no_body_on_head(self):
body = self._simulate_request(self.test_route, method='HEAD')
self.assertEquals(body, [])
self.assertEquals(self.srmock.status, falcon.HTTP_200)
def test_stream_chunked(self):
src = self._simulate_request('/chunked-stream')
dest = io.BytesIO()
for chunk in src:
dest.write(chunk)
self.assertEqual(dest.getvalue().encode('utf-8'),
self.chunked_resource.sample_body.encode('utf-8'))
for header in self.srmock.headers:
self.assertNotEqual(header[0].lower(), 'content-length')
def test_stream_known_len(self):
src = self._simulate_request('/stream')
dest = io.BytesIO()
for chunk in src:
dest.write(chunk)
expected_len = self.stream_resource.resp.stream_len
content_length = ('Content-Length', str(expected_len))
self.assertThat(self.srmock.headers, Contains(content_length))
self.assertEqual(dest.tell(), expected_len)
self.assertEqual(dest.getvalue().encode('utf-8'),
self.chunked_resource.sample_body.encode('utf-8'))
def test_status_not_set(self):
body = self._simulate_request('/nostatus')
self.assertEqual(body, [])
self.assertEqual(self.srmock.status, falcon.HTTP_200)