summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/harness_unit/test_httpd.py
blob: b62e731ff1cb616c086d0c3fcf5374a5df3d888c (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import json
import os
import types

import six
from six.moves.urllib_request import urlopen

import mozunit
import pytest

from wptserve.handlers import json_handler

from marionette_harness.runner import httpd

here = os.path.abspath(os.path.dirname(__file__))
parent = os.path.dirname(here)
default_doc_root = os.path.join(os.path.dirname(parent), "www")


@pytest.fixture
def server():
    server = httpd.FixtureServer(default_doc_root)
    yield server
    server.stop()


def test_ctor():
    with pytest.raises(ValueError):
        httpd.FixtureServer("foo")
    httpd.FixtureServer(default_doc_root)


def test_start_stop(server):
    server.start()
    server.stop()


def test_get_url(server):
    server.start()
    url = server.get_url("/")
    assert isinstance(url, six.string_types)
    assert "http://" in url

    server.stop()
    with pytest.raises(httpd.NotAliveError):
        server.get_url("/")


def test_doc_root(server):
    server.start()
    assert isinstance(server.doc_root, six.string_types)
    server.stop()
    assert isinstance(server.doc_root, six.string_types)


def test_router(server):
    assert server.router is not None


def test_routes(server):
    assert server.routes is not None


def test_is_alive(server):
    assert server.is_alive == False
    server.start()
    assert server.is_alive == True


def test_handler(server):
    counter = 0

    @json_handler
    def handler(request, response):
        return {"count": counter}

    route = ("GET", "/httpd/test_handler", handler)
    server.router.register(*route)
    server.start()

    url = server.get_url("/httpd/test_handler")
    body = urlopen(url).read()
    res = json.loads(body)
    assert res["count"] == counter


if __name__ == "__main__":
    mozunit.main("-p", "no:terminalreporter", "--log-tbpl=-", "--capture", "no")