Skip to content

Body

This section describes matcher functions designed to target and match HTTP request body content in incoming HTTP requests.

body

Sets the required HTTP request body content. This method specifies that the HTTP request body must match the provided content exactly.

Note: The body content is case-sensitive and must be an exact match.

Parameters

  • body: The required HTTP request body content. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to be "The Great Gatsby"
let mock = server.mock(|when, then| {
when.body("The Great Gatsby");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with the required body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated When instance to allow method chaining for additional configuration.

body_not

Sets the condition that the HTTP request body content must not match the specified value. This method ensures that the request body does not contain the provided content exactly.

Note: The body content is case-sensitive and must be an exact mismatch.

Parameters

  • body: The body content that the HTTP request must not contain. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to not be "The Great Gatsby"
let mock = server.mock(|when, then| {
when.body_not("The Great Gatsby");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a different body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("A Tale of Two Cities")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated When instance to allow method chaining for additional configuration.

body_includes

Sets the condition that the HTTP request body content must contain the specified substring. This method ensures that the request body includes the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • substring: The substring that the HTTP request body must contain. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to contain the substring "Gatsby"
let mock = server.mock(|when, then| {
when.body_includes("Gatsby");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with the required substring in the body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby is a novel.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated When instance to allow method chaining for additional configuration.

body_excludes

Sets the condition that the HTTP request body content must not contain the specified substring. This method ensures that the request body does not include the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • substring: The substring that the HTTP request body must not contain. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to not contain the substring "Gatsby"
let mock = server.mock(|when, then| {
when.body_excludes("Gatsby");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a different body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("A Tale of Two Cities is a novel.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated When instance to allow method chaining for additional configuration.

body_prefix

Sets the condition that the HTTP request body content must begin with the specified substring. This method ensures that the request body starts with the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • prefix: The substring that the HTTP request body must begin with. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to begin with the substring "The Great"
let mock = server.mock(|when, then| {
when.body_prefix("The Great");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with the required prefix in the body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby is a novel.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated When instance to allow method chaining for additional configuration.

body_prefix_not

Sets the condition that the HTTP request body content must not begin with the specified substring. This method ensures that the request body does not start with the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • prefix: The substring that the HTTP request body must not begin with. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to not begin with the substring "Error:"
let mock = server.mock(|when, then| {
when.body_prefix_not("Error:");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a different body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("Success: Operation completed.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated `When’ instance to allow method chaining for additional configuration.

body_suffix

Sets the condition that the HTTP request body content must end with the specified substring. This method ensures that the request body concludes with the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • suffix: The substring that the HTTP request body must end with. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to end with the substring "a novel."
let mock = server.mock(|when, then| {
when.body_suffix("a novel.");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with the required suffix in the body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby is a novel.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated `When’ instance to allow method chaining for additional configuration.

body_suffix_not

Sets the condition that the HTTP request body content must not end with the specified substring. This method ensures that the request body does not conclude with the provided content as a substring.

Note: The body content is case-sensitive.

Parameters

  • suffix: The substring that the HTTP request body must not end with. This parameter accepts any type that can be converted into a String.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to not end with the substring "a novel."
let mock = server.mock(|when, then| {
when.body_suffix_not("a novel.");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a different body content
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby is a story.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated `When’ instance to allow method chaining for additional configuration.

body_matches

Sets the condition that the HTTP request body content must match the specified regular expression. This method ensures that the request body fully conforms to the provided regex pattern.

Note: The regex matching is case-sensitive unless the regex is explicitly defined to be case-insensitive.

Parameters

  • pattern: The regular expression pattern that the HTTP request body must match. This parameter accepts any type that can be converted into a Regex.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to match the regex pattern "^The Great Gatsby.*"
let mock = server.mock(|when, then| {
when.body_matches("^The Great Gatsby.*");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a body that matches the regex pattern
Client::new()
.post(&format!("http://{}/test", server.address()))
.body("The Great Gatsby is a novel by F. Scott Fitzgerald.")
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated `When’ instance to allow method chaining for additional configuration.

JSON Body

json_body

Sets the condition that the HTTP request body content must match the specified JSON structure. This method ensures that the request body exactly matches the JSON value provided.

Note: The body content is case-sensitive.

Note: This method does not automatically verify the Content-Type header. If specific content type verification is required (e.g., application/json), you must add this expectation manually.

Parameters

  • json_value: The JSON structure that the HTTP request body must match. This parameter accepts any type that can be converted into a serde_json::Value.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
use serde_json::json;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects the request body to match a specific JSON structure
let mock = server.mock(|when, then| {
when.json_body(json!({
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald"
}));
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request with a JSON body that matches the expected structure
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Content-Type", "application/json") // It's important to set the Content-Type header manually
.body(r#"{"title":"The Great Gatsby","author":"F. Scott Fitzgerald"}"#)
.send()
.unwrap();
// Verify that the mock was called at least once
mock.assert();

Returns

The updated `When’ instance to allow method chaining for additional configuration.

json_body_includes

Sets the expected partial JSON body to check for specific content within a larger JSON structure.

Attention: The partial JSON string must be a valid JSON string and should represent a substructure of the full JSON object. It can omit irrelevant attributes but must maintain any necessary object hierarchy.

Note: This method does not automatically set the Content-Type header to application/json. You must explicitly set this header in your requests.

Parameters

  • partial_body: The partial JSON content to check for. This must be a valid JSON string.

Example

Suppose your application sends the following JSON request body:

{
"parent_attribute": "Some parent data goes here",
"child": {
"target_attribute": "Example",
"other_attribute": "Another value"
}
}

To verify the presence of target_attribute with the value Example without needing the entire JSON object:

use httpmock::prelude::*;
use reqwest::blocking::Client;
let server = MockServer::start();
let mock = server.mock(|when, then| {
when.json_body_includes(r#"
{
"child": {
"target_attribute": "Example"
}
}
"#);
then.status(200);
});
// Send a POST request with a JSON body
let response = Client::new()
.post(&format!("http://{}/some/path", server.address()))
.header("content-type", "application/json")
.body(r#"
{
"parent_attribute": "Some parent data goes here",
"child": {
"target_attribute": "Example",
"other_attribute": "Another value"
}
}
"#)
.send()
.unwrap();
// Assert the mock was called and the response status is as expected
mock.assert();
assert_eq!(response.status(), 200);

It’s important that the partial JSON contains the full object hierarchy necessary to reach the target attribute. Irrelevant attributes such as parent_attribute and child.other_attribute can be omitted.

json_body_excludes

Sets the expected partial JSON body to ensure that specific content is not present within a larger JSON structure.

Attention: The partial JSON string must be a valid JSON string and should represent a substructure of the full JSON object. It can omit irrelevant attributes but must maintain any necessary object hierarchy.

Note: This method does not automatically set the Content-Type header to application/json. You must explicitly set this header in your requests.

Parameters

  • partial_body: The partial JSON content to check for exclusion. This must be a valid JSON string.

Example

Suppose your application sends the following JSON request body:

{
"parent_attribute": "Some parent data goes here",
"child": {
"target_attribute": "Example",
"other_attribute": "Another value"
}
}

To verify the absence of target_attribute with the value Example:

use httpmock::prelude::*;
use reqwest::blocking::Client;
let server = MockServer::start();
let mock = server.mock(|when, then| {
when.json_body_excludes(r#"
{
"child": {
"target_attribute": "Example"
}
}
"#);
then.status(200);
});
// Send a POST request with a JSON body
let response = Client::new()
.post(&format!("http://{}/some/path", server.address()))
.header("content-type", "application/json")
.body(r#"
{
"parent_attribute": "Some parent data goes here",
"child": {
"other_attribute": "Another value"
}
}
"#)
.send()
.unwrap();
// Assert the mock was called and the response status is as expected
mock.assert();
assert_eq!(response.status(), 200);

It’s important that the partial JSON contains the full object hierarchy necessary to reach the target attribute. Irrelevant attributes such as parent_attribute and child.other_attribute in the example can be omitted.

URL Encoded Body

form_urlencoded_tuple

Adds a key-value pair to the requirements for an application/x-www-form-urlencoded request body.

This method sets an expectation for a specific key-value pair to be included in the request body of an application/x-www-form-urlencoded POST request. Each key and value are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key of the key-value pair to set as a requirement.
  • value: The value of the key-value pair to set as a requirement.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple("name", "Peter Griffin")
.form_urlencoded_tuple("town", "Quahog");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value pair added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_not

Adds a key-value pair to the negative requirements for an application/x-www-form-urlencoded request body.

This method sets an expectation for a specific key-value pair to be excluded from the request body of an application/x-www-form-urlencoded POST request. Each key and value are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key of the key-value pair to set as a requirement.
  • value: The value of the key-value pair to set as a requirement.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_not("name", "Peter Griffin");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Lois%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value pair added to the negative application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_exists

Sets a requirement for the existence of a key in an application/x-www-form-urlencoded request body.

This method sets an expectation that a specific key must be present in the request body of an application/x-www-form-urlencoded POST request, regardless of its value. The key is URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key that must exist in the application/x-www-form-urlencoded request body.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_exists("name")
.form_urlencoded_tuple_exists("town");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key existence requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_missing

Sets a requirement that a key must be absent in an application/x-www-form-urlencoded request body.

This method sets an expectation that a specific key must not be present in the request body of an application/x-www-form-urlencoded POST request. The key is URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key that must be absent in the application/x-www-form-urlencoded request body.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_missing("name")
.form_urlencoded_tuple_missing("town");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("city=Quahog&occupation=Cartoonist")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key absence requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_includes

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must contain a specific substring.

This method sets an expectation that the value associated with a specific key must contain a specified substring in the request body of an application/x-www-form-urlencoded POST request. The key and the substring are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • substring: The substring that must be present in the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_includes("name", "Griffin")
.form_urlencoded_tuple_includes("town", "Quahog");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value substring requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_excludes

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must not contain a specific substring.

This method sets an expectation that the value associated with a specific key must not contain a specified substring in the request body of an application/x-www-form-urlencoded POST request. The key and the substring are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • substring: The substring that must not be present in the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_excludes("name", "Griffin");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Lois%20Smith&city=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value substring exclusion requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_prefix

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must start with a specific prefix.

This method sets an expectation that the value associated with a specific key must start with a specified prefix in the request body of an application/x-www-form-urlencoded POST request. The key and the prefix are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • prefix: The prefix that must appear at the start of the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_prefix("name", "Pete")
.form_urlencoded_tuple_prefix("town", "Qua");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value prefix requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_prefix_not

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must not start with a specific prefix.

This method sets an expectation that the value associated with a specific key must not start with a specified prefix in the request body of an application/x-www-form-urlencoded POST request. The key and the prefix are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • prefix: The prefix that must not appear at the start of the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_prefix_not("name", "Lois")
.form_urlencoded_tuple_prefix_not("town", "Hog");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value prefix exclusion requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_suffix

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must end with a specific suffix.

This method sets an expectation that the value associated with a specific key must end with a specified suffix in the request body of an application/x-www-form-urlencoded POST request. The key and the suffix are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • suffix: The suffix that must appear at the end of the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_suffix("name", "Griffin")
.form_urlencoded_tuple_suffix("town", "hog");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value suffix requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_suffix_not

Sets a requirement that a key’s value in an application/x-www-form-urlencoded request body must not end with a specific suffix.

This method sets an expectation that the value associated with a specific key must not end with a specified suffix in the request body of an application/x-www-form-urlencoded POST request. The key and the suffix are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key: The key in the application/x-www-form-urlencoded request body.
  • suffix: The suffix that must not appear at the end of the value associated with the key.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_suffix_not("name", "Smith")
.form_urlencoded_tuple_suffix_not("town", "ville");
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value suffix exclusion requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_matches

Sets a requirement that a key-value pair in an application/x-www-form-urlencoded request body must match specific regular expressions.

This method sets an expectation that the key and the value in a key-value pair must match the specified regular expressions in the request body of an application/x-www-form-urlencoded POST request. The key and value regular expressions are URL-encoded as specified by the URL Standard.

Note: The mock server does not automatically verify that the HTTP method is POST as per spec. If you want to verify that the request method is POST, you must explicitly set it in your mock configuration.

Parameters

  • key_regex: The regular expression that the key must match in the application/x-www-form-urlencoded request body.
  • value_regex: The regular expression that the value must match in the application/x-www-form-urlencoded request body.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
use regex::Regex;
// Arrange
let server = MockServer::start();
let key_regex = Regex::new(r"^name$").unwrap();
let value_regex = Regex::new(r"^Peter\sGriffin$").unwrap();
let m = server.mock(|when, then| {
when.method(POST)
.path("/example")
.header("content-type", "application/x-www-form-urlencoded")
.form_urlencoded_tuple_matches(key_regex, value_regex);
then.status(202);
});
let response = Client::new()
.post(server.url("/example"))
.header("content-type", "application/x-www-form-urlencoded")
.body("name=Peter%20Griffin&town=Quahog")
.send()
.unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 202);

Returns

When: Returns the modified When object with the new key-value regex matching requirement added to the application/x-www-form-urlencoded expectations.

form_urlencoded_tuple_key_value_count