-
Notifications
You must be signed in to change notification settings - Fork 4
Add support for capturing all requests #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b39a30
Initial plan
Copilot 1decd46
Add support for capturing all requests for parallel test runs
Copilot a20da37
Add .gitignore to exclude Python cache files
Copilot 20067e9
Add /__find_request__ endpoint to search requests by header/body values
Copilot 34ca91a
Use deque with configurable maxlen to cap memory usage
Copilot ef1f96f
Add tests for all API routes and GitHub workflow
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| name: Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main, master] | ||
| pull_request: | ||
| branches: [main, master] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.11' | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install flask pytest | ||
|
|
||
| - name: Run tests | ||
| run: | | ||
| pytest test_app.py -v |
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,4 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| .pytest_cache/ |
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,92 @@ | ||
| from collections import deque | ||
| from datetime import datetime | ||
| from flask import Flask, request, jsonify | ||
| from os import environ | ||
|
|
||
| app = Flask('HTTP Request Catcher') | ||
|
|
||
| # Maximum number of requests to store in history (configurable via environment variable) | ||
| MAX_REQUEST_HISTORY = int(environ.get('MAX_REQUEST_HISTORY', 1000)) | ||
|
|
||
| last_request = None | ||
| all_requests = deque(maxlen=MAX_REQUEST_HISTORY) | ||
|
|
||
|
|
||
| @app.route('/__last_request__', methods=['GET']) | ||
| def get_last_request(): | ||
| return jsonify(last_request), 200 | ||
|
|
||
|
|
||
| @app.route('/__all_requests__', methods=['GET']) | ||
| def get_all_requests(): | ||
| return jsonify(list(all_requests)), 200 | ||
|
|
||
|
|
||
| @app.route('/__find_request__', methods=['GET']) | ||
| def find_request(): | ||
| """ | ||
| Find requests matching header or body values. | ||
| Query parameters: | ||
| - header_<name>=<value>: Match requests with specific header value | ||
| - body=<value>: Match requests containing this value in body | ||
| - method=<value>: Match requests with specific HTTP method | ||
| - url=<value>: Match requests with URL containing this value | ||
| """ | ||
| matches = [] | ||
|
|
||
| for req in all_requests: | ||
| match = True | ||
|
|
||
| for key, value in request.args.items(): | ||
| if key.startswith('header_'): | ||
| header_name = key[7:] # Remove 'header_' prefix | ||
| req_headers = {k.lower(): v for k, v in req['headers'].items()} | ||
| if req_headers.get(header_name.lower()) != value: | ||
| match = False | ||
| break | ||
| elif key == 'body': | ||
| if value not in req.get('data', ''): | ||
| match = False | ||
| break | ||
| elif key == 'method': | ||
| if req.get('method', '').upper() != value.upper(): | ||
| match = False | ||
| break | ||
| elif key == 'url': | ||
| if value not in req.get('url', ''): | ||
| match = False | ||
| break | ||
|
|
||
| if match: | ||
| matches.append(req) | ||
|
|
||
| return jsonify(matches), 200 | ||
|
|
||
|
|
||
| @app.route('/__clear__', methods=['POST', 'DELETE']) | ||
| def clear_requests(): | ||
| global last_request | ||
| last_request = None | ||
| all_requests.clear() | ||
| return '', 204 | ||
|
|
||
|
|
||
| @app.route('/', defaults={'path': ''}, methods=['PUT', 'POST', 'GET', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS']) | ||
| @app.route('/<path:path>', methods=['PUT', 'POST', 'GET', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS']) | ||
| def catch(path): | ||
| global last_request | ||
|
|
||
| last_request = { | ||
| 'method': request.method, | ||
| 'data': request.data.decode('utf-8'), | ||
| 'headers': dict(request.headers), | ||
| 'url': request.url, | ||
| 'time': datetime.now().isoformat(), | ||
| } | ||
| all_requests.append(last_request) | ||
|
|
||
| return '', 200 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host='0.0.0.0', port=5000) | ||
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.