Skip to content

Cookie

This section describes matcher functions designed to target and match cookies headers in incoming HTTP requests.

Attention: To use these matchers, enable the cookies feature by adding --features=cookies to your Cargo command. For example: cargo test --features=cookies.

Sets the cookie that needs to exist in the HTTP request. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie. Must be a case-sensitive match.
  • value: The expected value of the cookie.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with the value "1234567890"
let mock = server.mock(|when, then| {
when.cookie("SESSIONID", "1234567890");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=1234567890; CONSENT=1")
.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.

Sets the cookie that should not exist or should not have a specific value in the HTTP request. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie. Must be a case-sensitive match.
  • value: The value that the cookie should not have.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" to not have the value "1234567890"
let mock = server.mock(|when, then| {
when.cookie_not("SESSIONID", "1234567890");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=0987654321; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist in the HTTP request. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID"
let mock = server.mock(|when, then| {
when.cookie_exists("SESSIONID");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=1234567890; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must not exist in the HTTP request. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must not exist.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" not to exist
let mock = server.mock(|when, then| {
when.cookie_missing("SESSIONID");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that does not include the excluded cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must contain the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_substring: The substring that must be present in the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value containing "1234"
let mock = server.mock(|when, then| {
when.cookie_includes("SESSIONID", "1234");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=abc1234def; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must not contain the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_substring: The substring that must not be present in the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value not containing "1234"
let mock = server.mock(|when, then| {
when.cookie_excludes("SESSIONID", "1234");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=abcdef; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must start with the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_prefix: The substring that must be at the start of the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value starting with "1234"
let mock = server.mock(|when, then| {
when.cookie_prefix("SESSIONID", "1234");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=1234abcdef; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must not start with the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_prefix: The substring that must not be at the start of the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value not starting with "1234"
let mock = server.mock(|when, then| {
when.cookie_prefix_not("SESSIONID", "1234");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=abcd1234; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must end with the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_suffix: The substring that must be at the end of the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value ending with "7890"
let mock = server.mock(|when, then| {
when.cookie_suffix("SESSIONID", "7890");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=abcdef7890; CONSENT=1")
.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.

Sets the requirement that a cookie with the specified name must exist and its value must not end with the specified substring. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • name: The name of the cookie that must exist.
  • value_suffix: The substring that must not be at the end of the cookie value.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie named "SESSIONID" with a value not ending with "7890"
let mock = server.mock(|when, then| {
when.cookie_suffix_not("SESSIONID", "7890");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=abcdef1234; CONSENT=1")
.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.

Sets the requirement that a cookie with a name matching the specified regex must exist and its value must match the specified regex. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • key_regex: The regex pattern that the cookie name must match.
  • value_regex: The regex pattern that the cookie value must match.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie with a name matching the regex "^SESSION"
// and a value matching the regex "^[0-9]{10}$"
let mock = server.mock(|when, then| {
when.cookie_matches(r"^SESSION", r"^[0-9]{10}$");
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookie
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "TRACK=12345; SESSIONID=1234567890; CONSENT=1")
.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.

Sets the requirement that a cookie with a name and value matching the specified regexes must appear a specified number of times in the HTTP request. Cookie parsing follows RFC-6265. Attention: Cookie names are case-sensitive.

Parameters

  • key_regex: The regex pattern that the cookie name must match.
  • value_regex: The regex pattern that the cookie value must match.
  • count: The number of times a cookie with a matching name and value must appear.

Note: This function is only available when the cookies feature is enabled. This feature is enabled by default.

Example

use httpmock::prelude::*;
use reqwest::blocking::Client;
// Start a new mock server
let server = MockServer::start();
// Create a mock that expects a cookie with a name matching the regex "^SESSION"
// and a value matching the regex "^[0-9]{10}$" to appear exactly twice
let mock = server.mock(|when, then| {
when.cookie_count(r"^SESSION", r"^[0-9]{10}$", 2);
then.status(200); // Respond with a 200 status code if the condition is met
});
// Make a request that includes the required cookies
Client::new()
.post(&format!("http://{}/test", server.address()))
.header("Cookie", "SESSIONID=1234567890; TRACK=12345; SESSIONTOKEN=0987654321; CONSENT=1")
.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.