Interview #68: Rest-Assured: How do you send headers and cookies with a request?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
Sending headers and cookies with a request in RestAssured is a common requirement in API testing. RestAssured is a Java library that simplifies testing RESTful APIs by providing an easy-to-use DSL for creating and verifying HTTP requests and responses.
Disclaimer: For QA-Testing Jobs/Training, WhatsApp us @ 91-9606623245
Overview:
Headers and cookies are critical in HTTP requests:
In RestAssured, you can set headers and cookies for requests using methods like header(), headers(), cookie(), and cookies().
Steps to Send Headers and Cookies with RestAssured:
Example Code
Sending a Request with a Single Header and Single Cookie:
import io.restassured.RestAssured;
public class RestAssuredHeadersAndCookies {
public static void main(String[] args) {
RestAssured.baseURI = "https://api.example.com";
RestAssured.given()
.header("Authorization", "Bearer someAccessToken") // Add a header
.cookie("session_id", "12345abcd") // Add a cookie
.when()
.get("/endpoint") // Perform a GET request
.then()
.statusCode(200) // Assert the status code
.log().all(); // Log the response
}
}
Sending Multiple Headers and Cookies:
import io.restassured.RestAssured;
import io.restassured.http.Headers;
import io.restassured.http.Header;
import io.restassured.http.Cookies;
import io.restassured.http.Cookie;
public class RestAssuredMultipleHeadersAndCookies {
public static void main(String[] args) {
RestAssured.baseURI = "https://api.example.com";
// Create multiple headers
Headers headers = new Headers(
new Header("Authorization", "Bearer someAccessToken"),
new Header("Content-Type", "application/json")
);
// Create multiple cookies
Cookies cookies = new Cookies(
new Cookie.Builder("session_id", "12345abcd").build(),
new Cookie.Builder("user_preference", "dark_mode").build()
);
RestAssured.given()
.headers(headers) // Add multiple headers
.cookies(cookies) // Add multiple cookies
.when()
.get("/endpoint")
.then()
.statusCode(200)
.log().all();
}
}
Explanation of Key Methods:
1. Headers:
header(String name, Object value):
headers(Headers headers):
领英推荐
Headers headers = new Headers(
new Header("Header1", "Value1"),
new Header("Header2", "Value2")
);
.headers(headers);
2. Cookies:
cookie(String name, Object value):
cookies(Cookies cookies):
Cookies cookies = new Cookies(
new Cookie.Builder("cookie1", "value1").build(),
new Cookie.Builder("cookie2", "value2").build()
);
.cookies(cookies);
Handling Dynamic Headers and Cookies:
String token = getTokenFromAuthAPI();
RestAssured.given()
.header("Authorization", "Bearer " + token)
.when()
.get("/secure-endpoint");
String sessionId = getSessionIdFromLoginAPI();
RestAssured.given()
.cookie("session_id", sessionId)
.when()
.get("/user-data");
Logging and Debugging:
RestAssured.given()
.header("Authorization", "Bearer someAccessToken")
.cookie("session_id", "12345abcd")
.log().all() // Logs the request details
.when()
.get("/endpoint")
.then()
.log().all(); // Logs the response details
Advanced Use Cases:
RestAssured.filters((requestSpec, responseSpec, ctx) -> {
requestSpec.header("Custom-Header", "CustomValue");
return ctx.next(requestSpec, responseSpec);
});
new Cookie.Builder("name", "value")
.setSecured(true)
.setHttpOnly(true)
.build();
Response response = RestAssured.given()
.when()
.get("/endpoint");
String headerValue = response.getHeader("Header-Name");
String cookieValue = response.getCookie("Cookie-Name");
Summary:
RestAssured’s flexibility and simplicity make it an excellent tool for comprehensive API testing involving headers and cookies.