Questions tagged [httpretty]

HTTPretty is a HTTP client mock library for Python 100% inspired on ruby's FakeWeb.

HTTPretty is a HTTP client mock library for 100% inspired on 's .

HTTPretty monkey patches Python's socket core module, re-implementing the HTTP protocol, by mocking requests and responses. As for it works in this way, you don't need to worry what http library you're gonna use. HTTPretty will mock the response for you :) (and also give you the latest requests so that you can check them).

This way even if the RESTful API server is down or its contains are changed, the expected reply can be mocked.

You should be using this tag if your question is related to the use of HTTPretty library or its API.

18 questions
11
votes
1 answer

Mock a HTTP request that times out with HTTPretty

Using the HTTPretty library for Python, I can create mock HTTP responses of choice and then pick them up i.e. with the requests library like so: import httpretty import requests # set up a mock httpretty.enable() httpretty.register_uri( …
Dirk
  • 9,381
  • 17
  • 70
  • 98
5
votes
1 answer

Is there an alternative to parse_qs that handles semi-colons?

TL;DR What libraries/calls are available to handle query strings containing semi-colons differently than parse_qs? >>> urlparse.parse_qs("tagged=python;ruby") >>> {'tagged': ['python']} Full Background I'm working with the StackExchange API to…
Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
3
votes
1 answer

How to get a request that is not the last in HttPretty?

Using the HTTPretty library for Python, I can create mock HTTP responses for my unit tests. When the code I am testing runs, instead of my request reaching the third party, the request is intercepted and my code receives the response I configured. I…
Alvaro Aguilar
  • 694
  • 8
  • 23
3
votes
0 answers

Silence exceptions that do not result in test failure in python unittest

I am writing unit tests for a python program using the unittest framework. One of the functions of the program is connecting to an external REST API using the requests library. If there is a connection error or timeout, I would like the function to…
lac
  • 755
  • 10
  • 19
2
votes
1 answer

How to use `httpretty` to mock out request

Here's a test I've tried writing: import httpretty import requests @httpretty.activate def test(): httpretty.register_uri( httpretty.GET, "http://localhost:8000", body='{"origin": "127.0.0.1"}', status=200 …
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
2
votes
0 answers

Httpretty causes socket to lose connection?

I'm using Httpretty to simulate a web API. If I use httpretty.enable(allow_net_connect=False) (When allow_net_connect is False any connection to an unregistered uri will throw httpretty.errors.UnmockedError), I get an error since pymysql can't…
Fernando César
  • 681
  • 1
  • 8
  • 16
2
votes
1 answer

Mocking Twisted web client HTTP requests using HTTPretty

As Httpretty works on the Python socket layer, even Twisted web requests should be mocked out. But i am seeing some weird behavior on using httpretty. It tries to connect to localhost somehow. Below example shows the difference: import…
prat0318
  • 571
  • 1
  • 8
  • 19
1
vote
1 answer

How can I get httpretty to stop printing exceptions during testing?

I have a test which simulates a request from a remote server which does not exist: @httpretty.activate def test_no_such_host(self): def no_such_host(request, uri, headers): raise requests.ConnectionError() …
Marnanel Thurman
  • 261
  • 1
  • 10
1
vote
0 answers

How to set different responses for the GET requests in Python's unit tests

I need to return different desponses depending on the GET parameters via HTTPretty: @httpretty.activate def test_multiple_requests(): httpretty.register_uri(httpretty.GET, 'https://example.com/data.xml?n=0', …
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
1
vote
1 answer

combine httpretty with pytest tmpdir

The following pytest-test uses httpretty, to mock a request. It writes the fetched data to a file: import requests import httpretty import json from os import listdir from os.path import join @httpretty.activate def…
zormit
  • 533
  • 4
  • 16
1
vote
1 answer

HTTPretty test hanging on Travis

While making python-intercom Python 3 compatible, I ran into an issue on Travis. The nosetests command doesn't appear to exit when run on Python 3.4 (it behaves as expected on Python 2.7). I narrowed it down to tests that use HTTPretty and created a…
John Keyes
  • 5,479
  • 1
  • 29
  • 48
1
vote
1 answer

Understanding my own decorator

I have written a decorator that is working correctly but i stumbled with the correct solution by trial and error and my litle knowledge about decorators tells me that something is not well defined. The case is i'm mocking a Rest Api to do some TDD,…
cllamach
  • 441
  • 3
  • 14
1
vote
1 answer

MongoClient ConnectionFailure after httpretty.enable()

Whenever I enable HTTPretty, I'm unable to make a connection with PyMongo. I know that HTTPretty alters the core socket module; is there any way around this? Code Example: import pymongo import httpretty import time …
bmsauer
  • 105
  • 9
1
vote
1 answer

How to assert a URL was not requested with HTTPretty

How does one go about asserting that a target URL was never requested when using HTTPretty to intercept HTTP requests in Python unit tests?
Nathan
  • 5,272
  • 2
  • 26
  • 28
0
votes
1 answer

Python requests.post fails in 421

I have the following code in the implementation (uses requests lib): def call_service(product_ids: Set[str]) response = requests.post( json={"product_ids": product_ids}, url="http://whatever.com", ) if response.status_code ==…
Luís Soares
  • 5,726
  • 4
  • 39
  • 66
1
2