# aioresponses package

## Submodules

## aioresponses.compat module

class aioresponses.compat.Pattern

  Bases: `object`

  Compiled regular expression object.

  findall(string, pos=0, endpos=9223372036854775807)

    Return a list of all non-overlapping matches of pattern in string.

  finditer(string, pos=0, endpos=9223372036854775807)

    Return an iterator over all non-overlapping matches for the RE pattern in string.

    For each match, the iterator returns a match object.

  flags

    The regex matching flags.

  fullmatch(string, pos=0, endpos=9223372036854775807)

    Matches against all of the string.

  groupindex

    A dictionary mapping group names to group numbers.

  groups

    The number of capturing groups in the pattern.

  match(string, pos=0, endpos=9223372036854775807)

    Matches zero or more characters at the beginning of the string.

  pattern

    The pattern string from which the RE object was compiled.

  scanner(string, pos=0, endpos=9223372036854775807)
  search(string, pos=0, endpos=9223372036854775807)

    Scan through string looking for a match, and return a corresponding match object instance.

    Return None if no position in the string matches.

  split(string, maxsplit=0)

    Split string by the occurrences of pattern.

  sub(repl, string, count=0)

    Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

  subn(repl, string, count=0)

    Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl.

class aioresponses.compat.RequestInfo(url: URL, method: str, headers: CIMultiDictProxy[str], real_url: URL | _SENTINEL = _SENTINEL.sentinel)

  Bases: `_RequestInfo`

