Skip to main content

A utility library for mocking out the `urllib3` Python library.

Project description

urllib3-mock

A utility library for mocking out the urllib3 Python library.

This is an adaptation of the responses library.

https://travis-ci.org/florentx/urllib3-mock.png?branch=master

Response body as string

from urllib3_mock import Responses
import requests

responses = Responses('requests.packages.urllib3')

@responses.activate
def test_my_api():
    responses.add('GET', '/api/1/foobar',
                  body='{"error": "not found"}', status=404,
                  content_type='application/json')

    resp = requests.get('http://twitter.com/api/1/foobar')

    assert resp.json() == {"error": "not found"}

    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == '/api/1/foobar'
    assert responses.calls[0].request.host == 'twitter.com'
    assert responses.calls[0].request.scheme == 'http'

Request callback

import json

from urllib3_mock import Responses
import requests

responses = Responses('requests.packages.urllib3')

@responses.activate
def test_calc_api():

    def request_callback(request):
        payload = json.loads(request.body)
        resp_body = {'value': sum(payload['numbers'])}
        headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}
        return (200, headers, json.dumps(resp_body))

    responses.add_callback('POST', '/sum',
                           callback=request_callback,
                           content_type='application/json')

    resp = requests.post(
        'http://calc.com/sum',
        json.dumps({'numbers': [1, 2, 3]}),
        headers={'content-type': 'application/json'},
    )

    assert resp.json() == {'value': 6}

    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == '/sum'
    assert responses.calls[0].request.host == 'calc.com'
    assert (
        responses.calls[0].response.headers['request-id'] ==
        '728d329e-0e86-11e4-a748-0c84dc037c13'
    )

Instead of passing a string URL into responses.add or responses.add_callback you can also supply a compiled regular expression.

import re
from urllib3_mock import Responses
import requests

responses = Responses('requests.packages.urllib3')

# Instead of
responses.add('GET', '/api/1/foobar',
              body='{"error": "not found"}', status=404,
              content_type='application/json')

# You can do the following
url_re = re.compile(r'/api/\d+/foobar')
responses.add('GET', url_re,
              body='{"error": "not found"}', status=404,
              content_type='application/json')

A response can also throw an exception as follows.

from urllib3_mock import Responses
from requests.packages.urllib3.exceptions import HTTPError

exception = HTTPError('Something went wrong')

responses = Responses('requests.packages.urllib3')
responses.add('GET', '/api/1/foobar',
              body=exception)
# All calls to 'http://twitter.com/api/1/foobar' will throw exception.

License

Copyright 2014 Florent Xicluna
Copyright 2013 Dropbox, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the 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 Distribution

urllib3-mock-0.3.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

urllib3_mock-0.3.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file urllib3-mock-0.3.1.tar.gz.

File metadata

  • Download URL: urllib3-mock-0.3.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for urllib3-mock-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a091f48c9dade489b47a7c25ee8343388882c25377233384e4edf6fb74a514ff
MD5 775073a7527e46c7e4e99846eea68a94
BLAKE2b-256 c580f33e2e2c69e7be57bf1a506c1844c37659f44dd04409b3895d3049d6e20c

See more details on using hashes here.

File details

Details for the file urllib3_mock-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for urllib3_mock-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40f3f0b869b0bc2cdfe71590413dd93f894d4e8b98acf7b8d89f5f9080ffe312
MD5 3a9fcc9c27c8590de42f8592c0410158
BLAKE2b-256 fbf5a7a2044faa67f91389a1a09675664ea465c6613d8faa5d2543b27644eb9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page