README.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ==================
  2. pytest-localserver
  3. ==================
  4. pytest-localserver is a plugin for the `pytest`_ testing framework which enables
  5. you to test server connections locally.
  6. Sometimes `monkeypatching`_ ``urllib2.urlopen()`` just does not cut it, for
  7. instance if you work with ``urllib2.Request``, define your own openers/handlers
  8. or work with ``httplib``. In these cases it may come in handy to have an HTTP
  9. server running locally which behaves just like the real thing [1]_. Well, look
  10. no further!
  11. Quickstart
  12. ==========
  13. Let's say you have a function to scrape HTML which only required to be pointed
  14. at a URL ::
  15. import requests
  16. def scrape(url):
  17. html = requests.get(url).text
  18. # some parsing happens here
  19. # ...
  20. return result
  21. You want to test this function in its entirety without having to rely on a
  22. remote server whose content you cannot control, neither do you want to waste
  23. time setting up a complex mechanism to mock or patch the underlying Python
  24. modules dealing with the actual HTTP request (of which there are more than one
  25. BTW). So what do you do?
  26. You simply use pytest's `funcargs feature`_ and simulate an entire server
  27. locally! ::
  28. def test_retrieve_some_content(httpserver):
  29. httpserver.serve_content(open('cached-content.html').read())
  30. assert scrape(httpserver.url) == 'Found it!'
  31. What happened here is that for the duration of your tests an HTTP server is
  32. started on a random port on localhost which will serve the content you tell it
  33. to and behaves just like the real thing.
  34. The added bonus is that you can test whether your code behaves gracefully if
  35. there is a network problem::
  36. def test_content_retrieval_fails_graciously(httpserver):
  37. httpserver.serve_content('File not found!', 404)
  38. pytest.raises(ContentNotFoundException, scrape, httpserver.url)
  39. The same thing works for SMTP servers, too::
  40. def test_sending_some_message(smtpserver):
  41. mailer = MyMailer(host=smtpserver.addr[0], port=smtpserver.addr[1])
  42. mailer.send(to='bob@example.com', from_='alice@example.com',
  43. subject='MyMailer v1.0', body='Check out my mailer!')
  44. assert len(smtpserver.outbox)==1
  45. Here an SMTP server is started which accepts e-mails being sent to it. The
  46. nice feature here is that you can actually check if the message was received
  47. and what was sent by looking into the smtpserver's ``outbox``.
  48. It is really that easy!
  49. Available funcargs
  50. ==================
  51. Here is a short overview of the available funcargs. For more details I suggest
  52. poking around in the code itself.
  53. ``httpserver``
  54. provides a threaded HTTP server instance running on localhost. It has the
  55. following attributes:
  56. * ``code`` - HTTP response code (int)
  57. * ``content`` - content of next response (str)
  58. * ``headers`` - response headers (dict)
  59. Once these attribute are set, all subsequent requests will be answered with
  60. these values until they are changed or the server is stopped. A more
  61. convenient way to change these is ::
  62. httpserver.serve_content(content=None, code=200, headers=None)
  63. The server address can be found in property
  64. * ``url``
  65. which is the string representation of tuple ``server_address`` (host as str,
  66. port as int).
  67. If you want to check which form fields have been POSTed, Try ::
  68. httpserver.serve_content(..., show_post_vars=True)
  69. which will display them as parsable text.
  70. If you need to inspect the requests sent to the server, a list of all
  71. received requests can be found in property
  72. * ``requests``
  73. which is a list of ``werkzeug.wrappers.Request`` objects.
  74. ``httpsserver``
  75. is the same as ``httpserver`` only with SSL encryption.
  76. ``smtpserver``
  77. provides a threaded instance of ``smtpd.SMTPServer`` runnning on localhost.
  78. It has the following attributes:
  79. * ``addr`` - server address as tuple (host as str, port as int)
  80. * ``outbox`` - list of ``email.message.Message`` instances received.
  81. Using your a WSGI application as test server
  82. ============================================
  83. As of version 0.3 you can now use a `WSGI application`_ to run on the test
  84. server ::
  85. from pytest_localserver.http import WSGIServer
  86. def simple_app(environ, start_response):
  87. """Simplest possible WSGI application"""
  88. status = '200 OK'
  89. response_headers = [('Content-type', 'text/plain')]
  90. start_response(status, response_headers)
  91. return ['Hello world!\n']
  92. @pytest.fixture
  93. def testserver(request):
  94. """Defines the testserver funcarg"""
  95. server = WSGIServer(application=simple_app)
  96. server.start()
  97. request.addfinalizer(server.stop)
  98. return server
  99. def test_retrieve_some_content(testserver):
  100. assert scrape(testserver.url) == 'Hello world!\n'
  101. Have a look at the following page for more information on WSGI:
  102. http://wsgi.readthedocs.org/en/latest/learn.html
  103. Download and Installation
  104. =========================
  105. You can install the plugin by running ::
  106. pip install pytest-localserver
  107. Alternatively, get the latest stable version from `PyPI`_ or the latest
  108. `bleeding-edge`_ from Github.
  109. License and Credits
  110. ===================
  111. This plugin is released under the MIT license. You can find the full text of
  112. the license in the LICENSE file.
  113. Copyright (C) 2010-2021 Sebastian Rahlf and others (see AUTHORS).
  114. Some parts of this package is based on ideas or code from other people:
  115. - I borrowed some implementation ideas for the httpserver from `linkchecker`_.
  116. - The implementation for the SMTP server is based on the `Mailsink recipe`_ by
  117. Adam Feuer, Matt Branthwaite and Troy Frever.
  118. - The HTTPS implementation is based on work by `Sebastien Martini`_.
  119. Thanks guys!
  120. Development and future plans
  121. ============================
  122. Feel free to clone the repository and add your own changes. Pull requests are
  123. always welcome!::
  124. git clone https://github.com/pytest-dev/pytest-localserver
  125. If you find any bugs, please file a `report`_.
  126. Test can be run with tox. Note that you need virtualenv<1.8 to run tests for
  127. Python 2.4.
  128. I already have a couple of ideas for future versions:
  129. * support for FTP, SSH (maybe base all on twisted?)
  130. * making the SMTP outbox as convenient to use as ``django.core.mail.outbox``
  131. * add your own here!
  132. ----
  133. .. [1] The idea for this project was born when I needed to check that `a piece
  134. of software`_ behaved itself when receiving HTTP error codes 404 and 500.
  135. Having unsuccessfully tried to mock a server, I stumbled across
  136. `linkchecker`_ which uses a the same idea to test its internals.
  137. .. _monkeypatching: http://pytest.org/latest/monkeypatch.html
  138. .. _pytest: http://pytest.org/
  139. .. _funcargs feature: http://pytest.org/latest/funcargs.html
  140. .. _linkchecker: http://linkchecker.sourceforge.net/
  141. .. _WSGI application: http://www.python.org/dev/peps/pep-0333/
  142. .. _PyPI: http://pypi.python.org/pypi/pytest-localserver/
  143. .. _bleeding-edge: https://github.com/pytest-dev/pytest-localserver
  144. .. _report: https://github.com/pytest-dev/pytest-localserver/issues/
  145. .. _tox: http://testrun.org/tox/
  146. .. _a piece of software: http://pypi.python.org/pypi/python-amazon-product-api/
  147. .. _Mailsink recipe: http://code.activestate.com/recipes/440690/
  148. .. _Sebastien Martini: http://code.activestate.com/recipes/442473/