本文整理汇总了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)
示例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))
示例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))
示例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"}}
示例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()
示例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")
示例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")
示例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()
示例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
示例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
示例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,
# }
# ]
示例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,
}
]
示例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,
}
]
示例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,
}
]
示例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,
}
],
}