Handle requests to set cookies from query parameters and redirect.
(scope: dict[str, Any], _receive: Receive, send: Send)
| 150 | |
| 151 | |
| 152 | async def set_cookies(scope: dict[str, Any], _receive: Receive, send: Send) -> None: |
| 153 | """Handle requests to set cookies from query parameters and redirect.""" |
| 154 | |
| 155 | query_params = get_query_params(scope.get('query_string', b'')) |
| 156 | |
| 157 | headers = [ |
| 158 | [b'content-type', b'text/plain; charset=utf-8'], |
| 159 | [b'location', b'/cookies'], # Redirect header |
| 160 | ] |
| 161 | |
| 162 | for key, values in query_params.items(): |
| 163 | if values: # Only add if there's at least one value |
| 164 | cookie_value = f'{key}={values[0]}; Path=/' |
| 165 | headers.append([b'set-cookie', cookie_value.encode()]) |
| 166 | |
| 167 | await send( |
| 168 | { |
| 169 | 'type': 'http.response.start', |
| 170 | 'status': 302, # 302 Found for redirect |
| 171 | 'headers': headers, |
| 172 | } |
| 173 | ) |
| 174 | await send({'type': 'http.response.body', 'body': b'Redirecting to get_cookies...'}) |
| 175 | |
| 176 | |
| 177 | async def hello_world(_scope: dict[str, Any], _receive: Receive, send: Send) -> None: |
nothing calls this directly
no test coverage detected