test_httpd.py (1962B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import json 6 import os 7 from urllib.request import urlopen 8 9 import mozunit 10 import pytest 11 12 from wptserve.handlers import json_handler 13 14 from marionette_harness.runner import httpd 15 16 here = os.path.abspath(os.path.dirname(__file__)) 17 parent = os.path.dirname(here) 18 default_doc_root = os.path.join(os.path.dirname(parent), "www") 19 20 21 @pytest.fixture 22 def server(): 23 server = httpd.FixtureServer(default_doc_root) 24 yield server 25 server.stop() 26 27 28 def test_ctor(): 29 with pytest.raises(ValueError): 30 httpd.FixtureServer("foo") 31 httpd.FixtureServer(default_doc_root) 32 33 34 def test_start_stop(server): 35 server.start() 36 server.stop() 37 38 39 def test_get_url(server): 40 server.start() 41 url = server.get_url("/") 42 assert isinstance(url, str) 43 assert "http://" in url 44 45 server.stop() 46 with pytest.raises(httpd.NotAliveError): 47 server.get_url("/") 48 49 50 def test_doc_root(server): 51 server.start() 52 assert isinstance(server.doc_root, str) 53 server.stop() 54 assert isinstance(server.doc_root, str) 55 56 57 def test_router(server): 58 assert server.router is not None 59 60 61 def test_routes(server): 62 assert server.routes is not None 63 64 65 def test_is_alive(server): 66 assert server.is_alive == False 67 server.start() 68 assert server.is_alive == True 69 70 71 def test_handler(server): 72 counter = 0 73 74 @json_handler 75 def handler(request, response): 76 return {"count": counter} 77 78 route = ("GET", "/httpd/test_handler", handler) 79 server.router.register(*route) 80 server.start() 81 82 url = server.get_url("/httpd/test_handler") 83 body = urlopen(url).read() 84 res = json.loads(body) 85 assert res["count"] == counter 86 87 88 if __name__ == "__main__": 89 mozunit.main("-p", "no:terminalreporter", "--log-tbpl=-", "--capture", "no")