How to use blacksheep - 10 common examples

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 / test_application.py View on Github external
async def exception_handler(self, request, http_exception):
        nonlocal app
        assert self is app
        return Response(200, content=TextContent('Called'))
github RobertoPrevato / BlackSheep / tests / test_application.py View on Github external
async def example(request):
        nonlocal calls
        calls.append(5)
        return Response(200, [(b'Server', b'Python/3.7')], JsonContent({'id': '123'}))
github RobertoPrevato / BlackSheep / tests / test_bindings.py View on Github external
async def test_from_body_json_binding_extra_parameters_strategy():

    request = Request('POST', b'/', [
        JsonContentType
    ]).with_content(JsonContent({
        'a': 'world',
        'b': 9000,
        'c': 'This is an extra parameter, accepted by constructor explicitly'
    }))

    parameter = FromJson(ExampleTwo)

    value = await parameter.get_value(request)

    assert isinstance(value, ExampleTwo)
    assert value.a == 'world'
    assert value.b == 9000
github RobertoPrevato / BlackSheep / tests / test_requests.py View on Github external
def test_request_declares_json(headers, expected_result):
    request = Request('GET', b'/', headers)
    assert request.declares_json() is expected_result
github RobertoPrevato / BlackSheep / tests / test_requests.py View on Github external
def test_small_request_headers_add_through_higher_api():
    request = Request('GET', b'https://hello-world', None)

    request.headers.add(b'Hello', b'World')

    raw_bytes = write_small_request(request)

    assert b'Hello: World\r\n' in raw_bytes
github RobertoPrevato / BlackSheep / tests / test_bindings.py View on Github external
async def test_from_body_json_binding_request_missing_content_type():

    request = Request('POST', b'/', [])

    parameter = FromJson(ExampleOne)

    value = await parameter.get_value(request)

    assert value is None
github RobertoPrevato / BlackSheep / tests / test_bindings.py View on Github external
async def test_identity_binder():
    request = Request('GET', b'/', None)
    request.identity = object()

    parameter = IdentityBinder()

    value = await parameter.get_value(request)

    assert value is request.identity
github RobertoPrevato / BlackSheep / tests / test_normalization.py View on Github external
async def test_middleware_query_normalization():
    services = {'context': object()}
    fake_request = Request('GET', b'/?example=Lorem', None)

    async def middleware(request, handler, example: FromQuery(str)):
        assert request is fake_request
        assert handler is fake_handler
        assert example == 'Lorem'
        return await handler(request)

    normalized = normalize_middleware(middleware, services)

    # NB: middlewares base signature is (request, handler)
    result = await normalized(fake_request, fake_handler)
    assert result == 'fake-handler-result'
github RobertoPrevato / BlackSheep / tests / test_requests.py View on Github external
def test_cookie_parsing_duplicated_cookie_header_value():
    request = Request('POST', b'/', [
        (b'Cookie', b'ai=something; hello=world; foo=Hello%20World%3B; hello=kitty;')
    ])

    print(request.cookies)

    assert request.cookies == {
        'ai': 'something',
        'hello': 'kitty',
        'foo': 'Hello World;'
    }
github RobertoPrevato / BlackSheep / tests / test_requests.py View on Github external
def test_cookie_parsing_multiple_cookie_headers():
    request = Request('POST', b'/', [
        (b'Cookie', b'ai=something; hello=world; foo=Hello%20World%3B;'),
        (b'Cookie', b'jib=jab; ai=else;'),
    ])

    assert request.cookies == {
        'ai': 'else',
        'hello': 'world',
        'foo': 'Hello World;',
        'jib': 'jab'
    }