METADATA 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. Metadata-Version: 2.1
  2. Name: pytest-localserver
  3. Version: 0.9.0.post0
  4. Summary: pytest plugin to test server connections locally.
  5. Home-page: https://github.com/pytest-dev/pytest-localserver
  6. Author: Sebastian Rahlf
  7. Author-email: basti@redtoad.de
  8. Maintainer: David Zaslavsky
  9. Maintainer-email: diazona@ellipsix.net
  10. License: MIT License
  11. Keywords: pytest server localhost http smtp
  12. Classifier: Framework :: Pytest
  13. Classifier: Operating System :: OS Independent
  14. Classifier: Development Status :: 4 - Beta
  15. Classifier: Intended Audience :: Developers
  16. Classifier: License :: OSI Approved :: MIT License
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Programming Language :: Python :: 3 :: Only
  19. Classifier: Programming Language :: Python :: 3.6
  20. Classifier: Programming Language :: Python :: 3.7
  21. Classifier: Programming Language :: Python :: 3.8
  22. Classifier: Programming Language :: Python :: 3.9
  23. Classifier: Programming Language :: Python :: 3.10
  24. Classifier: Programming Language :: Python :: 3.11
  25. Classifier: Programming Language :: Python :: 3.12
  26. Classifier: Programming Language :: Python :: 3.13
  27. Classifier: Topic :: Software Development :: Testing
  28. Requires-Python: >=3.6
  29. License-File: LICENSE
  30. License-File: AUTHORS
  31. Requires-Dist: werkzeug >=0.10
  32. Provides-Extra: smtp
  33. Requires-Dist: aiosmtpd ; extra == 'smtp'
  34. .. image:: https://img.shields.io/pypi/v/pytest-localserver.svg?style=flat
  35. :alt: PyPI Version
  36. :target: https://pypi.python.org/pypi/pytest-localserver
  37. .. image:: https://img.shields.io/pypi/pyversions/pytest-localserver.svg
  38. :alt: Supported Python versions
  39. :target: https://pypi.python.org/pypi/pytest-localserver
  40. .. image:: https://img.shields.io/badge/code%20style-black-000000.svg
  41. :target: https://github.com/psf/black
  42. .. image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-localserver/master.svg
  43. :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-localserver/master
  44. :alt: pre-commit.ci status
  45. ==================
  46. pytest-localserver
  47. ==================
  48. pytest-localserver is a plugin for the `pytest`_ testing framework which enables
  49. you to test server connections locally.
  50. Sometimes `monkeypatching`_ ``urllib2.urlopen()`` just does not cut it, for
  51. instance if you work with ``urllib2.Request``, define your own openers/handlers
  52. or work with ``httplib``. In these cases it may come in handy to have an HTTP
  53. server running locally which behaves just like the real thing [1]_. Well, look
  54. no further!
  55. Quickstart
  56. ==========
  57. Let's say you have a function to scrape HTML which only required to be pointed
  58. at a URL ::
  59. import requests
  60. def scrape(url):
  61. html = requests.get(url).text
  62. # some parsing happens here
  63. # ...
  64. return result
  65. You want to test this function in its entirety without having to rely on a
  66. remote server whose content you cannot control, neither do you want to waste
  67. time setting up a complex mechanism to mock or patch the underlying Python
  68. modules dealing with the actual HTTP request (of which there are more than one
  69. BTW). So what do you do?
  70. You simply use pytest's `funcargs feature`_ and simulate an entire server
  71. locally! ::
  72. def test_retrieve_some_content(httpserver):
  73. httpserver.serve_content(open('cached-content.html').read())
  74. assert scrape(httpserver.url) == 'Found it!'
  75. What happened here is that for the duration of your tests an HTTP server is
  76. started on a random port on localhost which will serve the content you tell it
  77. to and behaves just like the real thing.
  78. The added bonus is that you can test whether your code behaves gracefully if
  79. there is a network problem::
  80. def test_content_retrieval_fails_graciously(httpserver):
  81. httpserver.serve_content('File not found!', 404)
  82. pytest.raises(ContentNotFoundException, scrape, httpserver.url)
  83. The same thing works for SMTP servers, too::
  84. def test_sending_some_message(smtpserver):
  85. mailer = MyMailer(host=smtpserver.addr[0], port=smtpserver.addr[1])
  86. mailer.send(to='bob@example.com', from_='alice@example.com',
  87. subject='MyMailer v1.0', body='Check out my mailer!')
  88. assert len(smtpserver.outbox)==1
  89. Here an SMTP server is started which accepts e-mails being sent to it. The
  90. nice feature here is that you can actually check if the message was received
  91. and what was sent by looking into the smtpserver's ``outbox``.
  92. It is really that easy!
  93. Available funcargs
  94. ==================
  95. Here is a short overview of the available funcargs. For more details I suggest
  96. poking around in the code itself.
  97. ``httpserver``
  98. provides a threaded HTTP server instance running on localhost. It has the
  99. following attributes:
  100. * ``code`` - HTTP response code (int)
  101. * ``content`` - content of next response (str, bytes, or iterable of either)
  102. * ``headers`` - response headers (dict)
  103. * ``chunked`` - whether to chunk-encode the response (enumeration)
  104. * ``store_request_data`` - whether to store request data for later use
  105. Once these attributes are set, all subsequent requests will be answered with
  106. these values until they are changed or the server is stopped. A more
  107. convenient way to change these is ::
  108. httpserver.serve_content(content=None, code=200, headers=None, chunked=pytest_localserver.http.Chunked.NO, store_request_data=True)
  109. The ``chunked`` attribute or parameter can be set to
  110. * ``Chunked.YES``, telling the server to always apply chunk encoding
  111. * ``Chunked.NO``, telling the server to never apply chunk encoding
  112. * ``Chunked.AUTO``, telling the server to apply chunk encoding only if
  113. the ``Transfer-Encoding`` header includes ``chunked``
  114. If chunk encoding is applied, each str or bytes in ``content`` becomes one
  115. chunk in the response.
  116. You can use ``store_request_data=False`` to disable loading the request data into
  117. memory. This will make it impossible to check the request data using
  118. ``httpserver.requests[index].data`` but may make sense when posting a larger amount of
  119. data and you don't need to check this.
  120. The server address can be found in property
  121. * ``url``
  122. which is the string representation of tuple ``server_address`` (host as str,
  123. port as int).
  124. If you want to check which form fields have been POSTed, Try ::
  125. httpserver.serve_content(..., show_post_vars=True)
  126. which will display them as parsable text.
  127. If you need to inspect the requests sent to the server, a list of all
  128. received requests can be found in property
  129. * ``requests``
  130. which is a list of ``werkzeug.wrappers.Request`` objects.
  131. ``httpsserver``
  132. is the same as ``httpserver`` only with SSL encryption.
  133. ``smtpserver``
  134. provides a threaded SMTP server, with an API similar to ``smtpd.SMTPServer``,
  135. (the deprecated class from the Python standard library) running on localhost.
  136. It has the following attributes:
  137. * ``addr`` - server address as tuple (host as str, port as int)
  138. * ``outbox`` - list of ``email.message.Message`` instances received.
  139. Using your a WSGI application as test server
  140. ============================================
  141. As of version 0.3 you can now use a `WSGI application`_ to run on the test
  142. server ::
  143. from pytest_localserver.http import WSGIServer
  144. def simple_app(environ, start_response):
  145. """Simplest possible WSGI application"""
  146. status = '200 OK'
  147. response_headers = [('Content-type', 'text/plain')]
  148. start_response(status, response_headers)
  149. return ['Hello world!\n']
  150. @pytest.fixture
  151. def testserver(request):
  152. """Defines the testserver funcarg"""
  153. server = WSGIServer(application=simple_app)
  154. server.start()
  155. request.addfinalizer(server.stop)
  156. return server
  157. def test_retrieve_some_content(testserver):
  158. assert scrape(testserver.url) == 'Hello world!\n'
  159. Have a look at the following page for more information on WSGI:
  160. http://wsgi.readthedocs.org/en/latest/learn.html
  161. Download and Installation
  162. =========================
  163. You can install the plugin by running ::
  164. pip install pytest-localserver
  165. Alternatively, get the latest stable version from `PyPI`_ or the latest
  166. `bleeding-edge`_ from Github.
  167. License and Credits
  168. ===================
  169. This plugin is released under the MIT license. You can find the full text of
  170. the license in the LICENSE file.
  171. Copyright (C) 2010-2022 Sebastian Rahlf and others (see AUTHORS).
  172. Some parts of this package is based on ideas or code from other people:
  173. - I borrowed some implementation ideas for the httpserver from `linkchecker`_.
  174. - The implementation for the SMTP server is based on the `Mailsink recipe`_ by
  175. Adam Feuer, Matt Branthwaite and Troy Frever.
  176. - The HTTPS implementation is based on work by `Sebastien Martini`_.
  177. Thanks guys!
  178. Development and future plans
  179. ============================
  180. Feel free to clone the repository and add your own changes. Pull requests are
  181. always welcome!::
  182. git clone https://github.com/pytest-dev/pytest-localserver
  183. If you find any bugs, please file a `report`_.
  184. Test can be run with tox.
  185. I already have a couple of ideas for future versions:
  186. * support for FTP, SSH (maybe base all on twisted?)
  187. * making the SMTP outbox as convenient to use as ``django.core.mail.outbox``
  188. * add your own here!
  189. Preparing a release
  190. -------------------
  191. For package maintainers, here is how we release a new version:
  192. #. Ensure that the ``CHANGES`` file is up to date with the latest changes.
  193. #. Make sure that all tests pass on the version you want to release.
  194. #. Use the `new release form on Github`_ (or some other equivalent method) to
  195. create a new release, following the pattern of previous releases.
  196. * Each release has to be based on a tag. You can either create the tag first
  197. (e.g. using ``git tag``) and then make a release from that tag, or you can
  198. have Github create the tag as part of the process of making a release;
  199. either way works.
  200. * The tag name **must** be the `PEP 440`_-compliant version number prefixed
  201. by ``v``, making sure to include at least three version number components
  202. (e.g. ``v0.6.0``).
  203. * The "Auto-generate release notes" button will be useful in summarizing
  204. the changes since the last release.
  205. #. Using either the `release workflows page`_ or the link in the email you
  206. received about a "Deployment review", go to the workflow run created for
  207. the new release and click "Review deployments", then either approve or reject
  208. the two deployments, one to Test PyPI and one to real PyPI. (It should not be
  209. necessary to reject a deployment unless something really weird happens.)
  210. Once the deployment is approved, Github will automatically upload the files.
  211. ----
  212. .. [1] The idea for this project was born when I needed to check that `a piece
  213. of software`_ behaved itself when receiving HTTP error codes 404 and 500.
  214. Having unsuccessfully tried to mock a server, I stumbled across
  215. `linkchecker`_ which uses a the same idea to test its internals.
  216. .. _monkeypatching: http://pytest.org/latest/monkeypatch.html
  217. .. _pytest: http://pytest.org/
  218. .. _funcargs feature: http://pytest.org/latest/funcargs.html
  219. .. _linkchecker: http://linkchecker.sourceforge.net/
  220. .. _WSGI application: http://www.python.org/dev/peps/pep-0333/
  221. .. _PyPI: http://pypi.python.org/pypi/pytest-localserver/
  222. .. _bleeding-edge: https://github.com/pytest-dev/pytest-localserver
  223. .. _report: https://github.com/pytest-dev/pytest-localserver/issues/
  224. .. _tox: http://testrun.org/tox/
  225. .. _a piece of software: http://pypi.python.org/pypi/python-amazon-product-api/
  226. .. _Mailsink recipe: http://code.activestate.com/recipes/440690/
  227. .. _Sebastien Martini: http://code.activestate.com/recipes/442473/
  228. .. _PEP 440: https://peps.python.org/pep-0440/
  229. .. _build: https://pypa-build.readthedocs.io/en/latest/
  230. .. _twine: https://twine.readthedocs.io/en/stable/
  231. .. _new release form on Github: https://github.com/pytest-dev/pytest-localserver/releases/new
  232. .. _release workflows page: https://github.com/pytest-dev/pytest-localserver/actions/workflows/release.yml