Test Client - TestClient

You can use the TestClient class to test FastAPI applications without creating an actual HTTP and socket connection, just communicating directly with the FastAPI code.

Read more about it in the FastAPI docs for Testing.

You can import it directly from fastapi.testclient:

  1. from fastapi.testclient import TestClient

fastapi.testclient.TestClient

  1. TestClient(
  2. app,
  3. base_url="http://testserver",
  4. raise_server_exceptions=True,
  5. root_path="",
  6. backend="asyncio",
  7. backend_options=None,
  8. cookies=None,
  9. headers=None,
  10. )

Bases: Client

PARAMETERDESCRIPTION
app

TYPE: ASGIApp

base_url

TYPE: str DEFAULT: http://testserver

raise_server_exceptions

TYPE: bool DEFAULT: True

root_path

TYPE: str DEFAULT: ‘’

backend

TYPE: str DEFAULT: ‘asyncio’

backend_options

TYPE: Optional[Dict[str, Any]] DEFAULT: None

cookies

TYPE: CookieTypes DEFAULT: None

headers

TYPE: Dict[str, str] DEFAULT: None

Source code in starlette/testclient.py

  1. 367
  2. 368
  3. 369
  4. 370
  5. 371
  6. 372
  7. 373
  8. 374
  9. 375
  10. 376
  11. 377
  12. 378
  13. 379
  14. 380
  15. 381
  16. 382
  17. 383
  18. 384
  19. 385
  20. 386
  21. 387
  22. 388
  23. 389
  24. 390
  25. 391
  26. 392
  27. 393
  28. 394
  29. 395
  30. 396
  31. 397
  32. 398
  33. 399
  34. 400
  35. 401
  36. 402
  37. 403
  38. 404
  39. 405
  40. 406
  1. def init(
  2. self,
  3. app: ASGIApp,
  4. baseurl: str = http://testserver,
  5. raiseserver_exceptions: bool = True,
  6. root_path: str = “”,
  7. backend: str = asyncio,
  8. backend_options: typing.Optional[typing.Dict[str, typing.Any]] = None,
  9. cookies: httpx._client.CookieTypes = None,
  10. headers: typing.Dict[str, str] = None,
  11. ) -> None:
  12. self.async_backend = _AsyncBackend(
  13. backend=backend, backend_options=backend_options or {}
  14. )
  15. if _is_asgi3(app):
  16. app = typing.cast(ASGI3App, app)
  17. asgi_app = app
  18. else:
  19. app = typing.cast(ASGI2App, app) # type: ignore[assignment]
  20. asgi_app = _WrapASGI2(app) # type: ignore[arg-type]
  21. self.app = asgi_app
  22. self.app_state: typing.Dict[str, typing.Any] = {}
  23. transport = _TestClientTransport(
  24. self.app,
  25. portal_factory=self._portal_factory,
  26. raise_server_exceptions=raise_server_exceptions,
  27. root_path=root_path,
  28. app_state=self.app_state,
  29. )
  30. if headers is None:
  31. headers = {}
  32. headers.setdefault(user-agent, testclient)
  33. super().__init(
  34. app=self.app,
  35. base_url=base_url,
  36. headers=headers,
  37. transport=transport,
  38. follow_redirects=True,
  39. cookies=cookies,
  40. )

headers property writable

  1. headers

HTTP headers to include when sending requests.

follow_redirects instance-attribute

  1. follow_redirects = follow_redirects

max_redirects instance-attribute

  1. max_redirects = max_redirects

is_closed property

  1. is_closed

Check if the client being closed

trust_env property

  1. trust_env

timeout property writable

  1. timeout

event_hooks property writable

  1. event_hooks

auth property writable

  1. auth

Authentication class used when none is passed at the request-level.

See also Authentication.

base_url property writable

  1. base_url

Base URL to use when sending requests with relative URLs.

cookies property writable

  1. cookies

Cookie values to include when sending requests.

params property writable

  1. params

Query parameters to include in the URL when sending requests.

task instance-attribute

  1. task

portal class-attribute instance-attribute

  1. portal = None

async_backend instance-attribute

  1. async_backend = _AsyncBackend(
  2. backend=backend, backend_options=backend_options or {}
  3. )