class aioresponses.compat.URL(val: str | SplitResult | URL | UndefinedType = UndefinedType._singleton, *, encoded: bool = False, strict: bool | None = None)

  Bases: `object`

  absolute

    A check for absolute URLs.

    Return True for absolute ones (having scheme or starting with //), False otherwise.

  authority

    Decoded authority part of URL.

    Empty string for relative URLs.

  classmethod build(*, scheme: str = '', authority: str = '', user: str | None = None, password: str | None = None, host: str = '', port: int | None = None, path: str = '', query: None | str | Mapping[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]] | Sequence[tuple[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]]] = None, query_string: str = '', fragment: str = '', encoded: bool = False) → URL

    Creates and returns a new URL

  explicit_port

    Port part of URL, without scheme-based fallback.

    None for relative URLs or URLs without explicit port.

  extend_query(query: None | str | Mapping[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]] | Sequence[tuple[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]]]) → URL
  extend_query(**kwargs: str | SupportsInt | float | Sequence[str | SupportsInt | float]) → URL

    Return a new URL with query part combined with the existing.

    This method will not remove existing query parameters.

    Example: >>> url = URL(’http://example.com/?a=1&b=2’) >>> url.extend_query(a=3, c=4) URL(’http://example.com/?a=1&b=2&a=3&c=4’)

  fragment

    Decoded fragment part of URL.

    Empty string if fragment is missing.

  host

    Decoded host part of URL.

    None for relative URLs.

  host_port_subcomponent

    Return the host and port subcomponent part of URL.

    Trailing dots are removed from the host part.

    This value is suitable for use in the Host header of an HTTP request.

    None for relative URLs.

    https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2 IP-literal = “[” ( IPv6address / IPvFuture ) “]” https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3 port = *DIGIT

    Examples: - http://example.com:8080 -> example.com:8080 - http://example.com:80 -> example.com - http://example.com.:80 -> example.com - https://127.0.0.1:8443 -> 127.0.0.1:8443 - https://[::1]:8443 -> [::1]:8443 - http://[::1] -> [::1]

  host_subcomponent

    Return the host subcomponent part of URL.

    None for relative URLs.

    https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2

    IP-literal = “[” ( IPv6address / IPvFuture ) “]”

    Examples: - http://example.com:8080 -> example.com - http://example.com:80 -> example.com - https://127.0.0.1:8443 -> 127.0.0.1 - https://[::1]:8443 -> [::1] - http://[::1] -> [::1]

  human_repr() → str

    Return decoded human readable string for URL representation.

  is_absolute() → bool

    A check for absolute URLs.

    Return True for absolute ones (having scheme or starting with //), False otherwise.

    Is is preferred to call the .absolute property instead as it is cached.

  is_default_port() → bool

    A check for default port.

    Return True if port is default for specified scheme, e.g. ‘http://python.org’ or ‘http://python.org:80’, False otherwise.

    Return False for relative URLs.

  join(url: URL) → URL

    Join URLs

    Construct a full (“absolute”) URL by combining a “base URL” (self) with another URL (url).

    Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL.

  joinpath(*other: str, encoded: bool = False) → URL

    Return a new URL with the elements in other appended to the path.

  name

    The last part of parts.

  origin() → URL

    Return an URL with scheme, host and port parts only.

    user, password, path, query and fragment are removed.

  parent

    A new URL with last part of path removed and cleaned up query and fragment.

  parts

    A tuple containing decoded path parts.

    (‘/’,) for absolute URLs if path is missing.

  password

    Decoded password part of URL.

    None if password is missing.

  path

    Decoded path of URL.

    / for absolute URLs without path part.

  path_qs

    Decoded path of URL with query.

  path_safe

    Decoded path of URL.

    / for absolute URLs without path part.

    / (%2F) and % (%25) are not decoded

  port

    Port part of URL, with scheme-based fallback.

    None for relative URLs or URLs without explicit port and scheme without default port substitution.

  query

    A MultiDictProxy representing parsed query parameters in decoded representation.

    Empty value if URL has no query part.

  query_string

    Decoded query part of URL.

    Empty string if query is missing.

  raw_authority

    Encoded authority part of URL.

    Empty string for relative URLs.

  raw_fragment

    Encoded fragment part of URL.

    Empty string if fragment is missing.

  raw_host

    Encoded host part of URL.

    None for relative URLs.

    When working with IPv6 addresses, use the host_subcomponent property instead as it will return the host subcomponent with brackets.

  raw_name

    The last part of raw_parts.

  raw_parts

    A tuple containing encoded path parts.

    (‘/’,) for absolute URLs if path is missing.

  raw_password

    Encoded password part of URL.

    None if password is missing.

  raw_path

    Encoded path of URL.

    / for absolute URLs without path part.

  raw_path_qs

    Encoded path of URL with query.

  raw_query_string

    Encoded query part of URL.

    Empty string if query is missing.

  raw_suffix
  raw_suffixes
  raw_user

    Encoded user part of URL.

    None if user is missing.

  relative() → URL

    Return a relative part of the URL.

    scheme, user, password, host and port are removed.

  scheme

    Scheme for absolute URLs.

    Empty string for relative URLs or URLs starting with //

  suffix
  suffixes
  update_query(query: None | str | Mapping[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]] | Sequence[tuple[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]]]) → URL
  update_query(**kwargs: str | SupportsInt | float | Sequence[str | SupportsInt | float]) → URL

    Return a new URL with query part updated.

    This method will overwrite existing query parameters.

    Example: >>> url = URL(’http://example.com/?a=1&b=2’) >>> url.update_query(a=3, c=4) URL(’http://example.com/?a=3&b=2&c=4’)

  user

    Decoded user part of URL.

    None if user is missing.

  with_fragment(fragment: str | None) → URL

    Return a new URL with fragment replaced.

    Autoencode fragment if needed.

    Clear fragment to default if None is passed.

  with_host(host: str) → URL

    Return a new URL with host replaced.

    Autoencode host if needed.

    Changing host for relative URLs is not allowed, use .join() instead.

  with_name(name: str, *, keep_query: bool = False, keep_fragment: bool = False) → URL

    Return a new URL with name (last part of path) replaced.

    Query and fragment parts are cleaned up.

    Name is encoded if needed.

  with_password(password: str | None) → URL

    Return a new URL with password replaced.

    Autoencode password if needed.

    Clear password if argument is None.

  with_path(path: str, *, encoded: bool = False, keep_query: bool = False, keep_fragment: bool = False) → URL

    Return a new URL with path replaced.

  with_port(port: int | None) → URL

    Return a new URL with port replaced.

    Clear port to default if None is passed.

  with_query(query: None | str | Mapping[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]] | Sequence[tuple[str, str | SupportsInt | float | Sequence[str | SupportsInt | float]]]) → URL
  with_query(**kwargs: str | SupportsInt | float | Sequence[str | SupportsInt | float]) → URL

    Return a new URL with query part replaced.

    Accepts any Mapping (e.g. dict, multidict.MultiDict instances) or str, autoencode the argument if needed.

    A sequence of (key, value) pairs is supported as well.

    It also can take an arbitrary number of keyword arguments.

    Clear query if None is passed.

  with_scheme(scheme: str) → URL

    Return a new URL with scheme replaced.

  with_suffix(suffix: str, *, keep_query: bool = False, keep_fragment: bool = False) → URL

    Return a new URL with suffix (file extension of name) replaced.

    Query and fragment parts are cleaned up.

    suffix is encoded if needed.

  with_user(user: str | None) → URL

    Return a new URL with user replaced.

    Autoencode user if needed.

    Clear user/password if user is None.

  without_query_params(*query_params: str) → URL

    Remove some keys from query part and return new URL.

aioresponses.compat.merge_params(url: URL | str, params: Dict | None = None) → URL

aioresponses.compat.normalize_url(url: URL | str) → URL

  Normalize url to make comparisons.

aioresponses.compat.stream_reader_factory(loop: AbstractEventLoop | None = None) → StreamReader

## aioresponses.core module

class aioresponses.core.CallbackResult(method: str = 'GET', status: int = 200, body: str | bytes = '', content_type: str = 'application/json', payload: Dict | None = None, headers: Dict | None = None, response_class: Type[ClientResponse] | None = None, reason: str | None = None)

  Bases: `object`

class aioresponses.core.RequestCall(args, kwargs)

  Bases: `tuple`

  args

    Alias for field number 0

  kwargs

    Alias for field number 1

