{"name":"HTTPretty: Intercepting HTTP Requests for Python Testing","description":"HTTPretty is a powerful Python library designed to intercept HTTP requests at the socket level, effectively faking the entire socket module. It provides a robust solution for mocking external HTTP services, making it ideal for test-driven development and reliable API integration testing. Developers can use HTTPretty to simulate various HTTP responses, ensuring comprehensive and isolated testing environments.","github":"https://github.com/gabrielfalcao/HTTPretty","url":"https://osrepos.com/repo/gabrielfalcao-httpretty","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/gabrielfalcao-httpretty","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/gabrielfalcao-httpretty.md","json":"https://osrepos.com/repo/gabrielfalcao-httpretty.json","topics":["Python","HTTP Mocking","Testing","TDD","API Integration","Fakeweb","Socket Interception","Development Tools"],"keywords":["Python","HTTP Mocking","Testing","TDD","API Integration","Fakeweb","Socket Interception","Development Tools"],"stars":null,"summary":"HTTPretty is a powerful Python library designed to intercept HTTP requests at the socket level, effectively faking the entire socket module. It provides a robust solution for mocking external HTTP services, making it ideal for test-driven development and reliable API integration testing. Developers can use HTTPretty to simulate various HTTP responses, ensuring comprehensive and isolated testing environments.","content":"## Introduction\nHTTPretty is a Python library created by Gabriel Falcão that provides a comprehensive solution for mocking HTTP client requests. It operates by intercepting HTTP requests at the Python socket level, effectively faking the entire socket module. This capability makes HTTPretty an invaluable tool for developers working on API integrations, enabling them to simulate external service responses without making actual network calls. Inspired by FakeWeb, HTTPretty simplifies the process of creating isolated and repeatable tests for applications that interact with web services.\n\n## Installation\nTo get started with HTTPretty, you can easily install it using pip:\n\nbash\npip install httpretty\n\n\n## Examples\n\n### Simple Example\nThis example demonstrates how to register a GET request and assert the response and request details.\n\npython\nimport sure\nimport httpretty\nimport requests\n\n\n@httpretty.activate(verbose=True, allow_net_connect=False)\ndef test_httpbin():\n    httpretty.register_uri(\n        httpretty.GET,\n        \"https://httpbin.org/ip\",\n        body='{\"origin\": \"127.0.0.1\"}'\n    )\n\n    response = requests.get('https://httpbin.org/ip')\n    response.json().should.equal({'origin': '127.0.0.1'})\n\n    httpretty.latest_requests().should.have.length_of(1)\n    httpretty.last_request().should.equal(httpretty.latest_requests()[0])\n    httpretty.last_request().body.should.equal('{\"origin\": \"127.0.0.1\"}')\n\n\n### Checking Multiple Responses\nThis example shows how to register multiple responses for the same URL and verify the request bodies.\n\npython\n@httpretty.activate(verbose=True, allow_net_connect=False)\ndef test_post_bodies():\n    url = 'http://httpbin.org/post'\n    httpretty.register_uri(httpretty.POST, url, status=200)\n    httpretty.register_uri(httpretty.POST, url, status=400)\n    requests.post(url, data={'foo': 'bar'})\n    requests.post(url, data={'zoo': 'zoo'})\n    assert 'foo=bar' in httpretty.latest_requests()[0].body\n    assert 'zoo=bar' in httpretty.latest_requests()[1].body\n\n\n## Why Use HTTPretty?\nHTTPretty is essential for robust software development, especially when dealing with external APIs. Its primary benefits include:\n*   **Test-Driven Development (TDD) of API Integrations**: Facilitates writing tests for API clients before or during implementation, ensuring correct behavior.\n*   **Faking Responses of External APIs**: Allows developers to simulate various API responses, including success, failure, and edge cases, without relying on actual external services.\n*   **Recording and Playback of HTTP Requests**: Useful for debugging, performance testing, and creating repeatable test scenarios.\n*   **Isolation and Speed**: By intercepting requests at the socket level, HTTPretty ensures tests are isolated from network latency and external service availability, leading to faster and more reliable test suites.\n\n## Links\n*   [GitHub Repository](https://github.com/gabrielfalcao/HTTPretty)\n*   [Documentation](https://httpretty.readthedocs.io/en/latest/)\n*   [PyPI Package](https://pypi.org/project/httpretty/)","metrics":{"detailViews":1,"githubClicks":0},"dates":{"published":null,"modified":"2026-08-03T15:36:27.000Z"}}