Skip to content

Commit 40a7f33

Browse files
committed
add asgi tests
1 parent 0562ff2 commit 40a7f33

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

tests/asgi_server.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,28 @@ async def app(scope, receive, send):
119119
# a response splitting attempt
120120
headers.append((b'referer', scope['path'].encode('utf-8')))
121121

122-
await send({
123-
'type': 'http.response.start',
124-
'status': 200,
125-
'headers': headers
126-
})
122+
if scope['path'].startswith('/start'):
123+
await send({
124+
'type': 'http.response.start',
125+
'status': 200,
126+
'headers': headers
127+
})
128+
129+
if not scope['path'].startswith('/body'):
130+
await send({
131+
'type': 'http.response.start',
132+
'status': 200,
133+
'headers': headers
134+
})
135+
136+
if scope['path'].startswith('/invalid'):
137+
await send({'type': 'invalid.message'})
138+
127139
await send({
128140
'type': 'http.response.body',
129141
'body': b'Hello world!'
130142
})
131143

132144
if __name__ == '__main__':
133-
tremolo.run(app, host=ASGI_HOST, port=ASGI_PORT, debug=True, worker_num=2)
145+
tremolo.run(app, host=ASGI_HOST, port=ASGI_PORT, debug=True, worker_num=2,
146+
experimental=True, client_max_body_size=100 * 1048576)

tests/test_asgi_server.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,39 @@ def test_app_exits_early(self):
3636
b'HTTP/1.0 500 Internal Server Error')
3737
self.assertEqual(body, b'Internal Server Error')
3838

39+
def test_double_start(self):
40+
header, body = getcontents(
41+
host=ASGI_HOST,
42+
port=ASGI_PORT,
43+
raw=b'GET /start HTTP/1.0\r\n\r\n'
44+
)
45+
46+
self.assertEqual(header[:header.find(b'\r\n')],
47+
b'HTTP/1.0 500 Internal Server Error')
48+
self.assertEqual(body, b'response already started')
49+
50+
def test_body_before_start(self):
51+
header, body = getcontents(
52+
host=ASGI_HOST,
53+
port=ASGI_PORT,
54+
raw=b'GET /body HTTP/1.0\r\n\r\n'
55+
)
56+
57+
self.assertEqual(header[:header.find(b'\r\n')],
58+
b'HTTP/1.0 500 Internal Server Error')
59+
self.assertEqual(body, b'response not started')
60+
61+
def test_invalid_message_type(self):
62+
header, body = getcontents(
63+
host=ASGI_HOST,
64+
port=ASGI_PORT,
65+
raw=b'GET /invalid HTTP/1.0\r\n\r\n'
66+
)
67+
68+
self.assertEqual(header[:header.find(b'\r\n')],
69+
b'HTTP/1.0 500 Internal Server Error')
70+
self.assertEqual(body, b'invalid ASGI message type')
71+
3972
def test_get_ok_10(self):
4073
header, body = getcontents(host=ASGI_HOST,
4174
port=ASGI_PORT,
@@ -136,9 +169,6 @@ def test_sec_response_splitting(self):
136169
self.assertEqual(header[:header.find(b'\r\n')],
137170
b'HTTP/1.1 500 Internal Server Error')
138171
self.assertFalse(b'\r\nContent-Type: text/plain' in header)
139-
self.assertTrue(
140-
b'name or value cannot contain illegal characters' in body
141-
)
142172

143173
def test_websocket(self):
144174
header, body = getcontents(

0 commit comments

Comments
 (0)