class aioresponses.core.RequestMatch(url: URL | str | Pattern, method: str = 'GET', status: int = 200, body: str | bytes = '', payload: Dict | None = None, exception: Exception | None = None, headers: Dict | None = None, content_type: str = 'application/json', response_class: Type[ClientResponse] | None = None, timeout: bool = False, repeat: bool = False, reason: str | None = None, callback: Callable | None = None)

  Bases: `object`

  async build_response(url: URL, **kwargs: Any) → ClientResponse | Exception
  match(method: str, url: URL) → bool
  match_regexp(url: URL) → bool
  match_str(url: URL) → bool
  url_or_pattern: URL | Pattern = None

class aioresponses.core.aioresponses(**kwargs: Any)

  Bases: `object`

  Mock aiohttp requests made by ClientSession.

  add(url: URL | str | Pattern, method: str = 'GET', status: int = 200, body: str | bytes = '', exception: Exception | None = None, content_type: str = 'application/json', payload: Dict | None = None, headers: Dict | None = None, response_class: Type[ClientResponse] | None = None, repeat: bool = False, timeout: bool = False, reason: str | None = None, callback: Callable | None = None) → None
  assert_any_call(url: URL | str | Pattern, method: str = 'GET', *args: Any, **kwargs: Any)

    assert the mock has been called with the specified arguments. The assert passes if the mock has ever been called, unlike assert_called_with and assert_called_once_with that only pass if the call is the most recent one.

  assert_called()

    assert that the mock was called at least once.

  assert_called_once()

    assert that the mock was called only once.

  assert_called_once_with(*args: Any, **kwargs: Any)

    assert that the mock was called once with the specified arguments. Raises an AssertionError if the args and keyword args passed in are different to the only call to the mock.

  assert_called_with(url: URL | str | Pattern, method: str = 'GET', *args: Any, **kwargs: Any)

    assert that the last call was made with the specified arguments.

    Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.

  assert_not_called()

    assert that the mock was never called.

  clear() → None
  delete(url: URL | str | Pattern, **kwargs: Any) → None
  get(url: URL | str | Pattern, **kwargs: Any) → None
  head(url: URL | str | Pattern, **kwargs: Any) → None
  static is_exception(resp_or_exc: ClientResponse | Exception) → bool
  async match(method: str, url: URL, allow_redirects: bool = True, **kwargs: Any) → ClientResponse | None
  options(url: URL | str | Pattern, **kwargs: Any) → None
  patch(url: URL | str | Pattern, **kwargs: Any) → None
  post(url: URL | str | Pattern, **kwargs: Any) → None
  put(url: URL | str | Pattern, **kwargs: Any) → None
  requests: Dict = None
  start() → None
  stop() → None

## Module contents

class aioresponses.CallbackResult(method: str = 'GET', status: int = 200, body: str | bytes = '', content_type: str = 'application/json', payload: Dict | None = None, headers: Dict | None = None, response_class: Type[ClientResponse] | None = None, reason: str | None = None)

  Bases: `object`

class aioresponses.aioresponses(**kwargs: Any)

  Bases: `object`

  Mock aiohttp requests made by ClientSession.

  add(url: URL | str | Pattern, method: str = 'GET', status: int = 200, body: str | bytes = '', exception: Exception | None = None, content_type: str = 'application/json', payload: Dict | None = None, headers: Dict | None = None, response_class: Type[ClientResponse] | None = None, repeat: bool = False, timeout: bool = False, reason: str | None = None, callback: Callable | None = None) → None
  assert_any_call(url: URL | str | Pattern, method: str = 'GET', *args: Any, **kwargs: Any)

    assert the mock has been called with the specified arguments. The assert passes if the mock has ever been called, unlike assert_called_with and assert_called_once_with that only pass if the call is the most recent one.

  assert_called()

    assert that the mock was called at least once.

  assert_called_once()

    assert that the mock was called only once.

  assert_called_once_with(*args: Any, **kwargs: Any)

    assert that the mock was called once with the specified arguments. Raises an AssertionError if the args and keyword args passed in are different to the only call to the mock.

  assert_called_with(url: URL | str | Pattern, method: str = 'GET', *args: Any, **kwargs: Any)

    assert that the last call was made with the specified arguments.

    Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.

  assert_not_called()

    assert that the mock was never called.

  clear() → None
  delete(url: URL | str | Pattern, **kwargs: Any) → None
  get(url: URL | str | Pattern, **kwargs: Any) → None
  head(url: URL | str | Pattern, **kwargs: Any) → None
  static is_exception(resp_or_exc: ClientResponse | Exception) → bool
  async match(method: str, url: URL, allow_redirects: bool = True, **kwargs: Any) → ClientResponse | None
  options(url: URL | str | Pattern, **kwargs: Any) → None
  patch(url: URL | str | Pattern, **kwargs: Any) → None
  post(url: URL | str | Pattern, **kwargs: Any) → None
  put(url: URL | str | Pattern, **kwargs: Any) → None
  requests: Dict = None
  start() → None
  stop() → None