|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +import azure.functions as func |
| 7 | +from azure.functions._http_asgi import ( |
| 8 | + AsgiMiddleware |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class MockAsgiApplication: |
| 13 | + response_code = 200 |
| 14 | + response_body = b'' |
| 15 | + response_headers = [ |
| 16 | + [b"content-type", b"text/plain"], |
| 17 | + ] |
| 18 | + |
| 19 | + async def __call__(self, scope, receive, send): |
| 20 | + self.received_scope = scope |
| 21 | + # Verify against ASGI specification |
| 22 | + assert scope['type'] == 'http' |
| 23 | + assert isinstance(scope['type'], str) |
| 24 | + |
| 25 | + assert scope['asgi.spec_version'] in ['2.0', '2.1'] |
| 26 | + assert isinstance(scope['asgi.spec_version'], str) |
| 27 | + |
| 28 | + assert scope['asgi.version'] in ['2.0', '2.1', '2.2'] |
| 29 | + assert isinstance(scope['asgi.version'], str) |
| 30 | + |
| 31 | + assert scope['http_version'] in ['1.0', '1.1', '2'] |
| 32 | + assert isinstance(scope['http_version'], str) |
| 33 | + |
| 34 | + assert scope['method'] in ['POST', 'GET', 'PUT', 'DELETE', 'PATCH'] |
| 35 | + assert isinstance(scope['method'], str) |
| 36 | + |
| 37 | + assert scope['scheme'] in ['http', 'https'] |
| 38 | + assert isinstance(scope['scheme'], str) |
| 39 | + |
| 40 | + assert isinstance(scope['path'], str) |
| 41 | + assert isinstance(scope['raw_path'], bytes) |
| 42 | + assert isinstance(scope['query_string'], bytes) |
| 43 | + assert isinstance(scope['root_path'], str) |
| 44 | + |
| 45 | + assert hasattr(scope['headers'], '__iter__') |
| 46 | + for k, v in scope['headers']: |
| 47 | + assert isinstance(k, bytes) |
| 48 | + assert isinstance(v, bytes) |
| 49 | + |
| 50 | + assert scope['client'] is None or hasattr(scope['client'], '__iter__') |
| 51 | + if scope['client']: |
| 52 | + assert len(scope['client']) == 2 |
| 53 | + assert isinstance(scope['client'][0], str) |
| 54 | + assert isinstance(scope['client'][1], int) |
| 55 | + |
| 56 | + assert scope['server'] is None or hasattr(scope['server'], '__iter__') |
| 57 | + if scope['server']: |
| 58 | + assert len(scope['server']) == 2 |
| 59 | + assert isinstance(scope['server'][0], str) |
| 60 | + assert isinstance(scope['server'][1], int) |
| 61 | + |
| 62 | + self.received_request = await receive() |
| 63 | + assert self.received_request['type'] == 'http.request' |
| 64 | + assert isinstance(self.received_request['body'], bytes) |
| 65 | + assert isinstance(self.received_request['more_body'], bool) |
| 66 | + |
| 67 | + await send( |
| 68 | + { |
| 69 | + "type": "http.response.start", |
| 70 | + "status": self.response_code, |
| 71 | + "headers": self.response_headers, |
| 72 | + } |
| 73 | + ) |
| 74 | + await send( |
| 75 | + { |
| 76 | + "type": "http.response.body", |
| 77 | + "body": self.response_body, |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +class TestHttpAsgiMiddleware(unittest.TestCase): |
| 83 | + def _generate_func_request( |
| 84 | + self, |
| 85 | + method="POST", |
| 86 | + url="https://function.azurewebsites.net/api/http?firstname=rt", |
| 87 | + headers={ |
| 88 | + "Content-Type": "application/json", |
| 89 | + "x-ms-site-restricted-token": "xmsrt" |
| 90 | + }, |
| 91 | + params={ |
| 92 | + "firstname": "roger" |
| 93 | + }, |
| 94 | + route_params={}, |
| 95 | + body=b'{ "lastname": "tsang" }' |
| 96 | + ) -> func.HttpRequest: |
| 97 | + return func.HttpRequest( |
| 98 | + method=method, |
| 99 | + url=url, |
| 100 | + headers=headers, |
| 101 | + params=params, |
| 102 | + route_params=route_params, |
| 103 | + body=body |
| 104 | + ) |
| 105 | + |
| 106 | + def _generate_func_context( |
| 107 | + self, |
| 108 | + invocation_id='123e4567-e89b-12d3-a456-426655440000', |
| 109 | + function_name='httptrigger', |
| 110 | + function_directory='/home/roger/wwwroot/httptrigger' |
| 111 | + ) -> func.Context: |
| 112 | + class MockContext(func.Context): |
| 113 | + def __init__(self, ii, fn, fd): |
| 114 | + self._invocation_id = ii |
| 115 | + self._function_name = fn |
| 116 | + self._function_directory = fd |
| 117 | + |
| 118 | + @property |
| 119 | + def invocation_id(self): |
| 120 | + return self._invocation_id |
| 121 | + |
| 122 | + @property |
| 123 | + def function_name(self): |
| 124 | + return self._function_name |
| 125 | + |
| 126 | + @property |
| 127 | + def function_directory(self): |
| 128 | + return self._function_directory |
| 129 | + |
| 130 | + return MockContext(invocation_id, function_name, function_directory) |
| 131 | + |
| 132 | + def test_middleware_calls_app(self): |
| 133 | + app = MockAsgiApplication() |
| 134 | + test_body = b'Hello world!' |
| 135 | + app.response_body = test_body |
| 136 | + app.response_code = 200 |
| 137 | + req = func.HttpRequest(method='get', url='/test', body=b'') |
| 138 | + response = AsgiMiddleware(app).handle(req) |
| 139 | + |
| 140 | + # Verify asserted |
| 141 | + self.assertEqual(response.status_code, 200) |
| 142 | + self.assertEqual(response.get_body(), test_body) |
| 143 | + |
| 144 | + def test_middleware_calls_app_with_context(self): |
| 145 | + app = MockAsgiApplication() |
| 146 | + test_body = b'Hello world!' |
| 147 | + app.response_body = test_body |
| 148 | + app.response_code = 200 |
| 149 | + req = self._generate_func_request() |
| 150 | + ctx = self._generate_func_context() |
| 151 | + response = AsgiMiddleware(app).handle(req, ctx) |
| 152 | + |
| 153 | + # Verify asserted |
| 154 | + self.assertEqual(response.status_code, 200) |
| 155 | + self.assertEqual(response.get_body(), test_body) |
0 commit comments