Best Python code snippet using localstack_python
proxy.py
Source:proxy.py
2from fastapi.responses import Response3from settings import ProxySettings4from utils import modify_url, modify_html5async def get_response(request: Request) -> Response:6 request_headers = prepare_request_headers(dict(request.headers))7 url = f'https://{ProxySettings.DOMAIN}{request.url.path}'8 try:9 async with request.app.state.client_session.request(10 method=request.method,11 url=url,12 params=dict(request.query_params),13 headers=request_headers,14 allow_redirects=True15 ) as habr_response:16 content = await habr_response.read()17 response_headers = prepare_response_headers(dict(habr_response.headers), str(request.base_url))18 if habr_response.content_type == 'text/html':19 text = content.decode('utf8')20 text = modify_html(text, str(request.base_url))21 content = text.encode('utf8')22 response = Response(23 content,24 status_code=habr_response.status,25 headers=response_headers,26 media_type=habr_response.content_type27 )28 return response29 except Exception as e:30 print(e)31 raise HTTPException(status_code=503) from e32def prepare_response_headers(headers: dict[str, str], proxy_address: str) -> dict[str, str]:33 headers.pop('Content-Encoding', None)34 headers.pop('Content-Length', None)35 if location := headers.get('Location'):36 headers['Location'] = modify_url(location, proxy_address)37 return headers38def prepare_request_headers(headers: dict[str, str]) -> dict[str, str]:39 headers['host'] = ProxySettings.DOMAIN40 headers.pop('referer', None)41 headers['connection'] = 'close'...
test_utils.py
Source:test_utils.py
...10 ]11)12def test_camel_case_pydantic_alias_generator(input_string: str, expected_output: str):13 assert camel_case_pydantic_alias_generator(input_string) == expected_output14def test_prepare_request_headers():15 assert prepare_request_headers(request_timeout_seconds=10) == {16 'Accept': 'application/json',17 'Content-Type': 'application/json; charset=utf-8',18 'Request-Timeout': '10.000',19 }20 assert prepare_request_headers() == {21 'Accept': 'application/json',22 'Content-Type': 'application/json; charset=utf-8',23 }24 assert prepare_request_headers(request_timeout_seconds=1.001)['Request-Timeout'] == '1.001'25 assert prepare_request_headers(request_timeout_seconds=1.0001)['Request-Timeout'] == '1.000'...
test_proxy.py
Source:test_proxy.py
...8 }, 'http://127.0.0.1:8232/')9 assert result == {10 'Location': 'http://127.0.0.1:8232/ru/all'11 }12def test_prepare_request_headers() -> None:13 result = prepare_request_headers({14 'host': '127.0.0.1:8232',15 'referer': 'http://127.0.0.1:8232/',16 'connection': 'keep-alive'17 })18 assert result == {19 'host': ProxySettings.DOMAIN,20 'connection': 'close',21 'cookie': 'web_override=false;'...
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!!