Fixture manager for httpretty
Project description
Fixture manager for httpretty
Features:
Reuse responses across tests
Allows maintaining state between requests
Access past request information
This was written to solve communicating to an Elasticsearch during tests. For our usage, mock didn’t scale well and placing httpretty fixtures on our base test case was impratical. To solve this, we wrote a fixture manager, httpretty-fixtures.
Getting Started
Install the module with: pip install httpretty_fixtures
# Load in our dependencies
import json
import unittest
import httpretty_fixtures
import requests
# Set up our fixture manager
class FakeElasticsearch(httpretty_fixtures.FixtureManager):
@httpretty_fixtures.get('http://localhost:9200/my_index/my_document/my_id')
def es_index(self, request, uri, res_headers):
return (200, res_headers, json.dumps({
'_index': 'my_index',
'_type': 'my_document',
'_id': 'my_id',
'_version': 1,
'found': True,
}))
# Define our tests
class MyTestCase(unittest.TestCase):
@FakeElasticsearch.run(['es_index'])
def test_retrieve_from_es(self):
"""Verify we can retrieve an item from Elasticsearch"""
# Make our request and verify we hit Elasticsearch
res = requests.get('http://localhost:9200/my_index/my_document/my_id')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.json()['_index'], 'my_index')
# Introspect our request received on `FakeElasticsearch`
self.assertEqual(httpretty_fixtures.first_request().path, '/my_index/my_document/my_id')
self.assertEqual(httpretty_fixtures.last_request().path, '/my_index/my_document/my_id')
self.assertEqual(len(httpretty_fixtures.requests()), 1)
self.assertEqual(httpretty_fixtures.requests()[0].path, '/my_index/my_document/my_id')
Documentation
httpretty-fixtures exports FixtureManager, get, put, post, delete, head, patch, options, connect, first_request, last_request, and requests as methods/variables.
We will refer to the package as httpretty_fixtures.
FixtureManager()
Class for setting up a set of fixtures on. This should be inherited from into another class with its own set of fixtures.
class FakeElasticsearch(httpretty_fixtures.FixtureManager):
@httpretty_fixtures.get('http://localhost:9200/my_index/my_document/my_id')
def es_index(self, request, uri, res_headers):
return (200, res_headers, json.dumps({'content': 'goes here'}))
fixture_manager.run(fixtures)
Decorator to run a set of fixtures during a function
fixtures list - Names of fixture functions to run
* str - Name of fixtures function to run
class FakeElasticsearch(httpretty_fixtures.FixtureManager):
@httpretty_fixtures.get('http://localhost:9200/my_index/my_document/my_id')
def es_index(self, request, uri, res_headers):
return (200, res_headers, json.dumps({}))
class MyTestCase(unittest.TestCase):
# The `es_index` fixture will be live for all of this test case
@FakeElasticsearch.run(['es_index'])
def test_retrieve_from_es(self):
"""Verify we can retrieve an item from Elasticsearch"""
# Make our request and verify we hit Elasticsearch
res = requests.get('http://localhost:9200/my_index/my_document/my_id')
fixture_manager.start(fixtures)
Start running HTTPretty with a set of fixtures
fixtures list - Names of fixture functions to run
* str - Name of fixtures function to run
This will run HTTPretty indefinitely until .stop() is run
fixture_manager.stop()
Stop a running instance of HTTPretty. This should always be run at some point after a .start()
httpretty_fixtures.{verb}(*register_uri_args, **register_uri_kwargs)
Decorator to register a fixture function under an HTTP verb
This is a summary for all possible HTTP verbs:
@httpretty_fixtures.get()
@httpretty_fixtures.put()
@httpretty_fixtures.post()
@httpretty_fixtures.delete()
@httpretty_fixtures.head()
@httpretty_fixtures.patch()
@httpretty_fixtures.options()
@httpretty_fixtures.connect()
Each of these verbs functions passes its arguments/keyword arguments to HTTPretty's register_uri` function.
If there are any arguments you want to apply to your fixture with respect to HTTPretty, this is how to do it.
https://github.com/gabrielfalcao/HTTPretty/tree/0.8.3#usage
@httpretty_fixtures.get("http://underdog.io/")
Function signature
httpretty_fixtures leverages the dynamic callback functionality of httpretty:
https://github.com/gabrielfalcao/HTTPretty/tree/0.8.3#dynamic-responses-through-callbacks
As a result, we expect our decorator to receive a function that matches the following signature:
@httpretty_fixtures.get("http://underdog.io/")
def request_handler(self, request, uri, res_headers):
res_tuple = (status_code, res_headers, body)
return res_tuple
# Example
@httpretty_fixtures.get("http://underdog.io/")
def hello(self, request, uri, res_headers):
return (200, res_headers, 'Hello World!')
The signature is as follows:
request_handler function - Handler for our request callback
self object - Instance of class extended on top of for FixtureManager
uri object - Information about incoming request
Structure is managed by httpretty
More info can be read from the source code
res_headers object - Default response headers to provide to request
These should be modified and/or passed through in the res_tuple
res_tuple tuple - Collection of information for our response
[0] int - Status code to provide for response
For example, 200 would be a 200 HTTP status code
[1] object - Modified or provided set of headers provided as a parameter
[2] str - Response body for our request
In the example above, we replied with 'Hello World!' but this could be JSON, XML, or whatever you need
httpretty_fixtures.first_request()
Alias to access the first request received by HTTPretty.
Warning: If you are using HTTPretty in other locations, then this will register those requests as well.
httpretty_fixtures.last_request()
Alias to access the last request received by HTTPretty.
Warning: If you are using HTTPretty in other locations, then this will register those requests as well.
httpretty_fixtures.requests()
Alias to access all requests received by HTTPretty.
Warning: If you are using HTTPretty in other locations, then this will register those requests as well.
Examples
Preserving state between requests
In this example, we will count between multiple requests to indicate that state is being preserved.
# Load in our dependencies
import unittest
import httpretty_fixtures
import requests
# Set up our fixture manager
class CounterServer(httpretty_fixtures.FixtureManager):
def __init__(self):
self.count = 0
super(CounterServer, self).__init__()
@httpretty_fixtures.get('http://localhost:9000/')
def counter(self, request, uri, res_headers):
self.count += 1
return (200, res_headers, str(self.count))
# Define our tests
class MyTestCase(unittest.TestCase):
@CounterServer.run(['counter'])
def test_counter_state(self):
"""Verify we can preserve state between requests"""
# Make our first request and verify its count
res = requests.get('http://localhost:9000/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, '1')
# Make our second request and verify its count
res = requests.get('http://localhost:9000/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, '2')
@CounterServer.run(['counter'])
def test_counter_alternate_state(self):
"""Verify state is not maintained between separate `FixtureManager.run()'s`"""
res = requests.get('http://localhost:9000/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, '1')
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Test via nosetests.
License
Copyright (c) 2015 Underdog.io
Licensed under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
File details
Details for the file httpretty_fixtures-1.0.2.zip
.
File metadata
- Download URL: httpretty_fixtures-1.0.2.zip
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28365ac81f4051d94d8796272b2002a8e4cb859688c0c87fdf7ff16a8a8bcdb2 |
|
MD5 | 33e24430411771178e5d2be94a6ee89c |
|
BLAKE2b-256 | 370e0027d97d06d0e0dd4cadc2b50456abf9db6f81e4f20c5f6e902560d13bf2 |
File details
Details for the file httpretty_fixtures-1.0.2.tar.gz
.
File metadata
- Download URL: httpretty_fixtures-1.0.2.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0aba528c467be1f1a0a9cfe67c9c3e513948e8b5cb1d9179be4fda6802cb31b2 |
|
MD5 | f3ea71ab0b27c80ac87302e9bc6b3682 |
|
BLAKE2b-256 | 143645c5154d82c1ba4dd87e02f26fb65c3f50d30a5acaab957c971dffbd6d24 |