{"name":"httmock: A Powerful Mocking Library for Python Requests","description":"httmock is an essential Python library designed for mocking HTTP requests made by the popular `requests` library. It allows developers to easily simulate API responses, making it ideal for testing applications that interact with external services. With httmock, you can control network interactions, ensuring reliable and repeatable tests without relying on actual network calls.","github":"https://github.com/patrys/httmock","url":"https://osrepos.com/repo/patrys-httmock","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/patrys-httmock","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/patrys-httmock.md","json":"https://osrepos.com/repo/patrys-httmock.json","topics":["http","mock","python","requests","testing","development","api"],"keywords":["http","mock","python","requests","testing","development","api"],"stars":null,"summary":"httmock is an essential Python library designed for mocking HTTP requests made by the popular `requests` library. It allows developers to easily simulate API responses, making it ideal for testing applications that interact with external services. With httmock, you can control network interactions, ensuring reliable and repeatable tests without relying on actual network calls.","content":"## Introduction\n`httmock` is an essential Python library for developers working with the `requests` package. It provides a flexible and powerful way to mock HTTP requests, enabling robust testing of applications that communicate with external APIs or web services. By intercepting and responding to `requests` calls, `httmock` allows you to simulate various scenarios, from successful API responses to network errors, without making actual network connections.\n\n## Installation\nInstallation is straightforward using pip:\n\nbash\npip install httmock\n\n\nFor Gentoo users, it can be installed via:\n\nbash\nemerge dev-python/httmock\n\n\n## Examples\n`httmock` offers decorators like `urlmatch` and `all_requests` to define how requests should be mocked.\n\n### Mocking Specific URLs with `urlmatch`:\nThe `urlmatch` decorator allows you to conditionally mock requests based on URL patterns.\n\npython\nfrom httmock import urlmatch, HTTMock\nimport requests\n\n@urlmatch(netloc=r'(.*\\\\.)?google\\\\.com$')\ndef google_mock(url, request):\n    return 'Feeling lucky, punk?'\n\nwith HTTMock(google_mock):\n    r = requests.get('http://google.com/')\nprint r.content  # 'Feeling lucky, punk?'\n\nThis example demonstrates how to intercept requests to `google.com` and return a custom string.\n\n### Mocking All Requests with `all_requests`:\nThe `all_requests` decorator intercepts all outgoing requests. You can return a dictionary that `httmock` will convert into a `requests.Response` object.\n\npython\nfrom httmock import all_requests, HTTMock\nimport requests\n\n@all_requests\ndef response_content(url, request):\n\treturn {'status_code': 200,\n\t        'content': 'Oh hai'}\n\nwith HTTMock(response_content):\n\tr = requests.get('https://foo_bar')\n\nprint r.status_code\nprint r.content\n\nHere, any request will receive a 200 status code and \"Oh hai\" as content.\n\n### Advanced Response Customization:\nFor more control, you can use the `response` method directly to specify status codes, headers, and content.\n\npython\nfrom httmock import all_requests, response, HTTMock\nimport requests\n\n@all_requests\ndef response_content(url, request):\n\theaders = {'content-type': 'application/json',\n\t           'Set-Cookie': 'foo=bar;'}\n\tcontent = {'message': 'API rate limit exceeded'}\n\treturn response(403, content, headers, None, 5, request)\n\nwith HTTMock(response_content):\n\tr = requests.get('https://api.github.com/users/whatever')\n\nprint r.json().get('message')\nprint r.cookies['foo']\n\nThis example shows how to simulate a 403 Forbidden response with custom JSON content and cookies.\n\n## Why Use `httmock`?\n`httmock` is invaluable for several reasons:\n*   **Reliable Testing:** Decouple your tests from external services, ensuring they run consistently and quickly without network dependencies.\n*   **API Simulation:** Easily simulate complex API behaviors, including different status codes, headers, and response bodies, which is crucial for testing error handling and edge cases.\n*   **Offline Development:** Continue developing and testing parts of your application that rely on external APIs even when you don't have an internet connection or access to the live API.\n*   **Reduced Costs:** Avoid hitting rate limits or incurring costs associated with excessive API calls during development and testing.\n\n## Links\nExplore `httmock` further on its official GitHub repository:\n*   [GitHub Repository](https://github.com/patrys/httmock)","metrics":{"detailViews":0,"githubClicks":0},"dates":{"published":null,"modified":"2026-08-03T19:13:24.000Z"}}