File: test_server.py

package info (click to toggle)
aresponses 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: python: 713; makefile: 66; sh: 5
file content (423 lines) | stat: -rw-r--r-- 13,177 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import asyncio
import re

import aiohttp
import pytest
import sys
from aiohttp import ServerDisconnectedError

import aresponses as aresponses_mod

# example test in readme.md
from aresponses.errors import (
    NoRouteFoundError,
    UnusedRouteError,
    UnorderedRouteCallError,
)


@pytest.mark.asyncio
async def test_foo(aresponses):
    # text as response (defaults to status 200 response)
    aresponses.add("foo.com", "/", "get", "hi there!!")

    # custom status code response
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="error", status=500))

    # JSON response
    aresponses.add("foo.com", "/", "get", {"status": "ok"})

    # passthrough response (makes an actual network call)
    aresponses.add("httpstat.us", "/200", "get", aresponses.passthrough)

    # custom handler response
    def my_handler(request):
        return aresponses.Response(status=200, text=str(request.url))

    aresponses.add("foo.com", "/", "get", my_handler)

    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
            assert text == "hi there!!"

        async with session.get(url) as response:
            text = await response.text()
            assert text == "error"
            assert response.status == 500

        async with session.get(url) as response:
            text = await response.text()
            assert text == '{"status": "ok"}'

        async with session.get("https://httpstat.us/200") as response:
            text = await response.text()
        assert text == "200 OK"

        async with session.get(url) as response:
            text = await response.text()
            assert text == "http://foo.com/"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_fixture(aresponses):
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi"))

    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "hi"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_body_match(aresponses):
    aresponses.add(
        "foo.com",
        "/",
        "get",
        aresponses.Response(text="hi"),
        body_pattern=re.compile(r".*?apple.*"),
    )

    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        try:
            async with session.get(url, data={"fruit": "pineapple"}) as response:
                text = await response.text()
                assert text == "hi"
        except ServerDisconnectedError:
            pass

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_https(aresponses):
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi"))

    url = "https://foo.com"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "hi"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_context_manager():
    loop = asyncio.get_running_loop()
    async with aresponses_mod.ResponsesMockServer(loop=loop) as arsps:
        arsps.add("foo.com", "/", "get", aresponses_mod.Response(text="hi"))

        url = "http://foo.com"
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                text = await response.text()
        assert text == "hi"

    arsps.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_bad_redirect(aresponses):
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi", status=301))
    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        response = await session.get(url)
        await response.text()

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_regex(aresponses):
    aresponses.add(
        aresponses.ANY, aresponses.ANY, aresponses.ANY, aresponses.Response(text="hi")
    )
    aresponses.add(
        aresponses.ANY,
        aresponses.ANY,
        aresponses.ANY,
        aresponses.Response(text="there"),
    )

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com") as response:
            text = await response.text()
            assert text == "hi"

        async with session.get("http://bar.com") as response:
            text = await response.text()
            assert text == "there"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_callable(aresponses):
    def handler(request):
        return aresponses.Response(body=request.host)

    aresponses.add(aresponses.ANY, aresponses.ANY, aresponses.ANY, handler)
    aresponses.add(aresponses.ANY, aresponses.ANY, aresponses.ANY, handler)

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com") as response:
            text = await response.text()
            assert text == "foo.com"

        async with session.get("http://bar.com") as response:
            text = await response.text()
            assert text == "bar.com"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_raw_response(aresponses):
    raw_response = b"""HTTP/1.1 200 OK\r
Date: Tue, 26 Dec 2017 05:47:50 GMT\r
\r
<html><body><h1>It works!</h1></body></html>
"""
    aresponses.add(
        aresponses.ANY,
        aresponses.ANY,
        aresponses.ANY,
        aresponses.RawResponse(raw_response),
    )

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com") as response:
            text = await response.text()
            assert "It works!" in text

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_querystring(aresponses):
    aresponses.add("foo.com", "/path", "get", aresponses.Response(text="hi"))

    url = "http://foo.com/path?reply=42"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "hi"

    aresponses.add(
        "foo.com",
        "/path2?reply=42",
        "get",
        aresponses.Response(text="hi"),
        match_querystring=True,
    )

    url = "http://foo.com/path2?reply=42"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "hi"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_querystring_not_match(aresponses):
    aresponses.add(
        "foo.com",
        "/path",
        "get",
        aresponses.Response(text="hi"),
        match_querystring=True,
    )
    aresponses.add(
        "foo.com",
        aresponses.ANY,
        "get",
        aresponses.Response(text="miss"),
        match_querystring=True,
    )
    aresponses.add(
        "foo.com",
        aresponses.ANY,
        "get",
        aresponses.Response(text="miss"),
        match_querystring=True,
    )

    url = "http://foo.com/path"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "hi"

    url = "http://foo.com/path?reply=42"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "miss"

    url = "http://foo.com/path?reply=43"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "miss"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_passthrough(aresponses):
    aresponses.add("httpstat.us", "/200", "get", aresponses.passthrough)

    url = "https://httpstat.us/200"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
    assert text == "200 OK"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_failure_not_called(aresponses):
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi"))
    with pytest.raises(UnusedRouteError):
        aresponses.assert_no_unused_routes()

    with pytest.raises(UnusedRouteError):
        aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_failure_no_match(aresponses):
    async with aiohttp.ClientSession() as session:
        try:
            async with session.get("http://foo.com") as response:
                await response.text()
        except ServerDisconnectedError:
            pass
    with pytest.raises(NoRouteFoundError):
        aresponses.assert_all_requests_matched()

    with pytest.raises(NoRouteFoundError):
        aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_failure_bad_ordering(aresponses):
    aresponses.add("foo.com", "/a", "get", aresponses.Response(text="hi"))
    aresponses.add("foo.com", "/b", "get", aresponses.Response(text="hi"))

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com/b") as response:
            await response.text()
        async with session.get("http://foo.com/a") as response:
            await response.text()

    aresponses.assert_all_requests_matched()
    aresponses.assert_no_unused_routes()
    with pytest.raises(UnorderedRouteCallError):
        aresponses.assert_called_in_order()

    with pytest.raises(UnorderedRouteCallError):
        aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_failure_not_called_enough_times(aresponses):
    aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi"), repeat=2)

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com/") as response:
            await response.text()

    with pytest.raises(UnusedRouteError):
        aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_history(aresponses):
    aresponses.add(response=aresponses.Response(text="hi"), repeat=2)

    async with aiohttp.ClientSession() as session:
        async with session.get("http://foo.com/b") as response:
            await response.text()
        async with session.get("http://bar.com/a") as response:
            await response.text()

    assert len(aresponses.history) == 2
    assert aresponses.history[0].request.host == "foo.com"
    assert aresponses.history[1].request.host == "bar.com"
    assert "Route(" in repr(aresponses.history[0].route)
    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_history_post(aresponses):
    """Ensure the request contents exist in the history"""
    aresponses.add(method_pattern="POST", response={"some": "response"})

    async with aiohttp.ClientSession() as session:
        async with session.post(
            "http://bar.com/zzz", json={"greeting": "hello"}
        ) as response:
            response_data = await response.json()
            assert response_data == {"some": "response"}

    assert len(aresponses.history) == 1
    assert aresponses.history[0].request.host == "bar.com"
    request_data = await aresponses.history[0].request.json()
    assert request_data == {"greeting": "hello"}
    assert "Route(" in repr(aresponses.history[0].route)
    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_history_post_binary(aresponses):
    """
    Ensure the request contents exist in the history

    ...and that it can handle binary requests
    """
    binary_not_utf = b"o\xad<|6\xd2a\x116\x17\xdb\x98-60:"
    aresponses.add(method_pattern="POST", response={"some": "response"})

    async with aiohttp.ClientSession() as session:
        async with session.post("http://bar.com/zzz", data=binary_not_utf) as response:
            response_data = await response.json()
            assert response_data == {"some": "response"}

    assert len(aresponses.history) == 1
    assert aresponses.history[0].request.host == "bar.com"
    request_data = await aresponses.history[0].request.read()
    assert request_data == binary_not_utf


@pytest.fixture()
def _short_recursion_limit():
    # The default is 1000, but it takes time (seconds); 100 is much faster.
    old_limit = sys.getrecursionlimit()
    sys.setrecursionlimit(100)
    yield
    sys.setrecursionlimit(old_limit)


@pytest.mark.asyncio
@pytest.mark.usefixtures("_short_recursion_limit")
async def test_not_exceeding_recursion_limit():
    loop = asyncio.get_running_loop()
    for _ in range(sys.getrecursionlimit()):
        async with aresponses_mod.ResponsesMockServer(loop=loop) as arsps:
            arsps.add("fake-host", "/", "get", "hello")
            async with aiohttp.ClientSession() as session:
                async with session.get("http://fake-host"):
                    pass