-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest_runserver_serve.py
More file actions
189 lines (158 loc) · 6.53 KB
/
test_runserver_serve.py
File metadata and controls
189 lines (158 loc) · 6.53 KB
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
import json
import pathlib
import socket
from typing import Any, Dict
from unittest.mock import MagicMock
import pytest
from aiohttp.web import Application, AppKey, Request, Response
from aiohttp_jinja2 import static_root_key
from pytest_toolbox import mktree
from aiohttp_devtools.exceptions import AiohttpDevException
from aiohttp_devtools.runserver.config import Config
from aiohttp_devtools.runserver.log_handlers import fmt_size
from aiohttp_devtools.runserver.serve import (
LAST_RELOAD, STATIC_PATH, STATIC_URL, WS, check_port_open, cleanup_aux_app,
modify_main_app, src_reload)
from .conftest import SIMPLE_APP, create_future
async def test_check_port_open(unused_tcp_port_factory):
port = unused_tcp_port_factory()
await check_port_open(port, delay=0.001)
async def test_check_port_not_open(unused_tcp_port_factory):
port = unused_tcp_port_factory()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(('0.0.0.0', port))
with pytest.raises(AiohttpDevException):
await check_port_open(port, delay=0.001)
async def test_aux_reload(smart_caplog):
aux_app = Application()
ws = MagicMock()
ws.send_str = MagicMock(return_value=create_future())
aux_app[LAST_RELOAD] = [0, 0.]
aux_app[STATIC_PATH] = "/path/to/static_files/"
aux_app[STATIC_URL] = "/static/"
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
assert 1 == await src_reload(aux_app, '/path/to/static_files/the_file.js')
assert ws.send_str.call_count == 1
send_obj = json.loads(ws.send_str.call_args[0][0])
expected_path = str(pathlib.Path('/static/the_file.js'))
assert send_obj == {
'command': 'reload',
'path': expected_path,
'liveCSS': True,
'liveImg': True,
}
assert 'adev.server.aux INFO: prompted reload of {} on 1 client\n'.format(expected_path) == smart_caplog
async def test_aux_reload_no_path():
aux_app = Application()
ws = MagicMock()
ws.send_str = MagicMock(return_value=create_future())
aux_app[LAST_RELOAD] = [0, 0.]
aux_app[STATIC_PATH] = "/path/to/static_files/"
aux_app[STATIC_URL] = "/static/"
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
assert 1 == await src_reload(aux_app)
assert ws.send_str.call_count == 1
send_obj = json.loads(ws.send_str.call_args[0][0])
assert send_obj == {
'command': 'reload',
'path': '/foo/bar',
'liveCSS': True,
'liveImg': True,
}
async def test_aux_reload_html_different():
aux_app = Application()
ws = MagicMock()
ws.send_str = MagicMock(return_value=create_future())
aux_app[LAST_RELOAD] = [0, 0.]
aux_app[STATIC_PATH] = "/path/to/static_files/"
aux_app[STATIC_URL] = "/static/"
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
assert 0 == await src_reload(aux_app, '/path/to/static_files/foo/bar.html')
assert ws.send_str.call_count == 0
async def test_aux_reload_no_static_path():
aux_app = Application()
ws = MagicMock()
ws.send_str = MagicMock(return_value=create_future())
aux_app[LAST_RELOAD] = [0, 0.]
aux_app[STATIC_PATH] = "."
aux_app[STATIC_URL] = "/static/"
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
assert 0 == await src_reload(aux_app, '/path/to/static_files/foo/.bar.html.kate-swp')
assert ws.send_str.call_count == 0
async def test_aux_reload_runtime_error(smart_caplog):
aux_app = Application()
ws = MagicMock()
ws.send_str = MagicMock(return_value=create_future())
ws.send_str = MagicMock(side_effect=RuntimeError('foobar'))
aux_app[LAST_RELOAD] = [0, 0.]
aux_app[STATIC_PATH] = "/path/to/static_files/"
aux_app[STATIC_URL] = "/static/"
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
assert 0 == await src_reload(aux_app)
assert ws.send_str.call_count == 1
assert 'adev.server.aux ERROR: Error broadcasting change to /foo/bar, RuntimeError: foobar\n' == smart_caplog
async def test_aux_cleanup():
aux_app = Application()
aux_app.on_cleanup.append(cleanup_aux_app)
ws = MagicMock()
ws.close = MagicMock(return_value=create_future())
aux_app[WS] = set(((ws, "/foo/bar"),)) # type: ignore[misc]
aux_app.freeze()
await aux_app.cleanup()
assert ws.close.call_count == 1
@pytest.mark.parametrize('value,result', [
(None, ''),
('', ''),
(1000, '1000B'),
(2000, '2.0KB'),
])
def test_fmt_size_large(value, result):
assert fmt_size(value) == result
class DummyApplication(Dict[AppKey[Any], object]):
_debug = False
def __init__(self):
self.on_response_prepare = []
self.middlewares = []
self.router = MagicMock()
self[static_root_key] = '/static/'
self._subapps = []
def add_subapp(self, path, app):
self._subapps.append(app)
def test_modify_main_app_all_off(tmpworkdir):
mktree(tmpworkdir, SIMPLE_APP)
config = Config(app_path="app.py", livereload=False, host="foobar.com",
static_path=".", browser_cache=True)
app = DummyApplication()
subapp = DummyApplication()
app.add_subapp("/sub/", subapp)
modify_main_app(app, config) # type: ignore[arg-type]
assert len(app.on_response_prepare) == 0
assert len(app.middlewares) == 0
assert app[static_root_key] == "http://foobar.com:8001/static"
assert subapp[static_root_key] == "http://foobar.com:8001/static"
assert app._debug is True
def test_modify_main_app_all_on(tmpworkdir):
mktree(tmpworkdir, SIMPLE_APP)
config = Config(app_path='app.py', static_path='.')
app = DummyApplication()
subapp = DummyApplication()
app.add_subapp("/sub/", subapp)
modify_main_app(app, config) # type: ignore[arg-type]
assert len(app.on_response_prepare) == 1
assert len(app.middlewares) == 2
assert app[static_root_key] == "http://localhost:8001/static"
assert subapp[static_root_key] == "http://localhost:8001/static"
assert app._debug is True
async def test_modify_main_app_on_prepare(tmpworkdir):
mktree(tmpworkdir, SIMPLE_APP)
config = Config(app_path='app.py', host='foobar.com')
app = DummyApplication()
modify_main_app(app, config) # type: ignore[arg-type]
on_prepare = app.on_response_prepare[0]
request = MagicMock(spec=Request)
request.path = '/'
response = MagicMock(spec=Response)
response.body = b'<h1>body</h1>'
response.content_type = 'text/html'
await on_prepare(request, response)
assert response.body == b'<h1>body</h1>\n<script src="http://foobar.com:8001/livereload.js"></script>\n'