testing/web-platform/tests/tools/serve/test_serve.py
author jugglinmike <mike@mikepennisi.com>
Wed, 22 Aug 2018 13:21:50 +0000
changeset 831112 7e8799b402ec66964a0751783a971bc01b9b696e
parent 829517 c12deabac46d90f5ae89964a219461f947da3ff2
permissions -rw-r--r--
Bug 1484577 [wpt PR 12565] - [wptserver] Correct value in unit test, a=testonly Automatic update from web-platform-tests[wptserver] Correct value in unit test (#12565) -- wpt-commits: 1b3c289f0ca989acf84b9b9e6a5f74071621af8e wpt-pr: 12565

import pickle
import platform
import os

import pytest

import localpaths
from . import serve
from .serve import ConfigBuilder


@pytest.mark.skipif(platform.uname()[0] == "Windows",
                    reason="Expected contents are platform-dependent")
def test_make_hosts_file_nix():
    with ConfigBuilder(ports={"http": [8000]},
                       browser_host="foo.bar",
                       alternate_hosts={"alt": "foo2.bar"},
                       subdomains={"a", "b"},
                       not_subdomains={"x, y"}) as c:
        hosts = serve.make_hosts_file(c, "192.168.42.42")
        lines = hosts.split("\n")
        assert set(lines) == {"",
                              "192.168.42.42\tfoo.bar",
                              "192.168.42.42\tfoo2.bar",
                              "192.168.42.42\ta.foo.bar",
                              "192.168.42.42\ta.foo2.bar",
                              "192.168.42.42\tb.foo.bar",
                              "192.168.42.42\tb.foo2.bar"}
        assert lines[-1] == ""

@pytest.mark.skipif(platform.uname()[0] != "Windows",
                    reason="Expected contents are platform-dependent")
def test_make_hosts_file_windows():
    with ConfigBuilder(ports={"http": [8000]},
                       browser_host="foo.bar",
                       alternate_hosts={"alt": "foo2.bar"},
                       subdomains={"a", "b"},
                       not_subdomains={"x", "y"}) as c:
        hosts = serve.make_hosts_file(c, "192.168.42.42")
        lines = hosts.split("\n")
        assert set(lines) == {"",
                              "0.0.0.0\tx.foo.bar",
                              "0.0.0.0\tx.foo2.bar",
                              "0.0.0.0\ty.foo.bar",
                              "0.0.0.0\ty.foo2.bar",
                              "192.168.42.42\tfoo.bar",
                              "192.168.42.42\tfoo2.bar",
                              "192.168.42.42\ta.foo.bar",
                              "192.168.42.42\ta.foo2.bar",
                              "192.168.42.42\tb.foo.bar",
                              "192.168.42.42\tb.foo2.bar"}
        assert lines[-1] == ""


def test_ws_doc_root_default():
    with ConfigBuilder() as c:
        assert c.ws_doc_root == os.path.join(localpaths.repo_root, "websockets", "handlers")


def test_init_ws_doc_root():
    with ConfigBuilder(ws_doc_root="/") as c:
        assert c.doc_root == localpaths.repo_root  # check this hasn't changed
        assert c.ws_doc_root == "/"


def test_set_ws_doc_root():
    cb = ConfigBuilder()
    cb.ws_doc_root = "/"
    with cb as c:
        assert c.doc_root == localpaths.repo_root  # check this hasn't changed
        assert c.ws_doc_root == "/"


def test_pickle():
    # Ensure that the config object can be pickled
    with ConfigBuilder() as c:
        pickle.dumps(c)