# httmock: A Powerful Mocking Library for Python Requests

This repository profile is provided by osrepos.com, an open source repository discovery platform.

Source: osrepos.com
Repository profile: https://osrepos.com/repo/patrys-httmock
Generated for open source discovery and AI-assisted research.

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
OSRepos URL: https://osrepos.com/repo/patrys-httmock

## 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.

## Topics

- http
- mock
- python
- requests
- testing
- development
- api

## Repository Information

Last analyzed by OSRepos: Mon Aug 03 2026 20:13:24 GMT+0100 (Western European Summer Time)
Detail views: 0
GitHub clicks: 0

## Safety Notice

OSRepos shares public repositories for knowledge and discovery only. Review source code, dependencies, licenses, and security implications before running or installing anything.

## Content

## Introduction
`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.

## Installation
Installation is straightforward using pip:

bash
pip install httmock


For Gentoo users, it can be installed via:

bash
emerge dev-python/httmock


## Examples
`httmock` offers decorators like `urlmatch` and `all_requests` to define how requests should be mocked.

### Mocking Specific URLs with `urlmatch`:
The `urlmatch` decorator allows you to conditionally mock requests based on URL patterns.

python
from httmock import urlmatch, HTTMock
import requests

@urlmatch(netloc=r'(.*\\.)?google\\.com$')
def google_mock(url, request):
    return 'Feeling lucky, punk?'

with HTTMock(google_mock):
    r = requests.get('http://google.com/')
print r.content  # 'Feeling lucky, punk?'

This example demonstrates how to intercept requests to `google.com` and return a custom string.

### Mocking All Requests with `all_requests`:
The `all_requests` decorator intercepts all outgoing requests. You can return a dictionary that `httmock` will convert into a `requests.Response` object.

python
from httmock import all_requests, HTTMock
import requests

@all_requests
def response_content(url, request):
	return {'status_code': 200,
	        'content': 'Oh hai'}

with HTTMock(response_content):
	r = requests.get('https://foo_bar')

print r.status_code
print r.content

Here, any request will receive a 200 status code and "Oh hai" as content.

### Advanced Response Customization:
For more control, you can use the `response` method directly to specify status codes, headers, and content.

python
from httmock import all_requests, response, HTTMock
import requests

@all_requests
def response_content(url, request):
	headers = {'content-type': 'application/json',
	           'Set-Cookie': 'foo=bar;'}
	content = {'message': 'API rate limit exceeded'}
	return response(403, content, headers, None, 5, request)

with HTTMock(response_content):
	r = requests.get('https://api.github.com/users/whatever')

print r.json().get('message')
print r.cookies['foo']

This example shows how to simulate a 403 Forbidden response with custom JSON content and cookies.

## Why Use `httmock`?
`httmock` is invaluable for several reasons:
*   **Reliable Testing:** Decouple your tests from external services, ensuring they run consistently and quickly without network dependencies.
*   **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.
*   **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.
*   **Reduced Costs:** Avoid hitting rate limits or incurring costs associated with excessive API calls during development and testing.

## Links
Explore `httmock` further on its official GitHub repository:
*   [GitHub Repository](https://github.com/patrys/httmock)