.. image:: https://img.shields.io/pypi/v/pytest-localserver.svg?style=flat
:alt: PyPI Version
:target: https://pypi.python.org/pypi/pytest-localserver
.. image:: https://img.shields.io/pypi/pyversions/pytest-localserver.svg
:alt: Supported Python versions
:target: https://pypi.python.org/pypi/pytest-localserver
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
.. image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-localserver/master.svg
:target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-localserver/master
:alt: pre-commit.ci status
==================
pytest-localserver
==================
pytest-localserver is a plugin for the `pytest`_ testing framework which enables
you to test server connections locally.
Sometimes `monkeypatching`_ ``urllib2.urlopen()`` just does not cut it, for
instance if you work with ``urllib2.Request``, define your own openers/handlers
or work with ``httplib``. In these cases it may come in handy to have an HTTP
server running locally which behaves just like the real thing [1]_. Well, look
no further!
Quickstart
==========
Let's say you have a function to scrape HTML which only required to be pointed
at a URL ::
import requests
def scrape(url):
html = requests.get(url).text
# some parsing happens here
# ...
return result
You want to test this function in its entirety without having to rely on a
remote server whose content you cannot control, neither do you want to waste
time setting up a complex mechanism to mock or patch the underlying Python
modules dealing with the actual HTTP request (of which there are more than one
BTW). So what do you do?
You simply use pytest's `funcargs feature`_ and simulate an entire server
locally! ::
def test_retrieve_some_content(httpserver):
httpserver.serve_content(open('cached-content.html').read())
assert scrape(httpserver.url) == 'Found it!'
What happened here is that for the duration of your tests an HTTP server is
started on a random port on localhost which will serve the content you tell it
to and behaves just like the real thing.
The added bonus is that you can test whether your code behaves gracefully if
there is a network problem::
def test_content_retrieval_fails_graciously(httpserver):
httpserver.serve_content('File not found!', 404)
pytest.raises(ContentNotFoundException, scrape, httpserver.url)
The same thing works for SMTP servers, too::
def test_sending_some_message(smtpserver):
mailer = MyMailer(host=smtpserver.addr[0], port=smtpserver.addr[1])
mailer.send(to='bob@example.com', from_='alice@example.com',
subject='MyMailer v1.0', body='Check out my mailer!')
assert len(smtpserver.outbox)==1
Here an SMTP server is started which accepts e-mails being sent to it. The
nice feature here is that you can actually check if the message was received
and what was sent by looking into the smtpserver's ``outbox``.
It is really that easy!
Available funcargs
==================
Here is a short overview of the available funcargs. For more details I suggest
poking around in the code itself.
``httpserver``
provides a threaded HTTP server instance running on localhost. It has the
following attributes:
* ``code`` - HTTP response code (int)
* ``content`` - content of next response (str, bytes, or iterable of either)
* ``headers`` - response headers (dict)
* ``chunked`` - whether to chunk-encode the response (enumeration)
* ``store_request_data`` - whether to store request data for later use
Once these attributes are set, all subsequent requests will be answered with
these values until they are changed or the server is stopped. A more
convenient way to change these is ::
httpserver.serve_content(content=None, code=200, headers=None, chunked=pytest_localserver.http.Chunked.NO, store_request_data=True)
The ``chunked`` attribute or parameter can be set to
* ``Chunked.YES``, telling the server to always apply chunk encoding
* ``Chunked.NO``, telling the server to never apply chunk encoding
* ``Chunked.AUTO``, telling the server to apply chunk encoding only if
the ``Transfer-Encoding`` header includes ``chunked``
If chunk encoding is applied, each str or bytes in ``content`` becomes one
chunk in the response.
You can use ``store_request_data=False`` to disable loading the request data into
memory. This will make it impossible to check the request data using
``httpserver.requests[index].data`` but may make sense when posting a larger amount of
data and you don't need to check this.
The server address can be found in property
* ``url``
which is the string representation of tuple ``server_address`` (host as str,
port as int).
If you want to check which form fields have been POSTed, Try ::
httpserver.serve_content(..., show_post_vars=True)
which will display them as parsable text.
If you need to inspect the requests sent to the server, a list of all
received requests can be found in property
* ``requests``
which is a list of ``werkzeug.wrappers.Request`` objects.
``httpsserver``
is the same as ``httpserver`` only with SSL encryption.
``smtpserver``
provides a threaded SMTP server, with an API similar to ``smtpd.SMTPServer``,
(the deprecated class from the Python standard library) running on localhost.
It has the following attributes:
* ``addr`` - server address as tuple (host as str, port as int)
* ``outbox`` - list of ``email.message.Message`` instances received.
Using your a WSGI application as test server
============================================
As of version 0.3 you can now use a `WSGI application`_ to run on the test
server ::
from pytest_localserver.http import WSGIServer
def simple_app(environ, start_response):
"""Simplest possible WSGI application"""
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return ['Hello world!\n']
@pytest.fixture
def testserver(request):
"""Defines the testserver funcarg"""
server = WSGIServer(application=simple_app)
server.start()
request.addfinalizer(server.stop)
return server
def test_retrieve_some_content(testserver):
assert scrape(testserver.url) == 'Hello world!\n'
Have a look at the following page for more information on WSGI:
http://wsgi.readthedocs.org/en/latest/learn.html
Download and Installation
=========================
You can install the plugin by running ::
pip install pytest-localserver
Alternatively, get the latest stable version from `PyPI`_ or the latest
`bleeding-edge`_ from Github.
License and Credits
===================
This plugin is released under the MIT license. You can find the full text of
the license in the LICENSE file.
Copyright (C) 2010-2022 Sebastian Rahlf and others (see AUTHORS).
Some parts of this package is based on ideas or code from other people:
- I borrowed some implementation ideas for the httpserver from `linkchecker`_.
- The implementation for the SMTP server is based on the `Mailsink recipe`_ by
Adam Feuer, Matt Branthwaite and Troy Frever.
- The HTTPS implementation is based on work by `Sebastien Martini`_.
Thanks guys!
Development and future plans
============================
Feel free to clone the repository and add your own changes. Pull requests are
always welcome!::
git clone https://github.com/pytest-dev/pytest-localserver
If you find any bugs, please file a `report`_.
Test can be run with tox.
I already have a couple of ideas for future versions:
* support for FTP, SSH (maybe base all on twisted?)
* making the SMTP outbox as convenient to use as ``django.core.mail.outbox``
* add your own here!
Preparing a release
-------------------
For package maintainers, here is how we release a new version:
#. Ensure that the ``CHANGES`` file is up to date with the latest changes.
#. Make sure that all tests pass on the version you want to release.
#. Use the `new release form on Github`_ (or some other equivalent method) to
create a new release, following the pattern of previous releases.
* Each release has to be based on a tag. You can either create the tag first
(e.g. using ``git tag``) and then make a release from that tag, or you can
have Github create the tag as part of the process of making a release;
either way works.
* The tag name **must** be the `PEP 440`_-compliant version number prefixed
by ``v``, making sure to include at least three version number components
(e.g. ``v0.6.0``).
* The "Auto-generate release notes" button will be useful in summarizing
the changes since the last release.
#. Using either the `release workflows page`_ or the link in the email you
received about a "Deployment review", go to the workflow run created for
the new release and click "Review deployments", then either approve or reject
the two deployments, one to Test PyPI and one to real PyPI. (It should not be
necessary to reject a deployment unless something really weird happens.)
Once the deployment is approved, Github will automatically upload the files.
----
.. [1] The idea for this project was born when I needed to check that `a piece
of software`_ behaved itself when receiving HTTP error codes 404 and 500.
Having unsuccessfully tried to mock a server, I stumbled across
`linkchecker`_ which uses a the same idea to test its internals.
.. _monkeypatching: http://pytest.org/latest/monkeypatch.html
.. _pytest: http://pytest.org/
.. _funcargs feature: http://pytest.org/latest/funcargs.html
.. _linkchecker: http://linkchecker.sourceforge.net/
.. _WSGI application: http://www.python.org/dev/peps/pep-0333/
.. _PyPI: http://pypi.python.org/pypi/pytest-localserver/
.. _bleeding-edge: https://github.com/pytest-dev/pytest-localserver
.. _report: https://github.com/pytest-dev/pytest-localserver/issues/
.. _tox: http://testrun.org/tox/
.. _a piece of software: http://pypi.python.org/pypi/python-amazon-product-api/
.. _Mailsink recipe: http://code.activestate.com/recipes/440690/
.. _Sebastien Martini: http://code.activestate.com/recipes/442473/
.. _PEP 440: https://peps.python.org/pep-0440/
.. _build: https://pypa-build.readthedocs.io/en/latest/
.. _twine: https://twine.readthedocs.io/en/stable/
.. _new release form on Github: https://github.com/pytest-dev/pytest-localserver/releases/new
.. _release workflows page: https://github.com/pytest-dev/pytest-localserver/actions/workflows/release.yml
Raw data
{
"_id": null,
"home_page": "https://github.com/pytest-dev/pytest-localserver",
"name": "pytest-localserver",
"maintainer": "David Zaslavsky",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "diazona@ellipsix.net",
"keywords": "pytest server localhost http smtp",
"author": "Sebastian Rahlf",
"author_email": "basti@redtoad.de",
"download_url": "https://files.pythonhosted.org/packages/ec/33/e77476a3dee691845a052d6a3b75c715c803566fdf744b43080f92c1107a/pytest_localserver-0.9.0.post0.tar.gz",
"platform": null,
"description": ".. image:: https://img.shields.io/pypi/v/pytest-localserver.svg?style=flat\n :alt: PyPI Version\n :target: https://pypi.python.org/pypi/pytest-localserver\n\n.. image:: https://img.shields.io/pypi/pyversions/pytest-localserver.svg\n :alt: Supported Python versions\n :target: https://pypi.python.org/pypi/pytest-localserver\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n :target: https://github.com/psf/black\n\n.. image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-localserver/master.svg\n :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-localserver/master\n :alt: pre-commit.ci status\n\n==================\npytest-localserver\n==================\n\npytest-localserver is a plugin for the `pytest`_ testing framework which enables\nyou to test server connections locally.\n\nSometimes `monkeypatching`_ ``urllib2.urlopen()`` just does not cut it, for\ninstance if you work with ``urllib2.Request``, define your own openers/handlers\nor work with ``httplib``. In these cases it may come in handy to have an HTTP\nserver running locally which behaves just like the real thing [1]_. Well, look\nno further!\n\nQuickstart\n==========\n\nLet's say you have a function to scrape HTML which only required to be pointed\nat a URL ::\n\n import requests\n def scrape(url):\n html = requests.get(url).text\n # some parsing happens here\n # ...\n return result\n\nYou want to test this function in its entirety without having to rely on a\nremote server whose content you cannot control, neither do you want to waste\ntime setting up a complex mechanism to mock or patch the underlying Python\nmodules dealing with the actual HTTP request (of which there are more than one\nBTW). So what do you do?\n\nYou simply use pytest's `funcargs feature`_ and simulate an entire server\nlocally! ::\n\n def test_retrieve_some_content(httpserver):\n httpserver.serve_content(open('cached-content.html').read())\n assert scrape(httpserver.url) == 'Found it!'\n\nWhat happened here is that for the duration of your tests an HTTP server is\nstarted on a random port on localhost which will serve the content you tell it\nto and behaves just like the real thing.\n\nThe added bonus is that you can test whether your code behaves gracefully if\nthere is a network problem::\n\n def test_content_retrieval_fails_graciously(httpserver):\n httpserver.serve_content('File not found!', 404)\n pytest.raises(ContentNotFoundException, scrape, httpserver.url)\n\nThe same thing works for SMTP servers, too::\n\n def test_sending_some_message(smtpserver):\n mailer = MyMailer(host=smtpserver.addr[0], port=smtpserver.addr[1])\n mailer.send(to='bob@example.com', from_='alice@example.com',\n subject='MyMailer v1.0', body='Check out my mailer!')\n assert len(smtpserver.outbox)==1\n\nHere an SMTP server is started which accepts e-mails being sent to it. The\nnice feature here is that you can actually check if the message was received\nand what was sent by looking into the smtpserver's ``outbox``.\n\nIt is really that easy!\n\nAvailable funcargs\n==================\n\nHere is a short overview of the available funcargs. For more details I suggest\npoking around in the code itself.\n\n``httpserver``\n provides a threaded HTTP server instance running on localhost. It has the\n following attributes:\n\n * ``code`` - HTTP response code (int)\n * ``content`` - content of next response (str, bytes, or iterable of either)\n * ``headers`` - response headers (dict)\n * ``chunked`` - whether to chunk-encode the response (enumeration)\n * ``store_request_data`` - whether to store request data for later use\n\n Once these attributes are set, all subsequent requests will be answered with\n these values until they are changed or the server is stopped. A more\n convenient way to change these is ::\n\n httpserver.serve_content(content=None, code=200, headers=None, chunked=pytest_localserver.http.Chunked.NO, store_request_data=True)\n\n The ``chunked`` attribute or parameter can be set to\n\n * ``Chunked.YES``, telling the server to always apply chunk encoding\n * ``Chunked.NO``, telling the server to never apply chunk encoding\n * ``Chunked.AUTO``, telling the server to apply chunk encoding only if\n the ``Transfer-Encoding`` header includes ``chunked``\n\n If chunk encoding is applied, each str or bytes in ``content`` becomes one\n chunk in the response.\n\n You can use ``store_request_data=False`` to disable loading the request data into\n memory. This will make it impossible to check the request data using\n ``httpserver.requests[index].data`` but may make sense when posting a larger amount of\n data and you don't need to check this.\n\n The server address can be found in property\n\n * ``url``\n\n which is the string representation of tuple ``server_address`` (host as str,\n port as int).\n\n If you want to check which form fields have been POSTed, Try ::\n\n httpserver.serve_content(..., show_post_vars=True)\n\n which will display them as parsable text.\n\n If you need to inspect the requests sent to the server, a list of all\n received requests can be found in property\n\n * ``requests``\n\n which is a list of ``werkzeug.wrappers.Request`` objects.\n\n``httpsserver``\n is the same as ``httpserver`` only with SSL encryption.\n\n``smtpserver``\n provides a threaded SMTP server, with an API similar to ``smtpd.SMTPServer``,\n (the deprecated class from the Python standard library) running on localhost.\n It has the following attributes:\n\n * ``addr`` - server address as tuple (host as str, port as int)\n * ``outbox`` - list of ``email.message.Message`` instances received.\n\nUsing your a WSGI application as test server\n============================================\n\nAs of version 0.3 you can now use a `WSGI application`_ to run on the test\nserver ::\n\n from pytest_localserver.http import WSGIServer\n\n def simple_app(environ, start_response):\n \"\"\"Simplest possible WSGI application\"\"\"\n status = '200 OK'\n response_headers = [('Content-type', 'text/plain')]\n start_response(status, response_headers)\n return ['Hello world!\\n']\n\n @pytest.fixture\n def testserver(request):\n \"\"\"Defines the testserver funcarg\"\"\"\n server = WSGIServer(application=simple_app)\n server.start()\n request.addfinalizer(server.stop)\n return server\n\n def test_retrieve_some_content(testserver):\n assert scrape(testserver.url) == 'Hello world!\\n'\n\nHave a look at the following page for more information on WSGI:\nhttp://wsgi.readthedocs.org/en/latest/learn.html\n\nDownload and Installation\n=========================\n\nYou can install the plugin by running ::\n\n pip install pytest-localserver\n\nAlternatively, get the latest stable version from `PyPI`_ or the latest\n`bleeding-edge`_ from Github.\n\nLicense and Credits\n===================\n\nThis plugin is released under the MIT license. You can find the full text of\nthe license in the LICENSE file.\n\nCopyright (C) 2010-2022 Sebastian Rahlf and others (see AUTHORS).\n\nSome parts of this package is based on ideas or code from other people:\n\n- I borrowed some implementation ideas for the httpserver from `linkchecker`_.\n- The implementation for the SMTP server is based on the `Mailsink recipe`_ by\n Adam Feuer, Matt Branthwaite and Troy Frever.\n- The HTTPS implementation is based on work by `Sebastien Martini`_.\n\nThanks guys!\n\nDevelopment and future plans\n============================\n\nFeel free to clone the repository and add your own changes. Pull requests are\nalways welcome!::\n\n git clone https://github.com/pytest-dev/pytest-localserver\n\nIf you find any bugs, please file a `report`_.\n\nTest can be run with tox.\n\nI already have a couple of ideas for future versions:\n\n* support for FTP, SSH (maybe base all on twisted?)\n* making the SMTP outbox as convenient to use as ``django.core.mail.outbox``\n* add your own here!\n\nPreparing a release\n-------------------\n\nFor package maintainers, here is how we release a new version:\n\n#. Ensure that the ``CHANGES`` file is up to date with the latest changes.\n#. Make sure that all tests pass on the version you want to release.\n#. Use the `new release form on Github`_ (or some other equivalent method) to\n create a new release, following the pattern of previous releases.\n\n * Each release has to be based on a tag. You can either create the tag first\n (e.g. using ``git tag``) and then make a release from that tag, or you can\n have Github create the tag as part of the process of making a release;\n either way works.\n * The tag name **must** be the `PEP 440`_-compliant version number prefixed\n by ``v``, making sure to include at least three version number components\n (e.g. ``v0.6.0``).\n * The \"Auto-generate release notes\" button will be useful in summarizing\n the changes since the last release.\n\n#. Using either the `release workflows page`_ or the link in the email you\n received about a \"Deployment review\", go to the workflow run created for\n the new release and click \"Review deployments\", then either approve or reject\n the two deployments, one to Test PyPI and one to real PyPI. (It should not be\n necessary to reject a deployment unless something really weird happens.)\n Once the deployment is approved, Github will automatically upload the files.\n\n----\n\n.. [1] The idea for this project was born when I needed to check that `a piece\n of software`_ behaved itself when receiving HTTP error codes 404 and 500.\n Having unsuccessfully tried to mock a server, I stumbled across\n `linkchecker`_ which uses a the same idea to test its internals.\n\n.. _monkeypatching: http://pytest.org/latest/monkeypatch.html\n.. _pytest: http://pytest.org/\n.. _funcargs feature: http://pytest.org/latest/funcargs.html\n.. _linkchecker: http://linkchecker.sourceforge.net/\n.. _WSGI application: http://www.python.org/dev/peps/pep-0333/\n.. _PyPI: http://pypi.python.org/pypi/pytest-localserver/\n.. _bleeding-edge: https://github.com/pytest-dev/pytest-localserver\n.. _report: https://github.com/pytest-dev/pytest-localserver/issues/\n.. _tox: http://testrun.org/tox/\n.. _a piece of software: http://pypi.python.org/pypi/python-amazon-product-api/\n.. _Mailsink recipe: http://code.activestate.com/recipes/440690/\n.. _Sebastien Martini: http://code.activestate.com/recipes/442473/\n.. _PEP 440: https://peps.python.org/pep-0440/\n.. _build: https://pypa-build.readthedocs.io/en/latest/\n.. _twine: https://twine.readthedocs.io/en/stable/\n.. _new release form on Github: https://github.com/pytest-dev/pytest-localserver/releases/new\n.. _release workflows page: https://github.com/pytest-dev/pytest-localserver/actions/workflows/release.yml\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "pytest plugin to test server connections locally.",
"version": "0.9.0.post0",
"project_urls": {
"Homepage": "https://github.com/pytest-dev/pytest-localserver"
},
"split_keywords": [
"pytest",
"server",
"localhost",
"http",
"smtp"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "475adba3573fb43fc5133a9d06a266b178a5210eb6ff7b4cf61dc261d663c728",
"md5": "8d5051f2479f3d78ca7d0913e91b091f",
"sha256": "88a50e99637c84ab20680fd54b8dcfd53a36cbdf877013abd0e7442536a95bce"
},
"downloads": -1,
"filename": "pytest_localserver-0.9.0.post0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8d5051f2479f3d78ca7d0913e91b091f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 20787,
"upload_time": "2024-10-06T04:00:05",
"upload_time_iso_8601": "2024-10-06T04:00:05.423348Z",
"url": "https://files.pythonhosted.org/packages/47/5a/dba3573fb43fc5133a9d06a266b178a5210eb6ff7b4cf61dc261d663c728/pytest_localserver-0.9.0.post0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ec33e77476a3dee691845a052d6a3b75c715c803566fdf744b43080f92c1107a",
"md5": "6af517995a7739d83786de26e142f7fe",
"sha256": "8033a36fb382d2bc4850f9acfe2c3fb5654cd5f0d163af6daf47f290db7d5ff0"
},
"downloads": -1,
"filename": "pytest_localserver-0.9.0.post0.tar.gz",
"has_sig": false,
"md5_digest": "6af517995a7739d83786de26e142f7fe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 30280,
"upload_time": "2024-10-06T04:00:08",
"upload_time_iso_8601": "2024-10-06T04:00:08.082537Z",
"url": "https://files.pythonhosted.org/packages/ec/33/e77476a3dee691845a052d6a3b75c715c803566fdf744b43080f92c1107a/pytest_localserver-0.9.0.post0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-06 04:00:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pytest-dev",
"github_project": "pytest-localserver",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pytest-localserver"
}