httmock: A Powerful Mocking Library for Python Requests

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

httmock: A Powerful Mocking Library for Python Requests

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.

Repository Information

Analyzed by OSRepos on August 3, 2026

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

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:

pip install httmock

For Gentoo users, it can be installed via:

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.

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.

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.

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?

  • 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:

Related repositories

Similar repositories that may be relevant next.

Mocket: A Comprehensive Socket Mocking Framework for Python

Mocket: A Comprehensive Socket Mocking Framework for Python

August 3, 2026

Mocket is a powerful Python framework designed for monkey-patching the `socket` and `ssl` modules, enabling robust testing of network-dependent applications. It serves as both a low-level framework for building custom clients and a ready-to-use mock for HTTP/HTTPS calls, supporting various environments including asyncio and MicroPython. This tool simplifies the process of isolating and testing Python clients that communicate over the socket protocol.

pythontestingmocking
vcrpy: Simplify and Speed Up Python HTTP Testing

vcrpy: Simplify and Speed Up Python HTTP Testing

August 3, 2026

vcrpy is a Python library that automatically mocks your HTTP interactions, making testing simpler and significantly faster. Inspired by Ruby's VCR, it records HTTP requests and responses to a 'cassette' file during the first test run, then replays them in subsequent runs, eliminating actual network traffic. This approach ensures deterministic tests, allows offline development, and boosts test execution speed.

httpmockingpython
Werkzeug: A Comprehensive WSGI Web Application Library for Python

Werkzeug: A Comprehensive WSGI Web Application Library for Python

July 22, 2026

Werkzeug is a comprehensive WSGI web application library, providing essential tools for building web applications in Python. It includes an interactive debugger, robust request and response objects, a flexible routing system, and various HTTP utilities. This powerful library serves as the foundation for popular frameworks like Flask, offering developers significant flexibility without enforcing specific dependencies.

werkzeugpythonwsgi
Piping Server: Infinite Data Transfer Over Pure HTTP

Piping Server: Infinite Data Transfer Over Pure HTTP

June 20, 2026

Piping Server is an innovative open-source project enabling infinite data transfer between any device over pure HTTP. It acts as a simple, storageless server, facilitating data streaming with just `curl` or a web browser. This makes it ideal for secure, real-time communication and large file transfers without requiring any installation.

data-transferhttpstream

Source repository

Open the original repository on GitHub.

View on GitHub
OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️