File: test_live_server.py

package info (click to toggle)
pytest-flask 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: python: 553; makefile: 161; sh: 5
file content (225 lines) | stat: -rwxr-xr-x 7,348 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import os

import pytest
from flask import url_for


pytestmark = pytest.mark.skipif(not hasattr(os, "fork"), reason="needs fork")


class TestLiveServer:
    def test_init(self, live_server):
        assert live_server.port
        assert live_server.host == "localhost"
        assert live_server.url() == "http://localhost:{0}".format(live_server.port)

    def test_server_is_alive(self, live_server):
        assert live_server._process
        assert live_server._process.is_alive()

    def test_server_listening(self, client, live_server):
        res = client.get(url_for("ping", _external=True))
        assert res.status_code == 200
        assert b"pong" in res.data

    def test_url_for(self, live_server):
        assert (
            url_for("ping", _external=True)
            == "http://localhost.localdomain:%s/ping" % live_server.port
        )

    def test_set_application_server_name(self, live_server):
        assert (
            live_server.app.config["SERVER_NAME"]
            == "localhost.localdomain:%d" % live_server.port
        )

    def test_rewrite_application_server_name(self, appdir):
        appdir.create_test_module(
            """
            import pytest
            @pytest.mark.options(server_name='example.com:5000')
            def test_a(live_server):
                assert live_server.app.config['SERVER_NAME'] == \\
                    'example.com:%d' % live_server.port
        """
        )

        result = appdir.runpytest("-v", "-o", "live_server_scope=function")
        result.stdout.fnmatch_lines(["*PASSED*"])
        assert result.ret == 0

    def test_prevent_starting_live_server(self, appdir):
        appdir.create_test_module(
            """
            import pytest

            def test_a(live_server):
                assert live_server._process is None
        """
        )

        result = appdir.runpytest("-v", "--no-start-live-server")
        result.stdout.fnmatch_lines(["*passed*"])
        assert result.ret == 0

    def test_start_live_server(self, appdir):
        appdir.create_test_module(
            """
            import pytest

            def test_a(live_server):
                assert live_server._process
                assert live_server._process.is_alive()
        """
        )
        result = appdir.runpytest("-v", "--start-live-server")
        result.stdout.fnmatch_lines(["*passed*"])
        assert result.ret == 0

    def test_stop_cleanly_join_exception(self, appdir, live_server, caplog):
        # timeout = 'a' here to force an exception when
        # attempting to self._process.join()
        assert not live_server._stop_cleanly(timeout="a")
        assert "Failed to join" in caplog.text

    @pytest.mark.parametrize("clean_stop", [True, False])
    def test_clean_stop_live_server(self, appdir, monkeypatch, clean_stop):
        """Ensure the fixture is trying to cleanly stop the server.

        Because this is tricky to test, we are checking that the
        _stop_cleanly() internal function was called and reported success.
        """
        from pytest_flask.fixtures import LiveServer

        original_stop_cleanly_func = LiveServer._stop_cleanly

        stop_cleanly_result = []

        def mocked_stop_cleanly(*args, **kwargs):
            result = original_stop_cleanly_func(*args, **kwargs)
            stop_cleanly_result.append(result)
            return result

        monkeypatch.setattr(LiveServer, "_stop_cleanly", mocked_stop_cleanly)

        appdir.create_test_module(
            """
            import pytest

            from flask import url_for

            def test_a(live_server, client):
                @live_server.app.route('/')
                def index():
                    return 'got it', 200

                live_server.start()

                res = client.get(url_for('index', _external=True))
                assert res.status_code == 200
                assert b'got it' in res.data
        """
        )
        args = [] if clean_stop else ["--no-live-server-clean-stop"]
        result = appdir.runpytest_inprocess("-v", "--no-start-live-server", *args)
        result.stdout.fnmatch_lines("*1 passed*")
        if clean_stop:
            assert stop_cleanly_result == [True]
        else:
            assert stop_cleanly_result == []

    def test_add_endpoint_to_live_server(self, appdir):
        appdir.create_test_module(
            """
            import pytest

            from flask import url_for

            def test_a(live_server, client):
                @live_server.app.route('/new-endpoint')
                def new_endpoint():
                    return 'got it', 200

                live_server.start()

                res = client.get(url_for('new_endpoint', _external=True))
                assert res.status_code == 200
                assert b'got it' in res.data
        """
        )
        result = appdir.runpytest("-v", "--no-start-live-server")
        result.stdout.fnmatch_lines(["*passed*"])
        assert result.ret == 0

    @pytest.mark.skip("this test hangs in the original code")
    def test_concurrent_requests_to_live_server(self, appdir):
        appdir.create_test_module(
            """
            import pytest

            from flask import url_for

            def test_concurrent_requests(live_server, client):
                @live_server.app.route('/one')
                def one():
                    res = client.get(url_for('two', _external=True))
                    return res.data

                @live_server.app.route('/two')
                def two():
                    return '42'

                live_server.start()

                res = client.get(url_for('one', _external=True))
                assert res.status_code == 200
                assert b'42' in res.data
        """
        )
        result = appdir.runpytest("-v", "--no-start-live-server")
        result.stdout.fnmatch_lines(["*passed*"])
        assert result.ret == 0

    @pytest.mark.parametrize("port", [5000, 5001])
    def test_live_server_fixed_port(self, port, appdir):
        appdir.create_test_module(
            """
            import pytest

            def test_port(live_server):
                assert live_server.port == %d
        """
            % port
        )
        result = appdir.runpytest("-v", "--live-server-port", str(port))
        result.stdout.fnmatch_lines(["*PASSED*"])
        assert result.ret == 0

    @pytest.mark.parametrize("host", ["127.0.0.1", "0.0.0.0"])
    def test_live_server_fixed_host(self, host, appdir):
        appdir.create_test_module(
            """
            import pytest

            def test_port(live_server):
                assert live_server.host == '%s'
        """
            % host
        )
        result = appdir.runpytest("-v", "--live-server-host", str(host))
        result.stdout.fnmatch_lines(["*PASSED*"])
        assert result.ret == 0

    def test_respect_wait_timeout(self, appdir):
        appdir.create_test_module(
            """
            import pytest

            def test_should_fail(live_server):
                assert live_server._process.is_alive()
        """
        )
        result = appdir.runpytest("-v", "--live-server-wait=0.00000001")
        result.stdout.fnmatch_lines(["**ERROR**"])
        assert result.ret == 1