Request Matchers
This section describes matcher functions designed to target and match the host in incoming HTTP requests.
These matchers are especially useful when using the proxy and record-and-playback features of httpmock
.
host
Sets the expected host name. This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: Both localhost
and 127.0.0.1
are treated equally.
If the provided host is set to either localhost
or 127.0.0.1
, it will match
requests containing either localhost
or 127.0.0.1
.
host
- The host name (should not include a port).
Example
Returns
The updated When
instance to enable method chaining.
host_not
Sets the host name that should NOT be responded for.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple suffixes, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
host
- The host name (should not include a port).
Example
Returns
The updated When
instance to enable method chaining.
host_includes
Adds a substring to match within the request’s host name.
This method ensures that the mock server only matches requests whose host name contains the specified substring.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple substrings, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Attention
This function does not automatically treat 127.0.0.1 like localhost.
Example
Parameters
host
: A string or other type convertible toString
that will be added as a substring to match against the request’s host name.
Returns
The updated When
instance to enable method chaining.
host_excludes
Adds a substring that must not be present within the request’s host name for the mock server to respond.
This method ensures that the mock server does not respond to requests if the host name contains the specified substring.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple excluded substrings, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Example
Parameters
host
: A string or other type convertible toString
that will be added as a substring to exclude from matching.
Returns
The updated When
instance to enable method chaining.
host_prefix
Adds a prefix that the request’s host name must start with for the mock server to respond.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple prefixes, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Example
Parameters
prefix
: A string or other type convertible toString
specifying the prefix that the host name should start with.
Returns
The updated When
instance to enable method chaining.
host_prefix_not
Adds a prefix that the request’s host name must not start with for the mock server to respond.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple excluded prefixes, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Example
Parameters
prefix
: A string or other type convertible toString
specifying the prefix that the host name should not start with.
Returns
The updated When
instance to enable method chaining.
host_suffix
Adds a suffix that the request’s host name must end with for the mock server to respond.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple suffixes, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Example
Parameters
host
: A string or other type convertible toString
specifying the suffix that the host name should end with.
Returns
The updated When
instance to enable method chaining.
host_suffix_not
Adds a suffix that the request’s host name must not end with for the mock server to respond.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple excluded suffixes, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Example
Parameters
host
: A string or other type convertible toString
specifying the suffix that the host name should not end with.
Returns
The updated When
instance to enable method chaining.
host_matches
Sets a regular expression pattern that the request’s host name must match for the mock server to respond.
This constraint is especially useful when working with proxy or forwarding rules, but it can also be used to serve mocks (e.g., when using a mock server as a proxy).
To add multiple patterns, invoke this function multiple times.
Note: Host matching is case-insensitive, conforming to RFC 3986, Section 3.2.2. This standard dictates that all host names are treated equivalently, regardless of character case.
Note: This function does not automatically compare with pseudo names, like “localhost”.
Parameters
regex
: A regular expression pattern to match against the host name. Should be a valid regex string.
Example
Returns
The updated When
instance to enable method chaining.