How to use the blacksheep.TextContent function in blacksheep

To help you get started, we’ve selected a few blacksheep examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RobertoPrevato / BlackSheep / tests / client / test_headers.py View on Github external
async def test_request_headers():
    fake_pools = FakePools([Response(200, [], TextContent('Hello, World!'))])

    async def middleware_for_assertions(request, next_handler):
        assert b'Hello' in request.headers
        assert request.headers.get_single(b'Hello') == b'World'

        return await next_handler(request)

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_for_assertions]
                             ) as client:
        await client.get(b'/', headers=[(b'Hello', b'World')])
        await client.post(b'/', headers=[(b'Hello', b'World')])
        await client.put(b'/', headers=[(b'Hello', b'World')])
        await client.delete(b'/', headers=[(b'Hello', b'World')])
github RobertoPrevato / BlackSheep / tests / test_application.py View on Github external
async def exception_handler(self, request, exception: CustomException):
        nonlocal called
        assert request is not None
        called = True
        return Response(200, content=TextContent('Called'))
github RobertoPrevato / BlackSheep / tests / client / test_cookiejar.py View on Github external
async def test_remove_cookie_with_expiration():
    expire_cookie = Cookie(b'X-Foo', b'Foo')
    expire_cookie.expiration = datetime.utcnow() + timedelta(days=-2)
    fake_pools = FakePools([Response(200, [(b'Set-Cookie', write_response_cookie(Cookie(b'X-Foo', b'Foo')))])
                           .with_content(TextContent('Hello, World!')),
                            Response(200, None, TextContent('Hello!')),
                            Response(200, [(b'Set-Cookie', write_response_cookie(expire_cookie))])
                           .with_content(TextContent('Hello, World!')),
                            Response(200, None, TextContent('Hello!'))])
    expect_cookie = False

    async def middleware_for_assertions(request, next_handler):
        cookie = request.cookies.get('X-Foo')
        if expect_cookie:
            assert cookie is not None, 'X-Foo cookie must be configured'
        else:
            assert cookie is None

        return await next_handler(request)

    async with ClientSession(base_url=b'https://bezkitu.org',
github RobertoPrevato / BlackSheep / tests / client / test_cookiejar.py View on Github external
async def test_cookies_jar(first_request_url, second_request_url, set_cookies, expected_cookies):
    fake_pools = FakePools([Response(200, set_cookies, TextContent('Hello, World!')),
                            Response(200, None, TextContent('Hello!'))])
    check_cookie = False

    async def middleware_for_assertions(request, next_handler):
        if check_cookie:
            if not expected_cookies:
                assert not request.cookies

            for expected_cookie in expected_cookies:
                cookie = request.cookies.get(expected_cookie)
                assert cookie is not None, f'{cookie.name.decode()} cookie must be configured for following requests'

        return await next_handler(request)

    async with ClientSession(pools=fake_pools,
                             middlewares=[middleware_for_assertions],
github RobertoPrevato / BlackSheep / tests / client / test_middlewares.py View on Github external
async def test_single_middleware():
    fake_pools = FakePools([Response(200, None, TextContent('Hello, World!'))])

    steps = []

    async def middleware_one(request, next_handler):
        steps.append(1)
        response = await next_handler(request)
        steps.append(2)
        return response

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_one]
                             ) as client:
        response = await client.get(b'/')

        assert steps == [1, 2]
github RobertoPrevato / BlackSheep / blacksheep / server / responses.py View on Github external
def status_code(status: int = 200, message: MessageType = None):
    """Returns a plain response with given status, with optional message; sent as plain text or JSON."""
    if not message:
        return Response(status)
    if isinstance(message, str):
        content = TextContent(message)
    else:
        content = JsonContent(message)
    return Response(status, content=content)
github RobertoPrevato / BlackSheep / blacksheep / server / authentication.py View on Github external
async def handle_authentication_challenge(app,
                                          request,
                                          exception: AuthenticateChallenge):
    return Response(401,
                    [exception.get_header()],
                    content=TextContent('Unauthorized'))