當前位置: 首頁>>代碼示例>>Python>>正文


Python testclient.TestClient方法代碼示例

本文整理匯總了Python中starlette.testclient.TestClient方法的典型用法代碼示例。如果您正苦於以下問題:Python testclient.TestClient方法的具體用法?Python testclient.TestClient怎麽用?Python testclient.TestClient使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在starlette.testclient的用法示例。


在下文中一共展示了testclient.TestClient方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: call_asgi

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def call_asgi(
        self,
        app: Any = None,
        base_url: Optional[str] = "http://testserver",
        headers: Optional[Dict[str, str]] = None,
        **kwargs: Any,
    ) -> requests.Response:
        application = app or self.app
        if application is None:
            raise RuntimeError(
                "ASGI application instance is required. "
                "Please, set `app` argument in the schema constructor or pass it to `call_asgi`"
            )
        client = ASGIClient(application)

        return self.call(base_url=base_url, session=client, headers=headers, **kwargs) 
開發者ID:kiwicom,項目名稱:schemathesis,代碼行數:18,代碼來源:models.py

示例2: test_webgear_class

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_webgear_class(source, stabilize, colorspace, time_delay):
    """
    Test for various WebGear API parameters
    """
    try:
        web = WebGear(
            source=source,
            stabilize=stabilize,
            colorspace=colorspace,
            time_delay=time_delay,
            logging=True,
        )
        client = TestClient(web(), raise_server_exceptions=True)
        response = client.get("/")
        assert response.status_code == 200
        response_404 = client.get("/test")
        assert response_404.status_code == 404
        web.shutdown()
    except Exception as e:
        pytest.fail(str(e)) 
開發者ID:abhiTronix,項目名稱:vidgear,代碼行數:22,代碼來源:test_webgear.py

示例3: test_webgear_options

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_webgear_options(options):
    """
    Test for various WebGear API internal options
    """
    try:
        web = WebGear(source=return_testvideo_path(), logging=True, **options)
        client = TestClient(web(), raise_server_exceptions=True)
        response = client.get("/")
        assert response.status_code == 200
        response_video = client.get("/video")
        assert response_video.status_code == 200
        web.shutdown()
    except Exception as e:
        if isinstance(e, AssertionError):
            logger.exception(str(e))
        elif isinstance(e, requests.exceptions.Timeout):
            logger.exceptions(str(e))
        else:
            pytest.fail(str(e)) 
開發者ID:abhiTronix,項目名稱:vidgear,代碼行數:21,代碼來源:test_webgear.py

示例4: test_custom_root_value_is_passed_to_subscription_resolvers

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_root_value_is_passed_to_subscription_resolvers(schema):
    app = GraphQL(schema, root_value={"test": "TEST-ROOT"})
    client = TestClient(app)
    with client.websocket_connect("/", "graphql-ws") as ws:
        ws.send_json({"type": GQL_CONNECTION_INIT})
        ws.send_json(
            {
                "type": GQL_START,
                "id": "test1",
                "payload": {"query": "subscription { testRoot }"},
            }
        )
        response = ws.receive_json()
        assert response["type"] == GQL_CONNECTION_ACK
        response = ws.receive_json()
        assert response["type"] == GQL_DATA
        assert response["payload"] == {"data": {"testRoot": "TEST-ROOT"}} 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:19,代碼來源:test_configuration.py

示例5: test_custom_root_value_function_is_called_by_subscription

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_root_value_function_is_called_by_subscription(schema):
    get_root_value = Mock(return_value=True)
    app = GraphQL(schema, root_value=get_root_value)
    client = TestClient(app)
    with client.websocket_connect("/", "graphql-ws") as ws:
        ws.send_json({"type": GQL_CONNECTION_INIT})
        ws.send_json(
            {
                "type": GQL_START,
                "id": "test1",
                "payload": {"query": "subscription { ping }"},
            }
        )
        response = ws.receive_json()
        assert response["type"] == GQL_CONNECTION_ACK
        response = ws.receive_json()
        assert response["type"] == GQL_DATA
        get_root_value.assert_called_once() 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:20,代碼來源:test_configuration.py

示例6: test_custom_logger_is_used_to_log_subscription_source_error

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_logger_is_used_to_log_subscription_source_error(schema, mocker):
    logging_mock = mocker.patch("ariadne.logger.logging")
    app = GraphQL(schema, logger="custom")
    client = TestClient(app)
    with client.websocket_connect("/", "graphql-ws") as ws:
        ws.send_json({"type": GQL_CONNECTION_INIT})
        ws.send_json(
            {
                "type": GQL_START,
                "id": "test1",
                "payload": {"query": "subscription { sourceError }"},
            }
        )
        response = ws.receive_json()
        assert response["type"] == GQL_CONNECTION_ACK
        response = ws.receive_json()
        assert response["type"] == GQL_DATA
        logging_mock.getLogger.assert_called_once_with("custom") 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:20,代碼來源:test_configuration.py

示例7: test_custom_logger_is_used_to_log_subscription_resolver_error

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_logger_is_used_to_log_subscription_resolver_error(schema, mocker):
    logging_mock = mocker.patch("ariadne.logger.logging")
    app = GraphQL(schema, logger="custom")
    client = TestClient(app)
    with client.websocket_connect("/", "graphql-ws") as ws:
        ws.send_json({"type": GQL_CONNECTION_INIT})
        ws.send_json(
            {
                "type": GQL_START,
                "id": "test1",
                "payload": {"query": "subscription { resolverError }"},
            }
        )
        response = ws.receive_json()
        assert response["type"] == GQL_CONNECTION_ACK
        response = ws.receive_json()
        assert response["type"] == GQL_DATA
        logging_mock.getLogger.assert_called_once_with("custom") 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:20,代碼來源:test_configuration.py

