diff --git a/tools/wptserve/tests/functional/base.py b/tools/wptserve/tests/functional/base.py index 190e385f1020a7a..960a62e53c22df5 100644 --- a/tools/wptserve/tests/functional/base.py +++ b/tools/wptserve/tests/functional/base.py @@ -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) diff --git a/tools/wptserve/tests/functional/docroot/bar.any.worker.js b/tools/wptserve/tests/functional/docroot/bar.any.worker.js new file mode 100644 index 000000000000000..66b7be39049eeda --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/bar.any.worker.js @@ -0,0 +1,9 @@ + +self.GLOBAL = { + isWindow: function() { return false; }, + isWorker: function() { return true; }, +}; +importScripts("/resources/testharness.js"); + +importScripts("/bar.any.js"); +done(); diff --git a/tools/wptserve/tests/functional/docroot/foo.any.html b/tools/wptserve/tests/functional/docroot/foo.any.html new file mode 100644 index 000000000000000..88e2665c6690421 --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.any.html @@ -0,0 +1,14 @@ + + + + + + + +
+ diff --git a/tools/wptserve/tests/functional/docroot/foo.any.serviceworker.html b/tools/wptserve/tests/functional/docroot/foo.any.serviceworker.html new file mode 100644 index 000000000000000..8dcb11a37687ff7 --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.any.serviceworker.html @@ -0,0 +1,15 @@ + + + + + + + diff --git a/tools/wptserve/tests/functional/docroot/foo.any.sharedworker.html b/tools/wptserve/tests/functional/docroot/foo.any.sharedworker.html new file mode 100644 index 000000000000000..277101697ff8cbe --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.any.sharedworker.html @@ -0,0 +1,9 @@ + + + + + + + diff --git a/tools/wptserve/tests/functional/docroot/foo.any.worker.html b/tools/wptserve/tests/functional/docroot/foo.any.worker.html new file mode 100644 index 000000000000000..f77edd971a4ec0c --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.any.worker.html @@ -0,0 +1,9 @@ + + + + + + + diff --git a/tools/wptserve/tests/functional/docroot/foo.window.html b/tools/wptserve/tests/functional/docroot/foo.window.html new file mode 100644 index 000000000000000..04c694ddf2aba20 --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.window.html @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tools/wptserve/tests/functional/docroot/foo.worker.html b/tools/wptserve/tests/functional/docroot/foo.worker.html new file mode 100644 index 000000000000000..3eddf36f1c3a911 --- /dev/null +++ b/tools/wptserve/tests/functional/docroot/foo.worker.html @@ -0,0 +1,9 @@ + + + + + + + diff --git a/tools/wptserve/tests/functional/test_handlers.py b/tools/wptserve/tests/functional/test_handlers.py index 759a096b7995707..069077bed94ca73 100644 --- a/tools/wptserve/tests/functional/test_handlers.py +++ b/tools/wptserve/tests/functional/test_handlers.py @@ -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): @@ -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()