def test_with_server(self): class TestResource: def on_get(self, request): return {"status": "ok"} router = ResourceRouter() router.add_route("/foo/bar", TestResource()) with proxy_server(ResourceRouterProxyListener(router)) as url: response = requests.get(f"{url}/foo/bar") assert response.ok assert response.json() == {"status": "ok"} # test with query response = requests.get(f"{url}/foo/bar?hello=there") assert response.ok assert response.json() == {"status": "ok"} response = requests.get(f"{url}/foo") assert not response.ok assert response.status_code == 404 response = requests.post(f"{url}/foo/bar") assert not response.ok assert response.status_code == 404
def test_request_response(self): # define a AWS provider class Provider: @handler("ListQueues", expand=False) def list_queues(self, context, request): return { "QueueUrls": [ "http://localhost:4566/000000000000/foo-queue", ], } # create a proxy listener for the provider listener = AwsApiListener("sqs", Provider()) # start temp proxy listener and connect to it with testutil.proxy_server(listener) as url: client = boto3.client( "sqs", aws_access_key_id="test", aws_secret_access_key="test", aws_session_token="test", region_name="us-east-1", endpoint_url=url, ) result = client.list_queues() assert result["QueueUrls"] == [ "http://localhost:4566/000000000000/foo-queue", ]
def test_cloudformation_ui(self): with proxy_server(LocalstackResourceHandler()) as url: # make sure it renders response = requests.get(f"{url}/_localstack/cloudformation/deploy") assert response.ok assert "</html>" in response.text, "deploy UI did not render HTML" assert "text/html" in response.headers.get("content-type", "")
def test_fallthrough(self): with proxy_server(LocalstackResourceHandler()) as url: # some other error is thrown by the proxy if there are no more listeners response = requests.get(f"{url}/foobar") assert not response.ok assert not response.status_code == 404 # internal paths are 404ed response = requests.get(f"{url}/_localstack/foobar") assert not response.ok assert response.status_code == 404
def test_health(self, monkeypatch): with proxy_server(LocalstackResourceHandler()) as url: # legacy endpoint response = requests.get(f"{url}/health") assert response.ok assert "services" in response.json() # new internal endpoint response = requests.get(f"{url}/_localstack/health") assert response.ok assert "services" in response.json()
def test_request_response(self): provider_calls = [] fallback_calls = [] class Provider: @handler("ListQueues", expand=False) def list_queues(self, context, request): return { "QueueUrls": [ "http://*****:*****@handler("DeleteQueue", expand=False) def delete_queue(self, context, request): provider_calls.append((context, request)) raise NotImplementedError class Fallback(ProxyListener): def forward_request(self, method: str, path: str, data, headers): fallback_calls.append((method, path, data, headers)) return requests_response("<Response></Response>") # create a proxy listener for the provider listener = AsfWithFallbackListener("sqs", Provider(), Fallback()) # start temp proxy listener and connect to it with testutil.proxy_server(listener) as url: client = boto3.client( "sqs", aws_access_key_id="test", aws_secret_access_key="test", aws_session_token="test", region_name="us-east-1", endpoint_url=url, ) # check that provider is called correctly result = client.list_queues() assert result["QueueUrls"] == [ "http://localhost:4566/000000000000/foo-queue", ] assert len(provider_calls) == 0 assert len(fallback_calls) == 0 # check that fallback is called correctly client.delete_queue( QueueUrl="http://localhost:4566/000000000000/somequeue") assert len(provider_calls) == 1 assert len(fallback_calls) == 1
def test_fallthrough(self): class RaiseError(ProxyListener): def forward_request(self, method, path, data, headers): raise ValueError("this error is expected") with proxy_server([LocalstackResourceHandler(), RaiseError()]) as url: # the RaiseError handler is called since this is not a /_localstack resource response = requests.get(f"{url}/foobar") assert not response.ok assert response.status_code >= 500 # internal paths are 404ed response = requests.get(f"{url}/_localstack/foobar") assert not response.ok assert response.status_code == 404