def test_content_default_url(provider: Provider) -> None: content = "testing default url" resource1 = provider.create(content=content, extension="txt") resource2 = provider.create(content=content, extension="txt") path = resource1.url.split("/")[-1] assert path.endswith(".txt") assert len(path) > 4 assert resource1.url == resource2.url
def test_file_resource(provider: Provider, http_client: HTTPClient) -> None: content = b"file content" with tempfile.NamedTemporaryFile(suffix=".txt") as f: f.write(content) f.flush() resource = provider.create(filepath=f.name) assert isinstance(resource, Resource) assert http_client.fetch(resource.url).body == content
def _serve(cls, content: str, js_resources: Dict[str, str]) -> str: if cls._provider is None: cls._provider = Provider() resource = cls._provider.create( content=content, route="", headers={"Access-Control-Allow-Origin": "*"}, ) cls._resources[resource.url] = resource for route, content in js_resources.items(): cls._resources[route] = cls._provider.create(content=content, route=route,) return resource.url
def test_handler_resource(provider: Provider, http_client: HTTPClient) -> None: class Handler: def __init__(self) -> None: self.count = 0 def __call__(self) -> str: self.count += 1 return f"Testing handler resource {self.count}\n" resource = provider.create(handler=Handler(), extension="txt") assert isinstance(resource, Resource) for i in range(1, 3): assert (http_client.fetch( resource.url).body.decode() == f"Testing handler resource {i}\n")
def test_content_route(provider: Provider, http_client: HTTPClient, route: str) -> None: content = "testing route {!r}".format(route) resource = provider.create(content=content, route=route) assert resource.url.split("/")[-1] == route.lstrip("/") assert http_client.fetch(resource.url).body == content.encode()
def test_content_resource(provider: Provider, http_client: HTTPClient) -> None: content = "testing content resource" resource = provider.create(content=content, extension="txt") assert isinstance(resource, Resource) assert resource.url.endswith("txt") assert http_client.fetch(resource.url).body.decode() == content
def provider() -> Iterator[Provider]: provider = Provider() yield provider provider.stop()
def test_expected_404(provider: Provider, http_client: HTTPClient) -> None: resource = provider.create(content="some new content") url = resource.url + ".html" with pytest.raises(HTTPClientError) as err: http_client.fetch(url) assert err.value.code == 404
def provider(): provider = Provider() yield provider provider.stop()