Exemplo n.º 1
0
def test_from_request_validation_error():
    problem = middleware.from_request_validation_error(
        RequestValidationError(errors=[
            ErrorWrapper(ValueError('foo'), 'here'),
            ErrorWrapper(ValueError('bar'), 'there'),
        ], ))

    assert problem == middleware.Problem(
        type='',
        title='Validation Error',
        status=400,
        detail='One or more user-provided parameters are invalid',
        errors=[
            {
                'loc': ('here', ),
                'msg': 'foo',
                'type': 'value_error',
            },
            {
                'loc': ('there', ),
                'msg': 'bar',
                'type': 'value_error',
            },
        ],
    )
Exemplo n.º 2
0
 def test_to_dict_default_values(self):
     problem = middleware.Problem()
     assert problem.to_dict() == {
         'type': 'about:blank',
         'title': 'Internal Server Error',
         'status': 500,
     }
Exemplo n.º 3
0
def test_from_dict_empty():
    problem = middleware.from_dict({})

    assert problem == middleware.Problem(
        type='about:blank',
        title='Internal Server Error',
        status=500,
    )
Exemplo n.º 4
0
def test_from_exception():
    problem = middleware.from_exception(ValueError('test error'))

    assert problem == middleware.Problem(
        type='',
        title='Unexpected Server Error',
        status=500,
        detail='test error',
        exc_type='ValueError',
    )
Exemplo n.º 5
0
    def test_init(self):
        problem = middleware.Problem()

        assert problem.type == 'about:blank'
        assert problem.status == 500
        assert problem.title == 'Internal Server Error'
        assert problem.detail is None
        assert problem.instance is None
        assert problem.kwargs == {}
        assert problem.debug is False
Exemplo n.º 6
0
    def test_render_problem(self):
        resp = middleware.ProblemResponse(middleware.Problem(), )

        assert resp.media_type == 'application/problem+json'
        assert resp.debug is False
        assert resp.status_code == 500
        assert json.loads(resp.body) == {
            'type': 'about:blank',
            'status': 500,
            'title': 'Internal Server Error',
        }
Exemplo n.º 7
0
def test_from_http_exception():
    problem = middleware.from_http_exception(
        HTTPException(
            status_code=401,
            detail='test http error',
        ))

    assert problem == middleware.Problem(
        type='',
        title='',
        status=401,
        detail='test http error',
    )
Exemplo n.º 8
0
def test_from_dict_with_extras():
    problem = middleware.from_dict({
        'type': 'test-problem',
        'title': 'Test Problem',
        'status': 500,
        'key1': 'extra',
        'key2': {
            'foo': ['bar', 'baz']
        }
    })

    assert problem == middleware.Problem(type='test-problem',
                                         title='Test Problem',
                                         status=500,
                                         key1='extra',
                                         key2={'foo': ['bar', 'baz']})
Exemplo n.º 9
0
def test_from_dict():
    problem = middleware.from_dict({
        'type': 'test-problem',
        'title': 'Test Problem',
        'status': 500,
        'detail': 'a test problem occurred',
        'instance': 'testproblem',
    })

    assert problem == middleware.Problem(
        type='test-problem',
        title='Test Problem',
        status=500,
        detail='a test problem occurred',
        instance='testproblem',
    )
Exemplo n.º 10
0
 def test_to_dict_all_values(self):
     problem = middleware.Problem(
         type='problem-type',
         title='Problem',
         status=500,
         detail='Something happened',
         instance='foo',
         other='bar',
     )
     assert problem.to_dict() == {
         'type': 'problem-type',
         'title': 'Problem',
         'status': 500,
         'detail': 'Something happened',
         'instance': 'foo',
         'other': 'bar',
     }
Exemplo n.º 11
0
 async def app(scope, receive, send):
     raise middleware.Problem()
Exemplo n.º 12
0
 async def problem():
     raise middleware.Problem()
Exemplo n.º 13
0
 def test_str(self):
     assert str(
         middleware.Problem()
     ) == "Problem:<{'type': 'about:blank', 'title': 'Internal Server Error', 'status': 500}>"  # noqa
Exemplo n.º 14
0
 def test_to_bytes_debug(self):
     problem = middleware.Problem()
     problem.debug = True
     assert problem.to_bytes(
     ) == b'{\n  "type": "about:blank",\n  "title": "Internal Server Error",\n  "status": 500\n}'  # noqa
Exemplo n.º 15
0
 def test_to_bytes(self):
     problem = middleware.Problem()
     assert problem.to_bytes(
     ) == b'{"type":"about:blank","title":"Internal Server Error","status":500}'  # noqa