• Stars
    star
    460
  • Rank 91,546 (Top 2 %)
  • Language
    Python
  • License
    Other
  • Created about 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A mocking library for requests

httmock

A mocking library for requests for Python 2.7 and 3.4+.

Installation

pip install httmock

Or, if you are a Gentoo user:

emerge dev-python/httmock

Usage

You can use it to mock third-party APIs and test libraries that use requests internally, conditionally using mocked replies with the urlmatch decorator:

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?'

The all_requests decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the requests.Response object returned:

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

If you pass in Set-Cookie headers, requests.Response.cookies will contain the values. You can also use response method directly instead of returning a dict:

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']

More Repositories

1

vscode-code-outline

A symbol outline for Visual Studio Code
TypeScript
249
star
2

PythonChecker

A maintainable Python code checker for Sublime Text 3
Python
31
star
3

pymongo-pubsub

a publish-subscribe pattern implementation for pymongo
Python
29
star
4

jstimeline

Animate objects using a single global timer
JavaScript
22
star
5

great-justice

Debug every ZIG
Python
13
star
6

django-channels-ariadne

An async experiment with Ariadne + Django Channels
Python
8
star
7

starlette-ariadne

An async experiment with Ariadne + Starlette
Python
8
star
8

djdt-vmprof

A VMProf panel for Django Debug Toolbar
Python
8
star
9

django-scbv

Simple (and sane) class-based views for Django
Python
6
star
10

laughing-man

A fun experiment with OpenCV
Python
6
star
11

node-mongodb-pubsub

a publish-subscribe pattern implementation for node-mongodb
JavaScript
5
star
12

daemontools-ng

A modern, compatible, LGPL alternative to daemontools
Python
4
star
13

gnome-specimen

A fork of Specimen hacked for make benefit glorious nation of jimmac
Python
4
star
14

python-number-to-words-pl

Provides a function to convert a number to its verbose "spoken" version
Python
3
star
15

empathy-tango

A Tango-based Adium-style theme for Empathy
3
star
16

qcheck

My journey into the deep abyss of implementing a QuickCheck in Python
Python
3
star
17

ui-experiments

A bit like Hamster Experiments, exploring the UI possibilities for fun and make benefit our glorious nation
Python
2
star
18

babel

A GitHub mirror of http://svn.edgewall.org/repos/babel/
Python
2
star
19

gtksourceview-improved

Improved language definitions and a bonus theme
2
star
20

django-explicit

Explicit querysets for Django
Python
1
star
21

blipy

A Python API for blip.pl
Python
1
star
22

route82

A simple URL router for Python
Python
1
star
23

saioleor

Saleor API mocked using Ariadne
Python
1
star
24

pleasework

Automatically exported from code.google.com/p/pleasework
JavaScript
1
star
25

django-specialized-views

Func-based views
Python
1
star