Пример #1
0
    def test_call_no_exception(self, client):
        async def app(scope, receive, send):
            resp = Response(b"", status_code=204)
            return await resp(scope, receive, send)

        app = middleware.ProblemMiddleware(app)
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get('/')

        assert response.status_code == 204
        assert response.content.decode() == ''
Пример #2
0
    def test_call_problem_exception(self):
        async def app(scope, receive, send):
            raise middleware.Problem()

        app = middleware.ProblemMiddleware(app)
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get('/')

        assert response.status_code == 500
        assert response.headers['Content-Type'] == 'application/problem+json'
        assert response.json() == {
            'status': 500,
            'title': 'Internal Server Error',
            'type': 'about:blank',
        }
Пример #3
0
    def test_call_http_exception(self, client):
        async def app(scope, receive, send):
            raise HTTPException(404, 'test error')

        app = middleware.ProblemMiddleware(app)
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get('/')

        assert response.status_code == 404
        assert response.headers['Content-Type'] == 'application/problem+json'
        assert response.json() == {
            'status': 404,
            'title': 'Not Found',
            'type': 'about:blank',
            'detail': 'test error',
        }
Пример #4
0
    def test_call_exception(self, client):
        async def app(scope, receive, send):
            raise ValueError('test error')

        app = middleware.ProblemMiddleware(app)
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get('/')

        assert response.status_code == 500
        assert response.headers['Content-Type'] == 'application/problem+json'
        assert response.json() == {
            'status': 500,
            'title': 'Unexpected Server Error',
            'type': 'about:blank',
            'detail': 'test error',
            'exc_type': 'ValueError',
        }
Пример #5
0
    def test_call_request_validation_error(self, client):
        async def app(scope, receive, send):
            raise RequestValidationError(
                errors=[ErrorWrapper(ValueError('foo'), 'bar')], )

        app = middleware.ProblemMiddleware(app)
        client = TestClient(app, raise_server_exceptions=False)
        response = client.get('/')

        assert response.status_code == 400
        assert response.headers['Content-Type'] == 'application/problem+json'
        assert response.json() == {
            'status': 400,
            'title': 'Validation Error',
            'type': 'about:blank',
            'detail': 'One or more user-provided parameters are invalid',
            'errors': [
                {
                    'loc': ['bar'],
                    'msg': 'foo',
                    'type': 'value_error',
                },
            ],
        }
Пример #6
0
    def test_init(self):
        app = FastAPI()
        pm = middleware.ProblemMiddleware(app, debug=True)

        assert pm.debug is True
        assert pm.app == app