app instance-attribute

  1. app = asgi_app

app_state instance-attribute

  1. app_state = {}

build_request

  1. build_request(
  2. method,
  3. url,
  4. *,
  5. content=None,
  6. data=None,
  7. files=None,
  8. json=None,
  9. params=None,
  10. headers=None,
  11. cookies=None,
  12. timeout=USE_CLIENT_DEFAULT,
  13. extensions=None
  14. )

Build and return a request instance.

  • The params, headers and cookies arguments are merged with any values set on the client.
  • The url argument is merged with any base_url set on the client.

See also: Request instances

PARAMETERDESCRIPTION
method

TYPE: str

url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Optional[Any] DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[RequestExtensions] DEFAULT: None

Source code in httpx/_client.py

  1. 320
  2. 321
  3. 322
  4. 323
  5. 324
  6. 325
  7. 326
  8. 327
  9. 328
  10. 329
  11. 330
  12. 331
  13. 332
  14. 333
  15. 334
  16. 335
  17. 336
  18. 337
  19. 338
  20. 339
  21. 340
  22. 341
  23. 342
  24. 343
  25. 344
  26. 345
  27. 346
  28. 347
  29. 348
  30. 349
  31. 350
  32. 351
  33. 352
  34. 353
  35. 354
  36. 355
  37. 356
  38. 357
  39. 358
  40. 359
  41. 360
  42. 361
  43. 362
  44. 363
  45. 364
  46. 365
  47. 366
  48. 367
  49. 368
  50. 369
  1. def build_request(
  2. self,
  3. method: str,
  4. url: URLTypes,
  5. ,
  6. content: typing.Optional[RequestContent] = None,
  7. data: typing.Optional[RequestData] = None,
  8. files: typing.Optional[RequestFiles] = None,
  9. json: typing.Optional[typing.Any] = None,
  10. params: typing.Optional[QueryParamTypes] = None,
  11. headers: typing.Optional[HeaderTypes] = None,
  12. cookies: typing.Optional[CookieTypes] = None,
  13. timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
  14. extensions: typing.Optional[RequestExtensions] = None,
  15. ) -> Request:
  16. “””
  17. Build and return a request instance.
  18. The params, headers and cookies arguments
  19. are merged with any values set on the client.
  20. The url argument is merged with any base_url set on the client.
  21. See also: [Request instances][0]
  22. [0]: /advanced/#request-instances
  23. “””
  24. url = self._merge_url(url)
  25. headers = self._merge_headers(headers)
  26. cookies = self._merge_cookies(cookies)
  27. params = self._merge_queryparams(params)
  28. extensions = {} if extensions is None else extensions
  29. if timeout not in extensions:
  30. timeout = (
  31. self.timeout
  32. if isinstance(timeout, UseClientDefault)
  33. else Timeout(timeout)
  34. )
  35. extensions = dict(*extensions, timeout=timeout.as_dict())
  36. return Request(
  37. method,
  38. url,
  39. content=content,
  40. data=data,
  41. files=files,
  42. json=json,
  43. params=params,
  44. headers=headers,
  45. cookies=cookies,
  46. extensions=extensions,
  47. )

stream

  1. stream(
  2. method,
  3. url,
  4. *,
  5. content=None,
  6. data=None,
  7. files=None,
  8. json=None,
  9. params=None,
  10. headers=None,
  11. cookies=None,
  12. auth=USE_CLIENT_DEFAULT,
  13. follow_redirects=USE_CLIENT_DEFAULT,
  14. timeout=USE_CLIENT_DEFAULT,
  15. extensions=None
  16. )

Alternative to httpx.request() that streams the response body instead of loading it into memory at once.

Parameters: See httpx.request.

See also: Streaming Responses

PARAMETERDESCRIPTION
method

TYPE: str

