Пример #1
0
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()
Пример #2
0
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()
Пример #3
0
async def test_safe_mocked_error_response_eventually_fails(aresponses):
    # when all calls fail it should fail
    aresponses.add("blabla", "/", "GET",
                   aresponses.Response(status=500, text="Error"))
    with pytest.raises(Exception):
        response = await safe_http_get_json('http://blabla/')
    aresponses.assert_called_in_order()
Пример #4
0
async def test_fixture_body_json_failed(aresponses):
    aresponses.add("foo.com", "/", "post", aresponses.Response(text="hi"), body={"a": 2})

    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        async with session.post(url, json={"a": 1}) as _:
            pass
Пример #5
0
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'
Пример #6
0
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'
Пример #7
0
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"
Пример #8
0
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"
Пример #9
0
async def test_fixture_body_text(aresponses):
    aresponses.add("foo.com", "/", "post", aresponses.Response(text="hi"), body='{"a": 1}')

    url = "http://foo.com"
    async with aiohttp.ClientSession() as session:
        async with session.post(url, json={"a": 1}) as response:
            text = await response.text()
    assert text == "hi"
Пример #10
0
async def test_mocked__acceptable_error_response_passes(aresponses):
    # when failure is acceptable - pass
    aresponses.add(
        "blabla", "/", "GET",
        aresponses.Response(status=500,
                            body=b'{"error": "true"}',
                            headers={"Content-Type": "application/json"}))
    response = await http_get_json('http://blabla', acceptable_codes=[500])
    aresponses.assert_called_in_order()
    assert "error" in response
Пример #11
0
async def test_safe_mocked_error_response_eventually_passes(aresponses):
    # first try will fail - second try will pass. should pass
    aresponses.add("blabla",
                   "/",
                   "GET",
                   aresponses.Response(status=500, text="Error"),
                   repeat=1)
    aresponses.add("blabla", "/", "GET", response={"status": "ok"})
    response = await safe_http_get_json('http://blabla/')
    assert "status" in response
    aresponses.assert_called_in_order()
Пример #12
0
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
<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
Пример #13
0
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"
Пример #14
0
async def test_401_bad_credentials(aresponses, event_loop):
    """Test that a 401 is thrown when credentials fail."""
    aresponses.add(
        "api.simplisafe.com",
        "/v1/api/token",
        "post",
        aresponses.Response(text="", status=401),
    )

    async with aiohttp.ClientSession(loop=event_loop) as websession:
        with pytest.raises(InvalidCredentialsError):
            await API.login_via_credentials(TEST_EMAIL, TEST_PASSWORD,
                                            websession)
Пример #15
0
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'
Пример #16
0
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))

    # 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("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/"
Пример #17
0
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))

    # 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('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/'
Пример #18
0
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'
Пример #19
0
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"
Пример #20
0
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'
Пример #21
0
async def test_mocked_error_response(aresponses):
    aresponses.add("blabla", "/api/v1", "GET", response={"status": "ok"})
    response = await http_get_json('http://blabla/api/v1')
    assert "status" in response
    aresponses.assert_called_in_order()