urllib3-mock


Nameurllib3-mock JSON
Version 0.3.3 PyPI version JSON
download
home_pagehttps://github.com/florentx/urllib3-mock
SummaryA utility library for mocking out the `urllib3` Python library.
upload_time2015-04-16 23:10:35
maintainerNone
docs_urlNone
authorFlorent Xicluna
requires_pythonNone
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            urllib3-mock
============

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

This is an adaptation of the `responses`_ library.

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


.. _urllib3: https://urllib3.readthedocs.org/
.. _responses: https://github.com/getsentry/responses


Response body as string
-----------------------

.. code-block:: python

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

.. code-block:: python

    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.

.. code-block:: python

    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.

.. code-block:: python

    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.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/florentx/urllib3-mock",
    "name": "urllib3-mock",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Florent Xicluna",
    "author_email": "florent.xicluna@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b0/23/6a338cfb7c922e455725c3a4cd2df59f05294f0406f9670e20e115b331e2/urllib3-mock-0.3.3.tar.gz",
    "platform": "UNKNOWN",
    "description": "urllib3-mock\n============\n\nA utility library for mocking out the `urllib3`_ Python library.\n\nThis is an adaptation of the `responses`_ library.\n\n.. image:: https://travis-ci.org/florentx/urllib3-mock.png?branch=master\n\t:target: https://travis-ci.org/florentx/urllib3-mock\n\n\n.. _urllib3: https://urllib3.readthedocs.org/\n.. _responses: https://github.com/getsentry/responses\n\n\nResponse body as string\n-----------------------\n\n.. code-block:: python\n\n    from urllib3_mock import Responses\n    import requests\n\n    responses = Responses('requests.packages.urllib3')\n\n    @responses.activate\n    def test_my_api():\n        responses.add('GET', '/api/1/foobar',\n                      body='{\"error\": \"not found\"}', status=404,\n                      content_type='application/json')\n\n        resp = requests.get('http://twitter.com/api/1/foobar')\n\n        assert resp.json() == {\"error\": \"not found\"}\n\n        assert len(responses.calls) == 1\n        assert responses.calls[0].request.url == '/api/1/foobar'\n        assert responses.calls[0].request.host == 'twitter.com'\n        assert responses.calls[0].request.scheme == 'http'\n\nRequest callback\n----------------\n\n.. code-block:: python\n\n    import json\n\n    from urllib3_mock import Responses\n    import requests\n\n    responses = Responses('requests.packages.urllib3')\n\n    @responses.activate\n    def test_calc_api():\n\n        def request_callback(request):\n            payload = json.loads(request.body)\n            resp_body = {'value': sum(payload['numbers'])}\n            headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}\n            return (200, headers, json.dumps(resp_body))\n\n        responses.add_callback('POST', '/sum',\n                               callback=request_callback,\n                               content_type='application/json')\n\n        resp = requests.post(\n            'http://calc.com/sum',\n            json.dumps({'numbers': [1, 2, 3]}),\n            headers={'content-type': 'application/json'},\n        )\n\n        assert resp.json() == {'value': 6}\n\n        assert len(responses.calls) == 1\n        assert responses.calls[0].request.url == '/sum'\n        assert responses.calls[0].request.host == 'calc.com'\n        assert (\n            responses.calls[0].response.headers['request-id'] ==\n            '728d329e-0e86-11e4-a748-0c84dc037c13'\n        )\n\nInstead of passing a string URL into `responses.add` or `responses.add_callback`\nyou can also supply a compiled regular expression.\n\n.. code-block:: python\n\n    import re\n    from urllib3_mock import Responses\n    import requests\n\n    responses = Responses('requests.packages.urllib3')\n\n    # Instead of\n    responses.add('GET', '/api/1/foobar',\n                  body='{\"error\": \"not found\"}', status=404,\n                  content_type='application/json')\n\n    # You can do the following\n    url_re = re.compile(r'/api/\\d+/foobar')\n    responses.add('GET', url_re,\n                  body='{\"error\": \"not found\"}', status=404,\n                  content_type='application/json')\n\nA response can also throw an exception as follows.\n\n.. code-block:: python\n\n    from urllib3_mock import Responses\n    from requests.packages.urllib3.exceptions import HTTPError\n\n    exception = HTTPError('Something went wrong')\n\n    responses = Responses('requests.packages.urllib3')\n    responses.add('GET', '/api/1/foobar',\n                  body=exception)\n    # All calls to 'http://twitter.com/api/1/foobar' will throw exception.",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "A utility library for mocking out the `urllib3` Python library.",
    "version": "0.3.3",
    "project_urls": {
        "Download": "UNKNOWN",
        "Homepage": "https://github.com/florentx/urllib3-mock"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "64ec4c723737b2c7733b6b7257d6990aa813144e2bb96e75ceaf2791ee815277",
                "md5": "2aed73bd3b24204f1ce09c467d397402",
                "sha256": "702c90042920d771c9902b7b5b542551cc57f259078f4eada47ab4e8cdd11f1a"
            },
            "downloads": -1,
            "filename": "urllib3_mock-0.3.3-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2aed73bd3b24204f1ce09c467d397402",
            "packagetype": "bdist_wheel",
            "python_version": "3.3",
            "requires_python": null,
            "size": 6270,
            "upload_time": "2015-04-16T23:10:38",
            "upload_time_iso_8601": "2015-04-16T23:10:38.540994Z",
            "url": "https://files.pythonhosted.org/packages/64/ec/4c723737b2c7733b6b7257d6990aa813144e2bb96e75ceaf2791ee815277/urllib3_mock-0.3.3-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b0236a338cfb7c922e455725c3a4cd2df59f05294f0406f9670e20e115b331e2",
                "md5": "1eba839a0469cf029e7acd629cca21e2",
                "sha256": "b210037029ac96beac4f3e7b54f466c394b060525ea5a824803d5f5ed14558f1"
            },
            "downloads": -1,
            "filename": "urllib3-mock-0.3.3.tar.gz",
            "has_sig": false,
            "md5_digest": "1eba839a0469cf029e7acd629cca21e2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10408,
            "upload_time": "2015-04-16T23:10:35",
            "upload_time_iso_8601": "2015-04-16T23:10:35.698961Z",
            "url": "https://files.pythonhosted.org/packages/b0/23/6a338cfb7c922e455725c3a4cd2df59f05294f0406f9670e20e115b331e2/urllib3-mock-0.3.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2015-04-16 23:10:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "florentx",
    "github_project": "urllib3-mock",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "urllib3-mock"
}
        
Elapsed time: 0.08962s