### Pytest Test File Example Source: https://httpdbg.readthedocs.io/en/latest/test This pytest test file demonstrates setting up HTTP requests within fixtures and tests using the requests library. It includes session-scoped setup/teardown fixtures and test-specific POST and GET requests. ```python import pytest import requests @pytest.fixture(scope="session", autouse=True) def get_in_a_fixture(): requests.get("https://httpbin.org/get/session/setup") yield requests.get("https://httpbin.org/get/session/teardown") @pytest.fixture() def post_in_a_fixture(): return requests.post("https://httpbin.org/post", json={"demo": "in-fixture"}) def test_post(post_in_a_fixture): assert post_in_a_fixture.status_code == 200 assert ( requests.post("https://httpbin.org/post", json={"demo": "in-test"}).status_code == 200 ) def test_get(): assert requests.get("https://www.example.com").status_code == 200 ``` -------------------------------- ### Start pyhttpdbg Console Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Launches an interactive Python console with httpdbg tracing enabled. Perform HTTP requests within this console to capture them. ```bash (venv) ~/$ pyhttpdbg ``` -------------------------------- ### Install httpdbg using pip Source: https://httpdbg.readthedocs.io/en/latest/getting_started Install the httpdbg package using pip. This command should be run in your virtual environment. ```bash (venv) ~/$ pip install httpdbg ``` -------------------------------- ### Start Python console with httpdbg Source: https://httpdbg.readthedocs.io/en/latest/getting_started Launch an interactive Python console using httpdbg. This replaces the standard 'python' command. ```bash (venv) ~/$ pyhttpdbg ``` -------------------------------- ### Configure UI with URL Parameters Source: https://httpdbg.readthedocs.io/en/latest/ui Customize the httpdbg UI by appending parameters to the URL. This example shows how to group tests by session, hide the network location, and disable pretty-printing for requests and responses. ```url http://localhost:4909/?gb=session&hn=on&rd=on ``` -------------------------------- ### FastAPI Application Example Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg This Python code defines a FastAPI application with several endpoints for testing HTTP requests and responses. It includes basic GET and POST routes, as well as custom exception handling. ```python from typing import Union from fastapi import FastAPI from fastapi import HTTPException from pydantic import BaseModel import requests app = FastAPI() class Port(BaseModel): port: int @app.get("/") def hello_world(): return "Hello, World!" @app.get("/items/{item_id}") def get_item(item_id: int, q: Union[str, None] = None): if item_id == 456: raise HTTPException( status_code=456, detail="custom exception", headers={"X-Error": "This is an HTTP 456 error"}, ) return {"item_id": item_id, "q": q} @app.post("/withclientrequest") def do_client_request(port: Port): requests.get(f"http://localhost:{port.port}/") return "ok" ``` -------------------------------- ### Install notebook-httpdbg Source: https://httpdbg.readthedocs.io/en/latest/notebook Install the notebook-httpdbg package using pip. This is the first step to enable httpdbg tracing in your Jupyter environment. ```bash pip install notebook-httpdbg ``` -------------------------------- ### Install pytest-httpdbg Source: https://httpdbg.readthedocs.io/en/latest/test Install the pytest-httpdbg plugin using pip. This package provides pytest integration for httpdbg. ```bash pip install pytest-httpdbg ``` -------------------------------- ### Perform HTTP Request in Console Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Example of making an HTTP POST request using the httpx library within the pyhttpdbg console. The request and its response will be captured. ```python import httpx httpx.post("https://httpbin.org/post", json={"hello":"demo"}) ``` -------------------------------- ### Running FastAPI with pyhttpdbg Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg This command starts the FastAPI application using pyhttpdbg, enabling HTTP request debugging. Replace `python` with `pyhttpdbg` to activate the debugging server. ```bash $ pyhttpdbg -m fastapi run tests/demo_fastapi.py .... - - .--. -.. -... --. .... - - .--. -.. -... --. .... - - .--. -.. -... --. httpdbg - HTTP(S) requests available at http://localhost:4909/ .... - - .--. -.. -... --. .... - - .--. -.. -... --. .... - - .--. -.. -... --. FastAPI Starting production server 🚀 Searching for package file structure from directories with __init__.py files Importing from /home/cle/dev/httpdbg module 📁 tests ├── 🐍 __init__.py └── 🐍 demo_fastapi.py code Importing the FastAPI app object from the module with the following code: from tests.demo_fastapi import app app Using import string: tests.demo_fastapi:app server Server started at http://0.0.0.0:8000 server Documentation at http://0.0.0.0:8000/docs Logs: INFO Started server process [14228] INFO Waiting for application startup. INFO Application startup complete. INFO Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO 127.0.0.1:36978 - "GET / HTTP/1.1" 200 INFO 127.0.0.1:36982 - "GET /items/123 HTTP/1.1" 200 INFO 127.0.0.1:43596 - "GET /items/456 HTTP/1.1" 456 INFO 127.0.0.1:36026 - "POST /withclientrequest HTTP/1.1" 200 ``` -------------------------------- ### Trace Module Execution with pyhttpdbg Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Runs a Python module using pyhttpdbg, capturing HTTP requests made during its execution. This is useful for tracing package installations or other module activities. ```bash (venv) ~/$ pyhttpdbg -m pip install hookdns ``` -------------------------------- ### Trace Script Execution with Custom Initiator Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Executes a script with pyhttpdbg, specifying 'examples.mylib' as a custom initiator. This links HTTP requests originating from this module to the module itself in the trace. ```bash (venv) ~/$ pyhttpdbg -i examples.mylib --script examples/myscript.py ``` -------------------------------- ### pyhttpdbg Command-Line Help Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Displays the available command-line options for pyhttpdbg. Use this to understand how to configure tracing for scripts, modules, or consoles. ```bash (venv) ~/$ pyhttpdbg --help ``` -------------------------------- ### Trace HTTP Requests with %%httpdbg Source: https://httpdbg.readthedocs.io/en/latest/notebook Use the %%httpdbg magic command at the beginning of a cell to trace all HTTP requests made within that cell. Ensure the extension is loaded first. ```python %%httpdbg _ = requests.get("https://www.example.com") ``` -------------------------------- ### Run Unittest with Pyhttpdbg Source: https://httpdbg.readthedocs.io/en/latest/test Execute unittest tests using pyhttpdbg. This command will trace HTTP requests made during the execution of the specified unittest file. ```bash pyhttpdbg -m unittest tests/demo_run_unittest.py ``` -------------------------------- ### Run Python code with httpdbg Source: https://httpdbg.readthedocs.io/en/latest Execute your Python code using the `pyhttpdbg` command to enable HTTP request tracing. This replaces the standard `python` command. ```bash pyhttpdbg -m pytest -v examples/ ``` -------------------------------- ### Load notebook-httpdbg Extension Source: https://httpdbg.readthedocs.io/en/latest/notebook Load the notebook-httpdbg extension in your Jupyter notebook. This command makes the %%httpdbg magic command available for use in cells. ```python %load_ext notebook_httpdbg ``` -------------------------------- ### Configure %%httpdbg Output Source: https://httpdbg.readthedocs.io/en/latest/notebook Customize the output of the %%httpdbg magic command by specifying the number of characters to display for headers and bodies. This helps manage the verbosity of the trace output. ```python %%httpdbg --header 500 --body 10000 ``` -------------------------------- ### Run Pytest with Allure and httpdbg Source: https://httpdbg.readthedocs.io/en/latest/test Run pytest tests with both the Allure reporting plugin and the httpdbg Allure integration enabled. This command generates Allure results and includes HTTP traces within the Allure report. ```bash pytest ../httpdbg-docs/examples/ --alluredir=./allure-results --httpdbg-allure ``` -------------------------------- ### Perform an HTTP request in Python console Source: https://httpdbg.readthedocs.io/en/latest/getting_started Make an HTTP POST request using the httpx library within the httpdbg-enabled Python console. The request will be traced by httpdbg. ```python import httpx httpx.post("https://httpbin.org/post", json={"hello":"demo"}) ``` -------------------------------- ### Trace Script Execution with pyhttpdbg Source: https://httpdbg.readthedocs.io/en/latest/pyhttpdbg Executes a Python script using pyhttpdbg, enabling tracing of all HTTP requests made by the script. The captured requests can be viewed in the web interface. ```bash (venv) ~/$ pyhttpdbg --script examples/myscript.py ``` -------------------------------- ### Run Pytest with httpdbg and HTML Report Source: https://httpdbg.readthedocs.io/en/latest/test Execute pytest tests with httpdbg enabled and configured to save traces in the 'report' directory. This command also generates an HTML report named 'report.html' in the same directory. ```bash pytest examples/ --httpdbg --httpdbg-dir report --html=report/report.html ``` -------------------------------- ### Pytest Conftest for HTML Report Integration Source: https://httpdbg.readthedocs.io/en/latest/test This pytest hook implementation in conftest.py integrates httpdbg traces into pytest-html reports. It appends a link to the HTTPDBG trace export for each test's report extras. ```python import os import pytest from pytest_httpdbg import httpdbg_record_filename @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): pytest_html = item.config.pluginmanager.getplugin("html") outcome = yield report = outcome.get_result() extras = getattr(report, "extras", []) if call.when == "call": if httpdbg_record_filename in item.stash: extras.append( pytest_html.extras.url( os.path.basename(item.stash[httpdbg_record_filename]), name="HTTPDBG", ) ) report.extras = extras ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.