示例8: test_custom_error_formatter_is_used_to_format_subscription_resolver_error

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_error_formatter_is_used_to_format_subscription_resolver_error(schema):
    error_formatter = Mock(return_value=True)
    app = GraphQL(schema, error_formatter=error_formatter)
    client = TestClient(app)
    with client.websocket_connect("/", "graphql-ws") as ws:
        ws.send_json({"type": GQL_CONNECTION_INIT})
        ws.send_json(
            {
                "type": GQL_START,
                "id": "test1",
                "payload": {"query": "subscription { resolverError }"},
            }
        )
        response = ws.receive_json()
        assert response["type"] == GQL_CONNECTION_ACK
        response = ws.receive_json()
        assert response["type"] == GQL_DATA
        assert response["id"] == "test1"
        error_formatter.assert_called_once() 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:21,代碼來源:test_configuration.py

示例9: test_custom_pagination_correct_filters

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_pagination_correct_filters():

    class CustomPagination2(Pagination):
        default_offset = 90
        default_limit = 1
        max_offset = 100
        max_limit = 2000

    @app.get("/hallo3/pagination3/")
    async def hallo3_pagination3(pagination: CustomPagination2 = Depends()):
        resp = await pagination.paginate(serializer_class=TestSerializer)
        return resp

    with TestClient(app) as client:
        response = client.get("/hallo3/pagination3/?limit=1001")
        assert response.status_code == 200

        response = client.get("/hallo3/pagination3/?offset=99")
        assert response.status_code == 200

        response = client.get("/hallo3/pagination3/?offset=99&limit=1001")
        assert response.status_code == 200 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:24,代碼來源:test_pagination.py

示例10: test_custom_pagination_invalid_offset_and_limit

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_custom_pagination_invalid_offset_and_limit():

    class CustomPagination(Pagination):
        default_offset = 90
        default_limit = 1
        max_offset = 100
        max_limit = 2000

    @app.get("/hallo2/pagination2/")
    async def hallo2_pagination2(pagination: CustomPagination = Depends()):
        resp = await pagination.paginate(serializer_class=TestSerializer)
        return resp

    with TestClient(app) as client:
        response = client.get("/hallo2/pagination2/?limit=2001")
        assert response.status_code == 422

        response = client.get("/hallo2/pagination2/?offset=101")
        assert response.status_code == 422

        response = client.get("/hallo2/pagination2/?offset=101&limit=2001")
        assert response.status_code == 422 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:24,代碼來源:test_pagination.py

示例11: pydantic_exception_invalid_query

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def pydantic_exception_invalid_query(q: int = Query(...)):
    return {"q": q}


# def test_exception_handler_invalid_query():
#     with TestClient(app) as client:
#         response = client.get(
#             "/pydantic/exception/invalidquery/", params={"q": "$"}
#         )
#         assert response.status_code == 400
#         response = response.json()
#         assert response["error_codes"] == [400]
#         assert response["message"] == "Validation error."
#         assert response["fields"] == [
#             {
#                 "name": "q",
#                 "message": "Value is not a valid integer.",
#                 "error_code": 400,
#             }
#         ] 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:22,代碼來源:test_exception_handlers.py

示例12: test_exception_handler_when_regex_invalid

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_exception_handler_when_regex_invalid():
    with TestClient(app) as client:
        response = client.post(
            "/pydantic/exception/regexp/", json={"name": "$$$"}
        )
        assert response.status_code == 400
        response = response.json()
        assert response["error_codes"] == [400]
        assert response["message"] == "Validation error."
        assert response["fields"] == [
            {
                "message": "Provided value doesn't match valid format.",
                "name": "name",
                "error_code": 400,
            }
        ] 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:18,代碼來源:test_exception_handlers.py

示例13: test_exception_handler_when_choice_invalid

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_exception_handler_when_choice_invalid():
    with TestClient(app) as client:
        response = client.post(
            "/pydantic/exception/choice/", json={"kind": "d"}
        )
        assert response.status_code == 400
        response = response.json()
        assert response["error_codes"] == [400]
        assert response["message"] == "Validation error."
        assert response["fields"] == [
            {
                "message": "One or more values provided are not valid.",
                "name": "kind",
                "error_code": 400,
            }
        ] 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:18,代碼來源:test_exception_handlers.py

示例14: test_exception_handler_when_one_of_multi_choice_invalid

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_exception_handler_when_one_of_multi_choice_invalid():
    with TestClient(app) as client:
        response = client.post(
            "/pydantic/exception/multiplechoice/", json={"multi": ["d", "a"]}
        )
        assert response.status_code == 400
        response = response.json()
        assert response["error_codes"] == [400]
        assert response["message"] == "Validation error."
        assert response["fields"] == [
            {
                "message": "One or more values provided are not valid.",
                "name": "multi",
                "error_code": 400,
            }
        ] 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:18,代碼來源:test_exception_handlers.py

示例15: test_exception_handler_with_list_str_instead_of_ints

# 需要導入模塊: from starlette import testclient [as 別名]
# 或者: from starlette.testclient import TestClient [as 別名]
def test_exception_handler_with_list_str_instead_of_ints():
    with TestClient(app) as client:
        response = client.post(
            "/pydantic/exception/multipleint/", json={"integers": ["d"]}
        )
        assert response.status_code == 400
        response = response.json()
        assert response == {
            "error_codes": [400],
            "message": "Validation error.",
            "fields": [
                {
                    "name": "integers",
                    "message": "Value is not a valid integer.",
                    "error_code": 400,
                }
            ],
        } 
開發者ID:identixone,項目名稱:fastapi_contrib,代碼行數:20,代碼來源:test_exception_handlers.py


注:本文中的starlette.testclient.TestClient方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。