Package org.springframework.mock.web

# Class MockCookie

java.lang.Object
jakarta.servlet.http.Cookie
org.springframework.mock.web.MockCookie

All Implemented Interfaces:
  `Serializable`, `Cloneable`

public class MockCookie extends Cookie
Extension of `Cookie` with extra attributes, as defined in RFC 6265.

As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.

Since:
  5.1
Author:
  Vedran Pavic, Juergen Hoeller, Sam Brannen
See Also:
  - Serialized Form

## Constructor Summary

Constructors
Constructor
Description
`MockCookie(String name, String value)`
Construct a new `MockCookie` with the supplied name and value.

## Method Summary

Modifier and Type
Method
Description
`ZonedDateTime`
`getExpires()`
Get the "Expires" attribute for this cookie.
`String`
`getSameSite()`
Get the "SameSite" attribute for this cookie.
`boolean`
`isPartitioned()`
Return whether the "Partitioned" attribute is set for this cookie.
`static MockCookie`
`parse(String setCookieHeader)`
Factory method that parses the value of the supplied "Set-Cookie" header.
`void`
`setAttribute(String name, String value)`
`void`
`setExpires(ZonedDateTime expires)`
Set the "Expires" attribute for this cookie.
`void`
`setPartitioned(boolean partitioned)`
Set the "Partitioned" attribute for this cookie.
`void`
`setSameSite(String sameSite)`
Set the "SameSite" attribute for this cookie.
`String`
`toString()`

### Methods inherited from class jakarta.servlet.http.Cookie

`clone, equals, getAttribute, getAttributes, getComment, getDomain, getMaxAge, getName, getPath, getSecure, getValue, getVersion, hashCode, isHttpOnly, setComment, setDomain, setHttpOnly, setMaxAge, setPath, setSecure, setValue, setVersion`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### MockCookie

public MockCookie(String name, String value)
Construct a new `MockCookie` with the supplied name and value.

Parameters:
  `name` - the name
  `value` - the value
See Also:
  - `Cookie(String, String)`

## Method Details

### setExpires

public void setExpires(@Nullable ZonedDateTime expires)
Set the "Expires" attribute for this cookie.

Since:
  5.1.11

### getExpires

@Nullable public ZonedDateTime getExpires()
Get the "Expires" attribute for this cookie.

Returns:
  the "Expires" attribute for this cookie, or `null` if not set
Since:
  5.1.11

### setSameSite

public void setSameSite(@Nullable String sameSite)
Set the "SameSite" attribute for this cookie.

This limits the scope of the cookie such that it will only be attached to same-site requests if the supplied value is `"Strict"` or cross-site requests if the supplied value is `"Lax"`.

See Also:
  - RFC6265 bis

### getSameSite

@Nullable public String getSameSite()
Get the "SameSite" attribute for this cookie.

Returns:
  the "SameSite" attribute for this cookie, or `null` if not set

### setPartitioned

public void setPartitioned(boolean partitioned)
Set the "Partitioned" attribute for this cookie.

Since:
  6.2
See Also:
  - The Partitioned attribute spec

### isPartitioned

public boolean isPartitioned()
Return whether the "Partitioned" attribute is set for this cookie.

Since:
  6.2
See Also:
  - The Partitioned attribute spec

### parse

public static MockCookie parse(String setCookieHeader)
Factory method that parses the value of the supplied "Set-Cookie" header.

Parameters:
  `setCookieHeader` - the "Set-Cookie" value; never `null` or empty
Returns:
  the created cookie

### setAttribute

public void setAttribute(String name, @Nullable String value)

Overrides:
  `setAttribute` in class `Cookie`

### toString

public String toString()

Overrides:
  `toString` in class `Cookie`