-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathtest_server__embed.py
More file actions
260 lines (211 loc) · 10.3 KB
/
test_server__embed.py
File metadata and controls
260 lines (211 loc) · 10.3 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
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
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations # isort:skip
import pytest ; pytest
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# External imports
import bs4
# Module under test
import bokeh.embed.server as bes # isort:skip
#-----------------------------------------------------------------------------
# Setup
#-----------------------------------------------------------------------------
@pytest.fixture
def test_plot() -> None:
from bokeh.plotting import figure
test_plot = figure()
test_plot.circle([1, 2], [2, 3])
return test_plot
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
class TestServerDocument:
def test_invalid_resources_param(self) -> None:
with pytest.raises(ValueError):
bes.server_document(url="http://localhost:8081/foo/bar/sliders", resources=123)
with pytest.raises(ValueError):
bes.server_document(url="http://localhost:8081/foo/bar/sliders", resources="whatever")
def test_resources_default_is_implicit(self) -> None:
r = bes.server_document(url="http://localhost:8081/foo/bar/sliders", resources="default")
assert 'resources=' not in r
def test_resources_none(self) -> None:
r = bes.server_document(url="http://localhost:8081/foo/bar/sliders", resources=None)
assert 'resources=none' in r
def test_general(self) -> None:
url = "http://localhost:8081/foo/bar/sliders"
r = bes.server_document(url=url)
assert 'bokeh-app-path=/foo/bar/sliders' in r
assert 'bokeh-absolute-url=http://localhost:8081/foo/bar/sliders' in r
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-app-path=/foo/bar/sliders&bokeh-absolute-url={url}\", true);"
assert request in script.string
def test_script_attrs_arguments_provided(self) -> None:
url = "http://localhost:5006"
r = bes.server_document(arguments=dict(foo=10))
assert 'foo=10' in r
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-absolute-url={url}&foo=10\", true);"
assert request in script.string
def test_script_attrs_url_provided_absolute_resources(self) -> None:
url = "http://localhost:8081/foo/bar/sliders"
r = bes.server_document(url=url)
assert 'bokeh-app-path=/foo/bar/sliders' in r
assert 'bokeh-absolute-url=http://localhost:8081/foo/bar/sliders' in r
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-app-path=/foo/bar/sliders&bokeh-absolute-url={url}\", true);"
assert request in script.string
def test_script_attrs_url_provided(self) -> None:
url = "http://localhost:8081/foo/bar/sliders"
r = bes.server_document(url=url, relative_urls=True)
assert 'bokeh-app-path=/foo/bar/sliders' in r
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-app-path=/foo/bar/sliders\", true);"
assert request in script.string
class TestServerSession:
def test_return_type(self, test_plot) -> None:
r = bes.server_session(test_plot, session_id='fakesession')
assert isinstance(r, str)
def test_script_attrs_session_id_provided(self, test_plot) -> None:
url = "http://localhost:5006"
r = bes.server_session(test_plot, session_id='fakesession')
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-absolute-url={url}\", true);"
assert request in script.string
assert 'xhr.setRequestHeader("Bokeh-Session-Id", "fakesession")' in script.string
def test_invalid_resources_param(self, test_plot) -> None:
with pytest.raises(ValueError):
bes.server_session(test_plot, session_id='fakesession', resources=123)
with pytest.raises(ValueError):
bes.server_session(test_plot, session_id='fakesession', resources="whatever")
def test_resources_default_is_implicit(self, test_plot) -> None:
r = bes.server_session(test_plot, session_id='fakesession', resources="default")
assert 'resources=' not in r
def test_resources_none(self, test_plot) -> None:
r = bes.server_session(test_plot, session_id='fakesession', resources=None)
assert 'resources=none' in r
def test_model_none(self) -> None:
url = "http://localhost:5006"
r = bes.server_session(None, session_id='fakesession')
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-absolute-url={url}"
assert request in script.string
assert 'xhr.setRequestHeader("Bokeh-Session-Id", "fakesession")' in script.string
def test_general(self, test_plot) -> None:
url = "http://localhost:5006"
r = bes.server_session(test_plot, session_id='fakesession')
html = bs4.BeautifulSoup(r, "html.parser")
scripts = html.findAll(name='script')
assert len(scripts) == 1
script = scripts[0]
attrs = script.attrs
assert list(attrs) == ['id']
divid = attrs['id']
request = f"xhr.open('GET', \"{url}/autoload.js?bokeh-autoload-element={divid}&bokeh-absolute-url={url}\", true);"
assert request in script.string
assert 'xhr.setRequestHeader("Bokeh-Session-Id", "fakesession")' in script.string
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
class Test__clean_url:
def test_default(self) -> None:
assert bes._clean_url("default") == bes.DEFAULT_SERVER_HTTP_URL.rstrip("/")
def test_bad_ws(self) -> None:
with pytest.raises(ValueError):
bes._clean_url("ws://foo")
def test_arg(self) -> None:
assert bes._clean_url("http://foo/bar") == "http://foo/bar"
assert bes._clean_url("http://foo/bar/") == "http://foo/bar"
class Test__get_app_path:
def test_arg(self) -> None:
assert bes._get_app_path("foo") == "/foo"
assert bes._get_app_path("http://foo") == "/"
assert bes._get_app_path("http://foo/bar") == "/bar"
assert bes._get_app_path("https://foo") == "/"
assert bes._get_app_path("https://foo/bar") == "/bar"
class Test__process_arguments:
def test_None(self) -> None:
assert bes._process_arguments(None) == ""
def test_args(self) -> None:
args = dict(foo=10, bar="baz")
r = bes._process_arguments(args)
# order unspecified
assert r == "&foo=10&bar=baz" or r == "&bar=baz&foo=10"
def test_args_ignores_bokeh_prefixed(self) -> None:
args = dict(foo=10, bar="baz")
args["bokeh-junk"] = 20
r = bes._process_arguments(args)
# order unspecified
assert r == "&foo=10&bar=baz" or r == "&bar=baz&foo=10"
class Test__process_app_path:
def test_root(self) -> None:
assert bes._process_app_path("/") == ""
def test_arg(self) -> None:
assert bes._process_app_path("/stuff") == "&bokeh-app-path=/stuff"
class Test__process_relative_urls:
def test_True(self) -> None:
assert bes._process_relative_urls(True, "") == ""
assert bes._process_relative_urls(True, "/stuff") == ""
def test_Flase(self) -> None:
assert bes._process_relative_urls(False, "/stuff") == "&bokeh-absolute-url=/stuff"
class Test__process_resources:
def test_bad_input(self) -> None:
with pytest.raises(ValueError):
bes._process_resources("foo")
def test_None(self) -> None:
assert bes._process_resources(None) == "&resources=none"
def test_default(self) -> None:
assert bes._process_resources("default") == ""
def Test__src_path(object):
def test_args(self) -> None:
assert bes._src_path("http://foo", "1234") =="http://foo/autoload.js?bokeh-autoload-element=1234"
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------