generated from NHSDigital/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
[GPCAPIM-260]-[Steel Thread integration testing]-[RP] #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import os | ||
| import threading | ||
| from collections.abc import Generator | ||
| from functools import partial | ||
| from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
|
|
||
| def get_mtls_cert() -> tuple[str, str] | None: | ||
| cert_path = os.getenv("MTLS_CERT") | ||
| key_path = os.getenv("MTLS_KEY") | ||
| if not cert_path or not key_path: | ||
| return None | ||
| return (cert_path, key_path) | ||
|
|
||
|
|
||
| class MtlsProxyHandler(BaseHTTPRequestHandler): | ||
| """ | ||
| A simple proxy that forwards requests to the target HTTPS URL | ||
| attaching the mTLS client certificates. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| target_base: str, | ||
| cert: tuple[str, str] | None, | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| self.target_base = target_base | ||
| self.cert = cert | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def do_proxy(self, method: str) -> None: | ||
| if not self.target_base: | ||
| self.send_error(500, "Target base URL not set") | ||
| return | ||
|
|
||
| url = f"{self.target_base}{self.path}" | ||
| content_length_header = self.headers.get("Content-Length") | ||
| content_length = int(content_length_header) if content_length_header else 0 | ||
| body = self.rfile.read(content_length) if content_length > 0 else None | ||
| headers = {k: v for k, v in self.headers.items() if k.lower() != "host"} | ||
|
|
||
| try: | ||
| response = requests.request( | ||
| method=method, | ||
| url=url, | ||
| headers=headers, | ||
| data=body, | ||
| cert=self.cert, | ||
| verify=False, | ||
| timeout=30, | ||
| ) | ||
|
|
||
| self.send_response(response.status_code) | ||
| for k, v in response.headers.items(): | ||
| self.send_header(k, v) | ||
| self.end_headers() | ||
| self.wfile.write(response.content) | ||
|
|
||
| except Exception as e: | ||
| self.send_error(500, f"Proxy Error: {str(e)}") | ||
|
|
||
| def do_GET(self) -> None: | ||
| self.do_proxy("GET") | ||
|
|
||
| def do_POST(self) -> None: | ||
| self.do_proxy("POST") | ||
|
|
||
| def do_PUT(self) -> None: | ||
| self.do_proxy("PUT") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def mtls_proxy(base_url: str) -> Generator[str, None, None]: | ||
| """ | ||
| Spins up a local HTTP server in a separate thread. | ||
| Returns the URL of this local proxy. | ||
| """ | ||
|
|
||
| cert = get_mtls_cert() | ||
| handler_factory = partial(MtlsProxyHandler, base_url, cert) | ||
| server = HTTPServer(("localhost", 0), handler_factory) | ||
| thread = threading.Thread(target=server.serve_forever) | ||
| thread.daemon = True | ||
| thread.start() | ||
|
|
||
| yield f"http://localhost:{server.server_port}" | ||
|
|
||
| server.shutdown() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.