tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_load.py (1404B)


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