How to use the starlette.status function in starlette

To help you get started, we’ve selected a few starlette 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 frankie567 / fastapi-users / tests / test_router.py View on Github external
def test_empty_body(self, test_app_client: TestClient, event_handler):
        response = test_app_client.post("/register", json={})
        assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
        assert event_handler.called is False
github encode / starlette / tests / test_websockets.py View on Github external
def app(scope):
        async def asgi(receive, send):
            nonlocal close_code
            websocket = WebSocket(scope, receive=receive, send=send)
            await websocket.accept()
            try:
                await websocket.receive_text()
            except WebSocketDisconnect as exc:
                close_code = exc.code

        return asgi

    client = TestClient(app)
    with client.websocket_connect("/") as websocket:
        websocket.close(code=status.WS_1001_GOING_AWAY)
    assert close_code == status.WS_1001_GOING_AWAY
github frankie567 / fastapi-users / tests / test_router.py View on Github external
def test_superuser(
        self,
        mocker,
        mock_user_db,
        test_app_client: TestClient,
        user: BaseUserDB,
        superuser: BaseUserDB,
    ):
        mocker.spy(mock_user_db, "delete")

        response = test_app_client.delete(
            f"/{user.id}", headers={"Authorization": f"Bearer {superuser.id}"}
        )
        assert response.status_code == status.HTTP_204_NO_CONTENT
        assert response.json() is None
        assert mock_user_db.delete.called is True

        deleted_user = mock_user_db.delete.call_args[0][0]
        assert deleted_user.id == user.id
github identixone / fastapi_contrib / tests / test_exceptions.py View on Github external
def test_unauthorized_exception():
    detail = "I don't know who you are, stranger."
    with pytest.raises(UnauthorizedError) as excinfo:
        raise UnauthorizedError(
            fields=[{"field": "because of this."}],
            detail=detail
        )

    exc = excinfo.value
    assert exc.error_code == status.HTTP_401_UNAUTHORIZED
    assert exc.status_code == status.HTTP_401_UNAUTHORIZED
    assert exc.detail == detail
    assert exc.fields == [{"field": "because of this."}]

    error_code = 444
    with pytest.raises(UnauthorizedError) as excinfo:
        raise UnauthorizedError(
            fields=[{"field": "because of this."}],
            detail=detail,
            error_code=error_code
        )

    exc = excinfo.value
    assert exc.error_code == error_code
    assert exc.status_code == status.HTTP_401_UNAUTHORIZED
    assert exc.detail == detail
    assert exc.fields == [{"field": "because of this."}]
github frankie567 / fastapi-users / tests / test_fastapi_users.py View on Github external
def test_routes_exist(self, test_app_client: TestClient):
        response = test_app_client.post("/users/register")
        assert response.status_code != status.HTTP_404_NOT_FOUND

        response = test_app_client.post("/users/login")
        assert response.status_code != status.HTTP_404_NOT_FOUND

        response = test_app_client.post("/users/forgot-password")
        assert response.status_code != status.HTTP_404_NOT_FOUND

        response = test_app_client.post("/users/reset-password")
        assert response.status_code != status.HTTP_404_NOT_FOUND

        response = test_app_client.get("/users")
        assert response.status_code != status.HTTP_404_NOT_FOUND

        response = test_app_client.get("/users/aaa")
        assert response.status_code != status.HTTP_404_NOT_FOUND
github frankie567 / fastapi-users / tests / test_router.py View on Github external
def test_regular_user(self, test_app_client: TestClient, user: BaseUserDB):
        response = test_app_client.get(
            "/000", headers={"Authorization": f"Bearer {user.id}"}
        )
        assert response.status_code == status.HTTP_403_FORBIDDEN
github ethe / pygraphy / pygraphy / view.py View on Github external
status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
            )

        try:
            query = data["query"]
            variables = data.get("variables")
        except KeyError:
            return PlainTextResponse(
                "No GraphQL query found in the request",
                status_code=status.HTTP_400_BAD_REQUEST,
            )

        result = await self.execute(
            query, variables=variables, request=request
        )
        status_code = status.HTTP_200_OK
        return Response(
            json.dumps(result, cls=GraphQLEncoder),
            status_code=status_code,
            media_type='application/json'
        )
github frankie567 / fastapi-users / fastapi_users / authentication / __init__.py View on Github external
def _get_credentials_exception(
        self, status_code: int = status.HTTP_401_UNAUTHORIZED
    ) -> HTTPException:
        return HTTPException(status_code=status_code)
github nsidnev / fastapi-realworld-example-app / app / api / routes / articles / articles_common.py View on Github external
if article.favorited:
        await articles_repo.remove_article_from_favorites(article=article, user=user)

        return ArticleInResponse(
            article=ArticleForResponse.from_orm(
                article.copy(
                    update={
                        "favorited": False,
                        "favorites_count": article.favorites_count - 1,
                    }
                )
            )
        )

    raise HTTPException(
        status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_IS_NOT_FAVORITED
    )
github strawberry-graphql / strawberry / strawberry / asgi / http.py View on Github external
async def get_http_response(request: Request, execute: typing.Callable) -> Response:
    if request.method == "GET":
        html = get_playground_html(str(request.url))
        return HTMLResponse(html)

    if request.method == "POST":
        content_type = request.headers.get("Content-Type", "")

        if "application/json" in content_type:
            data = await request.json()
        else:
            return PlainTextResponse(
                "Unsupported Media Type",
                status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
            )
    else:
        return PlainTextResponse(
            "Method Not Allowed", status_code=status.HTTP_405_METHOD_NOT_ALLOWED
        )

    try:
        query = data["query"]
        variables = data.get("variables")
        operation_name = data.get("operationName")
    except KeyError:
        return PlainTextResponse(
            "No GraphQL query found in the request",
            status_code=status.HTTP_400_BAD_REQUEST,
        )