1#!/usr/bin/env python
2
3"""
4tests for mozfile.load
5"""
6
7from __future__ import absolute_import
8
9import mozunit
10import pytest
11
12from wptserve.handlers import handler
13from wptserve.server import WebTestHttpd
14
15from mozfile import load
16
17
18@pytest.fixture(name="httpd_url")
19def fixture_httpd_url():
20    """Yield a started WebTestHttpd server."""
21
22    @handler
23    def example(request, response):
24        """Example request handler."""
25        body = b"example"
26        return (
27            200,
28            [("Content-type", "text/plain"), ("Content-length", len(body))],
29            body,
30        )
31
32    httpd = WebTestHttpd(host="127.0.0.1", routes=[("GET", "*", example)])
33
34    httpd.start(block=False)
35    yield httpd.get_url()
36    httpd.stop()
37
38
39def test_http(httpd_url):
40    """Test with WebTestHttpd and a http:// URL."""
41    content = load(httpd_url).read()
42    assert content == b"example"
43
44
45@pytest.fixture(name="temporary_file")
46def fixture_temporary_file(tmpdir):
47    """Yield a path to a temporary file."""
48    foobar = tmpdir.join("foobar.txt")
49    foobar.write("hello world")
50
51    yield str(foobar)
52
53    foobar.remove()
54
55
56def test_file_path(temporary_file):
57    """Test loading from a file path."""
58    assert load(temporary_file).read() == "hello world"
59
60
61def test_file_url(temporary_file):
62    """Test loading from a file URL."""
63    assert load("file://%s" % temporary_file).read() == "hello world"
64
65
66if __name__ == "__main__":
67    mozunit.main()
68