Skip to content

Commit

Permalink
Add unit tests for wrapper handlers in tools/serve
Browse files Browse the repository at this point in the history
Add unit tests for all kinds of wrapper handlers in tools/serve/serve.py.
See #12452
  • Loading branch information
qiuzhong committed Nov 9, 2018
1 parent 47f0061 commit 41f5197
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 1 deletion.
47 changes: 47 additions & 0 deletions tools/wptserve/tests/functional/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,50 @@ def setup_method(self, test_method):
def teardown_method(self, test_method):
self.conn.close()
self.server.stop()


class TestWrapperHandlerUsingServer(TestUsingServer):
'''For a wrapper handler, a .js dummy testing file is requried to render
the html file. This class extends the TestUsingServer and do some some
extra work: it tries to generate the dummy .js file in setUp and
remove it in tearDown.'''
dummy_js_files = {}

def gen_js_file(self, filename, empty=True, content=''):
self.remove_js_file(filename)

with open(filename, 'w') as fp:
if not empty:
fp.write(content)

def remove_js_file(self, filename):
if os.path.exists(filename):
os.remove(filename)

def setUp(self):
super(TestWrapperHandlerUsingServer, self).setUp()

for filename, content in self.dummy_js_files.items():
filepath = os.path.join(doc_root, filename)
if content == '':
self.gen_js_file(filepath)
else:
self.gen_js_file(filepath, False, content)

def run_wrapper_test(self, req_file, header_data, wrapper_handler):
route = ('GET', req_file, wrapper_handler())
self.server.router.register(*route)

resp = self.request(route[1])
self.assertEqual(200, resp.getcode())
self.assertEqual(header_data, resp.info()['Content-Type'])

with open(os.path.join(doc_root, req_file), 'r') as fp:
self.assertEqual(fp.read(), resp.read())

def tearDown(self):
super(TestWrapperHandlerUsingServer, self).tearDown()

for filename, _ in self.dummy_js_files.items():
filepath = os.path.join(doc_root, filename)
self.remove_js_file(filepath)
9 changes: 9 additions & 0 deletions tools/wptserve/tests/functional/docroot/bar.any.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");

importScripts("/bar.any.js");
done();
14 changes: 14 additions & 0 deletions tools/wptserve/tests/functional/docroot/foo.any.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<meta charset=utf-8>

<script>
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
};
</script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id=log></div>
<script src="/foo.any.js"></script>
15 changes: 15 additions & 0 deletions tools/wptserve/tests/functional/docroot/foo.any.serviceworker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<meta charset=utf-8>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
(async function() {
const scope = 'does/not/exist';
let reg = await navigator.serviceWorker.getRegistration(scope);
if (reg) await reg.unregister();
reg = await navigator.serviceWorker.register("/foo.any.worker.js", {scope});
fetch_tests_from_worker(reg.installing);
})();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<meta charset=utf-8>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new SharedWorker("/foo.any.worker.js"));
</script>
9 changes: 9 additions & 0 deletions tools/wptserve/tests/functional/docroot/foo.any.worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<meta charset=utf-8>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new Worker("/foo.any.worker.js"));
</script>
8 changes: 8 additions & 0 deletions tools/wptserve/tests/functional/docroot/foo.window.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!doctype html>
<meta charset=utf-8>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id=log></div>
<script src="/foo.window.js"></script>
9 changes: 9 additions & 0 deletions tools/wptserve/tests/functional/docroot/foo.worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<meta charset=utf-8>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new Worker("/foo.worker.js"));
</script>
64 changes: 63 additions & 1 deletion tools/wptserve/tests/functional/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
from six.moves.urllib.error import HTTPError

wptserve = pytest.importorskip("wptserve")
from .base import TestUsingServer, TestUsingH2Server, doc_root
from .base import TestUsingServer, TestUsingH2Server, \
TestWrapperHandlerUsingServer, doc_root

from serve.serve import WorkersHandler, WindowHandler, AnyHtmlHandler,\
SharedWorkersHandler, ServiceWorkersHandler, AnyWorkerHandler

class TestFileHandler(TestUsingServer):
def test_GET(self):
Expand Down Expand Up @@ -388,5 +391,64 @@ def test_requesting_multiple_resources(self):
assert resp.read() == ''


class TestWorkersHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'foo.worker.js': '',
'foo.any.js': ''}

def test_any_worker_html(self):
self.run_wrapper_test('foo.any.worker.html',
'text/html', WorkersHandler)

def test_worker_html(self):
self.run_wrapper_test('foo.worker.html',
'text/html', WorkersHandler)


class TestWindowHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'foo.window.js': ''}

def test_window_html(self):
self.run_wrapper_test('foo.window.html',
'text/html', WindowHandler)


class TestAnyHtmlHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'foo.any.js': ''}

def test_any_html(self):
self.run_wrapper_test('foo.any.html',
'text/html', AnyHtmlHandler)


class TestSharedWorkersHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'foo.any.js': '// META: global=sharedworker\n'}

def test_any_sharedworkers_html(self):
self.run_wrapper_test('foo.any.sharedworker.html',
'text/html', SharedWorkersHandler)


class TestServiceWorkersHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'foo.any.js': '// META: global=serviceworker\n'}

def test_serviceworker_html(self):
self.run_wrapper_test('foo.any.serviceworker.html',
'text/html', ServiceWorkersHandler)


class TestAnyWorkerHandler(TestWrapperHandlerUsingServer):

dummy_js_files = {'bar.any.js': ''}

def test_any_work_js(self):
self.run_wrapper_test('bar.any.worker.js', 'text/javascript',
AnyWorkerHandler)


if __name__ == '__main__':
unittest.main()

0 comments on commit 41f5197

Please sign in to comment.