summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozfile/tests/test_load.py
blob: 7a3896e33b2af1d7e8094760695901480173a76c (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
#!/usr/bin/env python

"""
tests for mozfile.load
"""

import mozunit
import pytest
from mozfile import load
from wptserve.handlers import handler
from wptserve.server import WebTestHttpd


@pytest.fixture(name="httpd_url")
def fixture_httpd_url():
    """Yield a started WebTestHttpd server."""

    @handler
    def example(request, response):
        """Example request handler."""
        body = b"example"
        return (
            200,
            [("Content-type", "text/plain"), ("Content-length", len(body))],
            body,
        )

    httpd = WebTestHttpd(host="127.0.0.1", routes=[("GET", "*", example)])

    httpd.start()
    yield httpd.get_url()
    httpd.stop()


def test_http(httpd_url):
    """Test with WebTestHttpd and a http:// URL."""
    content = load(httpd_url).read()
    assert content == b"example"


@pytest.fixture(name="temporary_file")
def fixture_temporary_file(tmpdir):
    """Yield a path to a temporary file."""
    foobar = tmpdir.join("foobar.txt")
    foobar.write("hello world")

    yield str(foobar)

    foobar.remove()


def test_file_path(temporary_file):
    """Test loading from a file path."""
    assert load(temporary_file).read() == "hello world"


def test_file_url(temporary_file):
    """Test loading from a file URL."""
    assert load("file://%s" % temporary_file).read() == "hello world"


if __name__ == "__main__":
    mozunit.main()