url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Optional[Any] DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault, None] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Union[bool, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[RequestExtensions] DEFAULT: None

YIELDSDESCRIPTION
Response

Source code in httpx/_client.py

  1. 816
  2. 817
  3. 818
  4. 819
  5. 820
  6. 821
  7. 822
  8. 823
  9. 824
  10. 825
  11. 826
  12. 827
  13. 828
  14. 829
  15. 830
  16. 831
  17. 832
  18. 833
  19. 834
  20. 835
  21. 836
  22. 837
  23. 838
  24. 839
  25. 840
  26. 841
  27. 842
  28. 843
  29. 844
  30. 845
  31. 846
  32. 847
  33. 848
  34. 849
  35. 850
  36. 851
  37. 852
  38. 853
  39. 854
  40. 855
  41. 856
  42. 857
  43. 858
  44. 859
  45. 860
  46. 861
  47. 862
  48. 863
  49. 864
  50. 865
  51. 866
  1. @contextmanager
  2. def stream(
  3. self,
  4. method: str,
  5. url: URLTypes,
  6. ,
  7. content: typing.Optional[RequestContent] = None,
  8. data: typing.Optional[RequestData] = None,
  9. files: typing.Optional[RequestFiles] = None,
  10. json: typing.Optional[typing.Any] = None,
  11. params: typing.Optional[QueryParamTypes] = None,
  12. headers: typing.Optional[HeaderTypes] = None,
  13. cookies: typing.Optional[CookieTypes] = None,
  14. auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT,
  15. follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,
  16. timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
  17. extensions: typing.Optional[RequestExtensions] = None,
  18. ) -> typing.Iterator[Response]:
  19. “””
  20. Alternative to httpx.request() that streams the response body
  21. instead of loading it into memory at once.
  22. *Parameters: See httpx.request.
  23. See also: [Streaming Responses][0]
  24. [0]: /quickstart#streaming-responses
  25. “””
  26. request = self.build_request(
  27. method=method,
  28. url=url,
  29. content=content,
  30. data=data,
  31. files=files,
  32. json=json,
  33. params=params,
  34. headers=headers,
  35. cookies=cookies,
  36. timeout=timeout,
  37. extensions=extensions,
  38. )
  39. response = self.send(
  40. request=request,
  41. auth=auth,
  42. follow_redirects=follow_redirects,
  43. stream=True,
  44. )
  45. try:
  46. yield response
  47. finally:
  48. response.close()

send

  1. send(
  2. request,
  3. *,
  4. stream=False,
  5. auth=USE_CLIENT_DEFAULT,
  6. follow_redirects=USE_CLIENT_DEFAULT
  7. )

Send a request.

The request is sent as-is, unmodified.

Typically you’ll want to build one with Client.build_request() so that any client-level configuration is merged into the request, but passing an explicit httpx.Request() is supported as well.

See also: Request instances

PARAMETERDESCRIPTION
request

TYPE: Request

stream

TYPE: bool DEFAULT: False

auth

TYPE: Union[AuthTypes, UseClientDefault, None] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Union[bool, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

Source code in httpx/_client.py

  1. 868
  2. 869
  3. 870
  4. 871
  5. 872
  6. 873
  7. 874
  8. 875
  9. 876
  10. 877
  11. 878
  12. 879
  13. 880
  14. 881
  15. 882
  16. 883
  17. 884
  18. 885
  19. 886
  20. 887
  21. 888
  22. 889
  23. 890
  24. 891
  25. 892
  26. 893
  27. 894
  28. 895
  29. 896
  30. 897
  31. 898
  32. 899
  33. 900
  34. 901
  35. 902
  36. 903
  37. 904
  38. 905
  39. 906
  40. 907
  41. 908
  42. 909
  43. 910
  44. 911
  45. 912
  46. 913
  47. 914
  48. 915
  1. def send(
  2. self,
  3. request: Request,
  4. *,
  5. stream: bool = False,
  6. auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT,
  7. follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,
  8. ) -> Response:
  9. “””
  10. Send a request.
  11. The request is sent as-is, unmodified.
  12. Typically youll want to build one with Client.build_request()
  13. so that any client-level configuration is merged into the request,
  14. but passing an explicit httpx.Request() is supported as well.
  15. See also: [Request instances][0]
  16. [0]: /advanced/#request-instances
  17. “””
  18. if self._state == ClientState.CLOSED:
  19. raise RuntimeError(Cannot send a request, as the client has been closed.”)
  20. self._state = ClientState.OPENED
  21. follow_redirects = (
  22. self.follow_redirects
  23. if isinstance(follow_redirects, UseClientDefault)
  24. else follow_redirects
  25. )
  26. auth = self._build_request_auth(request, auth)
  27. response = self._send_handling_auth(
  28. request,
  29. auth=auth,
  30. follow_redirects=follow_redirects,
  31. history=[],
  32. )
  33. try:
  34. if not stream:
  35. response.read()
  36. return response
  37. except BaseException as exc:
  38. response.close()
  39. raise exc

close

  1. close()

Close transport and proxies.

Source code in httpx/_client.py

  1. 1251
  2. 1252
  3. 1253
  4. 1254
  5. 1255
  6. 1256
  7. 1257
  8. 1258
  9. 1259
  10. 1260
  11. 1261
  1. def close(self) -> None:
  2. “””
  3. Close transport and proxies.
  4. “””
  5. if self._state != ClientState.CLOSED:
  6. self._state = ClientState.CLOSED
  7. self._transport.close()
  8. for transport in self._mounts.values():
  9. if transport is not None:
  10. transport.close()

request

  1. request(
  2. method,
  3. url,
  4. *,
  5. content=None,
  6. data=None,
  7. files=None,
  8. json=None,
  9. params=None,
  10. headers=None,
  11. cookies=None,
  12. auth=httpx._client.USE_CLIENT_DEFAULT,
  13. follow_redirects=None,
  14. allow_redirects=None,
  15. timeout=httpx._client.USE_CLIENT_DEFAULT,
  16. extensions=None
  17. )
PARAMETERDESCRIPTION
method

TYPE: str

url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[_RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Any DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 441
  2. 442
  3. 443
  4. 444
  5. 445
  6. 446
  7. 447
  8. 448
  9. 449
  10. 450
  11. 451
  12. 452
  13. 453
  14. 454
  15. 455
  16. 456
  17. 457
  18. 458
  19. 459
  20. 460
  21. 461
  22. 462
  23. 463
  24. 464
  25. 465
  26. 466
  27. 467
  28. 468
  29. 469
  30. 470
  31. 471
  32. 472
  33. 473
  34. 474
  35. 475
  36. 476
  37. 477
  38. 478
  39. 479
  1. def request( # type: ignore[override]
  2. self,
  3. method: str,
  4. url: httpx._types.URLTypes,
  5. *,
  6. content: typing.Optional[httpx._types.RequestContent] = None,
  7. data: typing.Optional[_RequestData] = None,
  8. files: typing.Optional[httpx._types.RequestFiles] = None,
  9. json: typing.Any = None,
  10. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  11. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  12. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  13. auth: typing.Union[
  14. httpx._types.AuthTypes, httpx._client.UseClientDefault
  15. ] = httpx._client.USE_CLIENT_DEFAULT,
  16. follow_redirects: typing.Optional[bool] = None,
  17. allow_redirects: typing.Optional[bool] = None,
  18. timeout: typing.Union[
  19. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  20. ] = httpx._client.USE_CLIENT_DEFAULT,
  21. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  22. ) -> httpx.Response:
  23. url = self.base_url.join(url)
  24. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  25. return super().request(
  26. method,
  27. url,
  28. content=content,
  29. data=data, # type: ignore[arg-type]
  30. files=files,
  31. json=json,
  32. params=params,
  33. headers=headers,
  34. cookies=cookies,
  35. auth=auth,
  36. follow_redirects=redirect,
  37. timeout=timeout,
  38. extensions=extensions,
  39. )

get

  1. get(
  2. url,
  3. *,
  4. params=None,
  5. headers=None,
  6. cookies=None,
  7. auth=httpx._client.USE_CLIENT_DEFAULT,
  8. follow_redirects=None,
  9. allow_redirects=None,
  10. timeout=httpx._client.USE_CLIENT_DEFAULT,
  11. extensions=None
  12. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 481
  2. 482
  3. 483
  4. 484
  5. 485
  6. 486
  7. 487
  8. 488
  9. 489
  10. 490
  11. 491
  12. 492
  13. 493
  14. 494
  15. 495
  16. 496
  17. 497
  18. 498
  19. 499
  20. 500
  21. 501
  22. 502
  23. 503
  24. 504
  25. 505
  26. 506
  27. 507
  28. 508
  1. def get( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  6. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  7. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  8. auth: typing.Union[
  9. httpx._types.AuthTypes, httpx._client.UseClientDefault
  10. ] = httpx._client.USE_CLIENT_DEFAULT,
  11. follow_redirects: typing.Optional[bool] = None,
  12. allow_redirects: typing.Optional[bool] = None,
  13. timeout: typing.Union[
  14. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  15. ] = httpx._client.USE_CLIENT_DEFAULT,
  16. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  17. ) -> httpx.Response:
  18. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  19. return super().get(
  20. url,
  21. params=params,
  22. headers=headers,
  23. cookies=cookies,
  24. auth=auth,
  25. follow_redirects=redirect,
  26. timeout=timeout,
  27. extensions=extensions,
  28. )

options

  1. options(
  2. url,
  3. *,
  4. params=None,
  5. headers=None,
  6. cookies=None,
  7. auth=httpx._client.USE_CLIENT_DEFAULT,
  8. follow_redirects=None,
  9. allow_redirects=None,
  10. timeout=httpx._client.USE_CLIENT_DEFAULT,
  11. extensions=None
  12. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 510
  2. 511
  3. 512
  4. 513
  5. 514
  6. 515
  7. 516
  8. 517
  9. 518
  10. 519
  11. 520
  12. 521
  13. 522
  14. 523
  15. 524
  16. 525
  17. 526
  18. 527
  19. 528
  20. 529
  21. 530
  22. 531
  23. 532
  24. 533
  25. 534
  26. 535
  27. 536
  28. 537
  1. def options( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  6. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  7. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  8. auth: typing.Union[
  9. httpx._types.AuthTypes, httpx._client.UseClientDefault
  10. ] = httpx._client.USE_CLIENT_DEFAULT,
  11. follow_redirects: typing.Optional[bool] = None,
  12. allow_redirects: typing.Optional[bool] = None,
  13. timeout: typing.Union[
  14. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  15. ] = httpx._client.USE_CLIENT_DEFAULT,
  16. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  17. ) -> httpx.Response:
  18. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  19. return super().options(
  20. url,
  21. params=params,
  22. headers=headers,
  23. cookies=cookies,
  24. auth=auth,
  25. follow_redirects=redirect,
  26. timeout=timeout,
  27. extensions=extensions,
  28. )

head

  1. head(
  2. url,
  3. *,
  4. params=None,
  5. headers=None,
  6. cookies=None,
  7. auth=httpx._client.USE_CLIENT_DEFAULT,
  8. follow_redirects=None,
  9. allow_redirects=None,
  10. timeout=httpx._client.USE_CLIENT_DEFAULT,
  11. extensions=None
  12. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 539
  2. 540
  3. 541
  4. 542
  5. 543
  6. 544
  7. 545
  8. 546
  9. 547
  10. 548
  11. 549
  12. 550
  13. 551
  14. 552
  15. 553
  16. 554
  17. 555
  18. 556
  19. 557
  20. 558
  21. 559
  22. 560
  23. 561
  24. 562
  25. 563
  26. 564
  27. 565
  28. 566
  1. def head( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  6. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  7. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  8. auth: typing.Union[
  9. httpx._types.AuthTypes, httpx._client.UseClientDefault
  10. ] = httpx._client.USE_CLIENT_DEFAULT,
  11. follow_redirects: typing.Optional[bool] = None,
  12. allow_redirects: typing.Optional[bool] = None,
  13. timeout: typing.Union[
  14. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  15. ] = httpx._client.USE_CLIENT_DEFAULT,
  16. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  17. ) -> httpx.Response:
  18. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  19. return super().head(
  20. url,
  21. params=params,
  22. headers=headers,
  23. cookies=cookies,
  24. auth=auth,
  25. follow_redirects=redirect,
  26. timeout=timeout,
  27. extensions=extensions,
  28. )

post

  1. post(
  2. url,
  3. *,
  4. content=None,
  5. data=None,
  6. files=None,
  7. json=None,
  8. params=None,
  9. headers=None,
  10. cookies=None,
  11. auth=httpx._client.USE_CLIENT_DEFAULT,
  12. follow_redirects=None,
  13. allow_redirects=None,
  14. timeout=httpx._client.USE_CLIENT_DEFAULT,
  15. extensions=None
  16. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[_RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Any DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 568
  2. 569
  3. 570
  4. 571
  5. 572
  6. 573
  7. 574
  8. 575
  9. 576
  10. 577
  11. 578
  12. 579
  13. 580
  14. 581
  15. 582
  16. 583
  17. 584
  18. 585
  19. 586
  20. 587
  21. 588
  22. 589
  23. 590
  24. 591
  25. 592
  26. 593
  27. 594
  28. 595
  29. 596
  30. 597
  31. 598
  32. 599
  33. 600
  34. 601
  35. 602
  36. 603
  1. def post( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. content: typing.Optional[httpx._types.RequestContent] = None,
  6. data: typing.Optional[_RequestData] = None,
  7. files: typing.Optional[httpx._types.RequestFiles] = None,
  8. json: typing.Any = None,
  9. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  10. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  11. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  12. auth: typing.Union[
  13. httpx._types.AuthTypes, httpx._client.UseClientDefault
  14. ] = httpx._client.USE_CLIENT_DEFAULT,
  15. follow_redirects: typing.Optional[bool] = None,
  16. allow_redirects: typing.Optional[bool] = None,
  17. timeout: typing.Union[
  18. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  19. ] = httpx._client.USE_CLIENT_DEFAULT,
  20. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  21. ) -> httpx.Response:
  22. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  23. return super().post(
  24. url,
  25. content=content,
  26. data=data, # type: ignore[arg-type]
  27. files=files,
  28. json=json,
  29. params=params,
  30. headers=headers,
  31. cookies=cookies,
  32. auth=auth,
  33. follow_redirects=redirect,
  34. timeout=timeout,
  35. extensions=extensions,
  36. )

put

  1. put(
  2. url,
  3. *,
  4. content=None,
  5. data=None,
  6. files=None,
  7. json=None,
  8. params=None,
  9. headers=None,
  10. cookies=None,
  11. auth=httpx._client.USE_CLIENT_DEFAULT,
  12. follow_redirects=None,
  13. allow_redirects=None,
  14. timeout=httpx._client.USE_CLIENT_DEFAULT,
  15. extensions=None
  16. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[_RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Any DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 605
  2. 606
  3. 607
  4. 608
  5. 609
  6. 610
  7. 611
  8. 612
  9. 613
  10. 614
  11. 615
  12. 616
  13. 617
  14. 618
  15. 619
  16. 620
  17. 621
  18. 622
  19. 623
  20. 624
  21. 625
  22. 626
  23. 627
  24. 628
  25. 629
  26. 630
  27. 631
  28. 632
  29. 633
  30. 634
  31. 635
  32. 636
  33. 637
  34. 638
  35. 639
  36. 640
  1. def put( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. content: typing.Optional[httpx._types.RequestContent] = None,
  6. data: typing.Optional[_RequestData] = None,
  7. files: typing.Optional[httpx._types.RequestFiles] = None,
  8. json: typing.Any = None,
  9. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  10. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  11. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  12. auth: typing.Union[
  13. httpx._types.AuthTypes, httpx._client.UseClientDefault
  14. ] = httpx._client.USE_CLIENT_DEFAULT,
  15. follow_redirects: typing.Optional[bool] = None,
  16. allow_redirects: typing.Optional[bool] = None,
  17. timeout: typing.Union[
  18. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  19. ] = httpx._client.USE_CLIENT_DEFAULT,
  20. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  21. ) -> httpx.Response:
  22. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  23. return super().put(
  24. url,
  25. content=content,
  26. data=data, # type: ignore[arg-type]
  27. files=files,
  28. json=json,
  29. params=params,
  30. headers=headers,
  31. cookies=cookies,
  32. auth=auth,
  33. follow_redirects=redirect,
  34. timeout=timeout,
  35. extensions=extensions,
  36. )

patch

  1. patch(
  2. url,
  3. *,
  4. content=None,
  5. data=None,
  6. files=None,
  7. json=None,
  8. params=None,
  9. headers=None,
  10. cookies=None,
  11. auth=httpx._client.USE_CLIENT_DEFAULT,
  12. follow_redirects=None,
  13. allow_redirects=None,
  14. timeout=httpx._client.USE_CLIENT_DEFAULT,
  15. extensions=None
  16. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

content

TYPE: Optional[RequestContent] DEFAULT: None

data

TYPE: Optional[_RequestData] DEFAULT: None

files

TYPE: Optional[RequestFiles] DEFAULT: None

json

TYPE: Any DEFAULT: None

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 642
  2. 643
  3. 644
  4. 645
  5. 646
  6. 647
  7. 648
  8. 649
  9. 650
  10. 651
  11. 652
  12. 653
  13. 654
  14. 655
  15. 656
  16. 657
  17. 658
  18. 659
  19. 660
  20. 661
  21. 662
  22. 663
  23. 664
  24. 665
  25. 666
  26. 667
  27. 668
  28. 669
  29. 670
  30. 671
  31. 672
  32. 673
  33. 674
  34. 675
  35. 676
  36. 677
  1. def patch( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. content: typing.Optional[httpx._types.RequestContent] = None,
  6. data: typing.Optional[_RequestData] = None,
  7. files: typing.Optional[httpx._types.RequestFiles] = None,
  8. json: typing.Any = None,
  9. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  10. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  11. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  12. auth: typing.Union[
  13. httpx._types.AuthTypes, httpx._client.UseClientDefault
  14. ] = httpx._client.USE_CLIENT_DEFAULT,
  15. follow_redirects: typing.Optional[bool] = None,
  16. allow_redirects: typing.Optional[bool] = None,
  17. timeout: typing.Union[
  18. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  19. ] = httpx._client.USE_CLIENT_DEFAULT,
  20. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  21. ) -> httpx.Response:
  22. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  23. return super().patch(
  24. url,
  25. content=content,
  26. data=data, # type: ignore[arg-type]
  27. files=files,
  28. json=json,
  29. params=params,
  30. headers=headers,
  31. cookies=cookies,
  32. auth=auth,
  33. follow_redirects=redirect,
  34. timeout=timeout,
  35. extensions=extensions,
  36. )

delete

  1. delete(
  2. url,
  3. *,
  4. params=None,
  5. headers=None,
  6. cookies=None,
  7. auth=httpx._client.USE_CLIENT_DEFAULT,
  8. follow_redirects=None,
  9. allow_redirects=None,
  10. timeout=httpx._client.USE_CLIENT_DEFAULT,
  11. extensions=None
  12. )
PARAMETERDESCRIPTION
url

TYPE: URLTypes

params

TYPE: Optional[QueryParamTypes] DEFAULT: None

headers

TYPE: Optional[HeaderTypes] DEFAULT: None

cookies

TYPE: Optional[CookieTypes] DEFAULT: None

auth

TYPE: Union[AuthTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

follow_redirects

TYPE: Optional[bool] DEFAULT: None

allow_redirects

TYPE: Optional[bool] DEFAULT: None

timeout

TYPE: Union[TimeoutTypes, UseClientDefault] DEFAULT: USE_CLIENT_DEFAULT

extensions

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in starlette/testclient.py

  1. 679
  2. 680
  3. 681
  4. 682
  5. 683
  6. 684
  7. 685
  8. 686
  9. 687
  10. 688
  11. 689
  12. 690
  13. 691
  14. 692
  15. 693
  16. 694
  17. 695
  18. 696
  19. 697
  20. 698
  21. 699
  22. 700
  23. 701
  24. 702
  25. 703
  26. 704
  27. 705
  28. 706
  1. def delete( # type: ignore[override]
  2. self,
  3. url: httpx._types.URLTypes,
  4. *,
  5. params: typing.Optional[httpx._types.QueryParamTypes] = None,
  6. headers: typing.Optional[httpx._types.HeaderTypes] = None,
  7. cookies: typing.Optional[httpx._types.CookieTypes] = None,
  8. auth: typing.Union[
  9. httpx._types.AuthTypes, httpx._client.UseClientDefault
  10. ] = httpx._client.USE_CLIENT_DEFAULT,
  11. follow_redirects: typing.Optional[bool] = None,
  12. allow_redirects: typing.Optional[bool] = None,
  13. timeout: typing.Union[
  14. httpx._client.TimeoutTypes, httpx._client.UseClientDefault
  15. ] = httpx._client.USE_CLIENT_DEFAULT,
  16. extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
  17. ) -> httpx.Response:
  18. redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
  19. return super().delete(
  20. url,
  21. params=params,
  22. headers=headers,
  23. cookies=cookies,
  24. auth=auth,
  25. follow_redirects=redirect,
  26. timeout=timeout,
  27. extensions=extensions,
  28. )

websocket_connect

  1. websocket_connect(url, subprotocols=None, **kwargs)
PARAMETERDESCRIPTION
url

TYPE: str

subprotocols

TYPE: Sequence[str] DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in starlette/testclient.py

  1. 708
  2. 709
  3. 710
  4. 711
  5. 712
  6. 713
  7. 714
  8. 715
  9. 716
  10. 717
  11. 718
  12. 719
  13. 720
  14. 721
  15. 722
  16. 723
  17. 724
  18. 725
  19. 726
  1. def websocket_connect(
  2. self, url: str, subprotocols: typing.Sequence[str] = None, kwargs: typing.Any
  3. ) -> typing.Any:
  4. url = urljoin(ws://testserver”, url)
  5. headers = kwargs.get(headers, {})
  6. headers.setdefault(connection, upgrade)
  7. headers.setdefault(sec-websocket-key, testserver==”)
  8. headers.setdefault(sec-websocket-version, 13)
  9. if subprotocols is not None:
  10. headers.setdefault(sec-websocket-protocol, “, .join(subprotocols))
  11. kwargs[headers] = headers
  12. try:
  13. super().request(GET, url, kwargs)
  14. except _Upgrade as exc:
  15. session = exc.session
  16. else:
  17. raise RuntimeError(Expected WebSocket upgrade) # pragma: no cover
  18. return session

lifespan async

  1. lifespan()

Source code in starlette/testclient.py

  1. 758
  2. 759
  3. 760
  4. 761
  5. 762
  6. 763
  1. async def lifespan(self) -> None:
  2. scope = {type: lifespan, state: self.app_state}
  3. try:
  4. await self.app(scope, self.stream_receive.receive, self.stream_send.send)
  5. finally:
  6. await self.stream_send.send(None)

wait_startup async

  1. wait_startup()

Source code in starlette/testclient.py

  1. 765
  2. 766
  3. 767
  4. 768
  5. 769
  6. 770
  7. 771
  8. 772
  9. 773
  10. 774
  11. 775
  12. 776
  13. 777
  14. 778
  15. 779
  16. 780
  1. async def wait_startup(self) -> None:
  2. await self.stream_receive.send({type: lifespan.startup})
  3. async def receive() -> typing.Any:
  4. message = await self.stream_send.receive()
  5. if message is None:
  6. self.task.result()
  7. return message
  8. message = await receive()
  9. assert message[type] in (
  10. lifespan.startup.complete,
  11. lifespan.startup.failed,
  12. )
  13. if message[type] == lifespan.startup.failed:
  14. await receive()

wait_shutdown async

  1. wait_shutdown()

Source code in starlette/testclient.py

  1. 782
  2. 783
  3. 784
  4. 785
  5. 786
  6. 787
  7. 788
  8. 789
  9. 790
  10. 791
  11. 792
  12. 793
  13. 794
  14. 795
  15. 796
  16. 797
  1. async def wait_shutdown(self) -> None:
  2. async def receive() -> typing.Any:
  3. message = await self.stream_send.receive()
  4. if message is None:
  5. self.task.result()
  6. return message
  7. async with self.stream_send:
  8. await self.stream_receive.send({type: lifespan.shutdown})
  9. message = await receive()
  10. assert message[type] in (
  11. lifespan.shutdown.complete,
  12. lifespan.shutdown.failed,
  13. )
  14. if message[type] == lifespan.shutdown.failed:
  15. await receive()