aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pytest-localserver/py3/pytest_localserver/plugin.py
blob: 1e6ad2f172020e34a360641cad685b5abafbb837 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (C) 2011 Sebastian Rahlf <basti at redtoad dot de>
#
# This program is release under the MIT license. You can find the full text of
# the license in the LICENSE file.
import os
import pkgutil

import pytest


@pytest.fixture
def httpserver(request):
    """The returned ``httpserver`` provides a threaded HTTP server instance
    running on a randomly assigned port on localhost. It can be taught which
    content (i.e. string) to serve with which response code and comes with
    following attributes:

    * ``code`` - HTTP response code (int)
    * ``content`` - content of next response (str)
    * ``headers`` - response headers (dict)

    Once these attribute 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='My content', code=200,
            headers={'content-type': 'text/plain'})

    The server address can be found in property

    * ``url``

    which is the string representation of tuple ``server_address`` (host as
    str, port as int).

    Example::

        import requests
        def scrape(url):
            html = requests.get(url).text
            # some parsing happens here
            # ...
            return result

        def test_retrieve_some_content(httpserver):
            httpserver.serve_content(open('cached-content.html').read())
            assert scrape(httpserver.url) == 'Found it!'

    """
    from pytest_localserver import http

    server = http.ContentServer()
    server.start()
    request.addfinalizer(server.stop)
    return server


@pytest.fixture
def httpsserver(request):
    """The returned ``httpsserver`` (note the additional S!) provides a
    threaded HTTP server instance similar to funcarg ``httpserver`` but with
    SSL encryption.
    """
    from pytest_localserver import https
    try:
        with open(https.DEFAULT_CERTIFICATE, 'wb') as f:
            f.write(pkgutil.get_data('pytest_localserver', 'server.pem'))
        server = https.SecureContentServer()
        server.start()
        request.addfinalizer(server.stop)
        yield server
    finally:
        os.remove(https.DEFAULT_CERTIFICATE)


@pytest.fixture
def smtpserver(request):
    """The returned ``smtpserver`` provides a threaded instance of
    ``smtpd.SMTPServer`` running on localhost.  It has the following
    attributes:

    * ``addr`` - server address as tuple (host as str, port as int)
    """
    from pytest_localserver import smtp

    server = smtp.Server()
    server.start()
    request.addfinalizer(server.stop)
    return server