Rest Assured Cheat Sheet

Rest Assured Setup



Maven Dependency


XML
Copy
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
    <scope>test</scope>
</dependency>

Static Imports


import static io.restassured.RestAssured.*; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.*;

Basic HTTP Requests



Basic HTTP Requests


Java
Copy
given()
    .queryParam("key", "value")
.when()
    .get("/endpoint")
.then()
    .statusCode(200)
    .body("key", equalTo("expectedValue"));

POST Request


Java
Copy
given()
    .header("Content-Type", "application/json")
    .body("{ \"name\": \"John\" }")
.when()
    .post("/endpoint")
.then()
    .statusCode(201)
    .body("id", notNullValue());

PUT Request


Java
Copy
given()
    .header("Content-Type", "application/json")
    .body("{ \"name\": \"John Updated\" }")
.when()
    .put("/endpoint/{id}", 1)
.then()
    .statusCode(200)
    .body("name", equalTo("John Updated"));

DELETE Request


Java
Copy
when()
    .delete("/endpoint/{id}", 1)
.then()
    .statusCode(204);

Request Specifications



Base URI and Base Path

RestAssured.baseURI = "https://api.example.com"; RestAssured.basePath = "/v1";

Making a request

Java
Copy
RequestSpecification requestSpec = new RequestSpecBuilder()
    .setBaseUri("https://api.example.com")
    .addHeader("Authorization", "Bearer token")
    .build();

given()
    .spec(requestSpec)
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

Response Handling



Extracting Response

Java
Copy
Response response = 
given()
    .when()
    .get("/endpoint");

String jsonResponse = response.asString();
int statusCode = response.getStatusCode();

Response Validation


Java
Copy
when()
    .get("/endpoint")
.then()
    .statusCode(200)
    .body("name", equalTo("John"))
    .body("age", greaterThan(20));

Response Headers


Java
Copy
Response response = 
given()
    .when()
    .get("/endpoint");

String contentType = response.header("Content-Type");

Response Time



Java
Copy
long timeInMs = 
given()
    .when()
    .get("/endpoint")
.then()
    .time();

Parameters and Headers



Query Parameters


Java
Copy
given()
    .queryParam("param1", "value1")
    .queryParam("param2", "value2")
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

Path Parameters


Java
Copy
given()
    .pathParam("id", 123)
.when()
    .get("/endpoint/{id}")
.then()
    .statusCode(200);

Headers


Java
Copy
given()
    .header("Authorization", "Bearer token")
    .header("Content-Type", "application/json")
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

Data Serialization and Deserialization



Sending JSON in the Body

Java
Copy
Map<String, String> jsonBody = new HashMap<>();
jsonBody.put("name", "John");
jsonBody.put("age", "30");

given()
    .header("Content-Type", "application/json")
    .body(jsonBody)
.when()
    .post("/endpoint")
.then()
    .statusCode(201);

Deserialize Response JSON to Java Object


Java
Copy
User user = 
given()
    .when()
    .get("/endpoint")
.then()
    .extract().as(User.class);

Serialize Java Object to JSON



Java
Copy
User user = new User("John", 30);

given()
    .contentType("application/json")
    .body(user)
.when()
    .post("/endpoint")
.then()
    .statusCode(201);

Authentication



Basic Authentication


Java
Copy
given()
    .auth()
    .basic("username", "password")
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

OAuth2 Authentication


Java
Copy
given()
    .auth()
    .oauth2("your_oauth2_token")
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

Assertions



Status Code Assertions


Java
Copy
when()
    .get("/endpoint")
.then()
    .statusCode(200);

Body Content Assertions


Java
Copy
when()
    .get("/endpoint")
.then()
    .body("name", equalTo("John"))
    .body("id", notNullValue());

Header Assertions


Java
Copy
when()
    .get("/endpoint")
.then()
    .header("Content-Type", "application/json")
    .header("X-Rate-Limit", equalTo("60"));

Logging



Log Request and Response


Java
Copy
given()
    .log().all()
.when()
    .get("/endpoint")
.then()
    .log().all();

Log Only on Failure


Java
Copy
given()
    .log().ifValidationFails()
.when()
    .get("/endpoint")
.then()
    .log().ifValidationFails();

Advanced Features



Handling Cookies


Java
Copy
Response response = 
given()
    .cookie("session_id", "abcdef")
.when()
    .get("/endpoint");

String sessionId = response.getCookie("session_id");

Multi-Part File Upload


Java
Copy
given()
    .multiPart(new File("/path/to/file"))
.when()
    .post("/upload")
.then()
    .statusCode(200);

Query Parameter Collection


Java
Copy
List<String> params = Arrays.asList("param1", "param2", "param3");

given()
    .queryParam("key", params)
.when()
    .get("/endpoint")
.then()
    .statusCode(200);

TestNG Integration



TestNG Test Case Get Example


Java
Copy
@Test
public void testGetRequest() {
    given()
        .when()
        .get("/endpoint")
    .then()
        .statusCode(200)
        .body("name", equalTo("John"));
}

TestNG Test Case Post Example


Java
Copy
@Test
public void testPostRequest() {
    given()
        .header("Content-Type", "application/json")
        .body("{ \"name\": \"John\" }")
    .when()
        .post("/endpoint")
    .then()
        .statusCode(201);
}

Common Utilities



Handling Timeouts


Java
Copy
RestAssured.config = RestAssured.config()
    .httpClient(HttpClientConfig.httpClientConfig()
    .setParam("http.connection.timeout", 5000)
    .setParam("http.socket.timeout", 5000));

Setting Proxy


Java
Copy
RestAssured.proxy("localhost", 8080);