Using Cookies

Applies to ReadyAPI 3.55, last modified on October 09, 2024

A web service may need to store client-specific information between different requests or sessions. This data may include a username, goods added to the shopping cart, customization, and so on. The server puts this information to cookies and sends them to a client.

A cookie is a name-value pair represented in plain text. When the client sends requests to the web service again, it includes the cookies to the request, so the web service gets the required data.

How Does It Work

When a web service needs to store information for later use, it adds the Set-Cookie header to the response. The value of the header contains the name and value of a cookie.

Set-Cookie: sessionid=001q2w3

Optionally, the Set-Cookie header may specify directives – additional parameters that control lifetime, visibility for server resources and so on. To learn more about possible directives, see the documentation on the MDN Web Docs website. For example, the following header specifies the cookie visible for the /store resource and its child resources:

Set-Cookie: cart=df86se2; Path=/store

Once a client receives this header in one of the responses, it should save the cookie. After this, the subsequent client requests to the same server will contain the Cookie header that specifies stored cookies. The client stores cookies until they expire or the HTTP session is closed. The sent cookies may look like this:

Cookie: sessionid=001q2w3; cart=df86se2

Use Cookies in Functional Tests

Store Received Cookies

By default, ReadyAPI does not store any cookies. To change this behavior, enable the Session test case option:

Using Cookies in ReadyAPI: Maintain HTTP Session

Click the image to enlarge it.

It forces ReadyAPI to store received cookies and include them to the subsequent requests to the server:

Using Cookies in ReadyAPI: HTTP Session

Click the image to enlarge it.

Add Cookies Manually

You can also, include the Cookie header manually:

Using Cookies in ReadyAPI: Sending a cookie

Click the image to enlarge it.

The value of the header can contain more than one cookie. For this, separate them with a semicolon:

Cookie: name=value; foo=bar

Use Cookies in Performance Tests

When ReadyAPI runs load tests, it executes underlying test cases. If the Session test case option is enabled for them, ReadyAPI stores cookies received during the sessions. In this case, virtual users have separate HTTP sessions, so cookies received by one of them will not affect others.

Using Cookies in Virtual Services

Mock services simulate web services, so they can send cookies to clients. For this, use the Set-Cookie header with the name and value of the cookie:

Set-Cookie: <name>=<value>
Using Cookies in ReadyAPI: Creating a cookie

Click the image to enlarge it.

You can specify as many Set-Cookie headers as you need.

Each cookie can contain directives that control a cookie’s lifetime, visibility for server resources, and so on. For example, the following header specifies a cookie that expires in 30 seconds:

Set-Cookie: name=value; Max-Age=30

To learn more about directives, see the documentation on the MDN Web Docs website.

See Also

Testing APIs
Test Case Editor
Request Editor Interface
Creating Responses

Highlight search results