Best Python code snippet using localstack_python
test_dispatcher.py
Source:test_dispatcher.py
...3from localstack.http import Request, Response, Router4from localstack.http.dispatcher import resource_dispatcher5class TestResourceDispatcher:6 def test_dispatch_to_correct_function(self):7 router = Router(dispatcher=resource_dispatcher(pass_response=False))8 requests = []9 class TestResource:10 def on_get(self, req):11 requests.append(req)12 return "GET/OK"13 def on_post(self, req):14 requests.append(req)15 return {"ok": "POST"}16 router.add("/health", TestResource())17 request1 = Request("GET", "/health")18 request2 = Request("POST", "/health")19 assert router.dispatch(request1).get_data(True) == "GET/OK"20 assert router.dispatch(request1).get_data(True) == "GET/OK"21 assert router.dispatch(request2).json == {"ok": "POST"}22 assert len(requests) == 323 assert requests[0] is request124 assert requests[1] is request125 assert requests[2] is request226 def test_dispatch_to_non_existing_method_raises_exception(self):27 router = Router(dispatcher=resource_dispatcher(pass_response=False))28 class TestResource:29 def on_post(self, request):30 return "POST/OK"31 router.add("/health", TestResource())32 with pytest.raises(MethodNotAllowed):33 assert router.dispatch(Request("GET", "/health"))34 assert router.dispatch(Request("POST", "/health")).get_data(True) == "POST/OK"35 def test_dispatcher_with_pass_response(self):36 router = Router(dispatcher=resource_dispatcher(pass_response=True))37 class TestResource:38 def on_get(self, req, resp: Response):39 resp.set_json({"message": "GET/OK"})40 def on_post(self, req, resp):41 resp.set_data("POST/OK")42 router.add("/health", TestResource())43 assert router.dispatch(Request("GET", "/health")).json == {"message": "GET/OK"}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!