How to blacklist headers in Rest Assured?

As of REST Assured 4.2.0 it's possible to blacklist headers so that they are not shown in the request or response log. Instead the header value will be replaced with [ BLACKLISTED]. 

Request

given()
    .config(RestAssured.config()
    .logConfig(LogConfig.logConfig().blacklistHeader("Accept"))
    .log().headers()

Output

Accept=[ BLACKLISTED ]

Response

given()
    .config(RestAssured.config()
    .logConfig(LogConfig.logConfig().blacklistHeader("set-cookie"))
then()
    .log().headers()

Output

set-cookie: [ BLACKLISTED ]

What is Response Specification in REST Assured?

‘ResponseSpecification’ interface comes handy in situation where similar set of assertions need to be done for several Rest requests. It achieves this by grouping common assertions into a ResponseSpecBuilder instance and using this instance for validations in multiple tests.

ResponseSpecBuilder resSpecBuilder;
static ResponseSpecification responseSpec;
	
resSpecBuilder = new ResponseSpecBuilder();
    resSpecBuilder.expectStatusCode(200);
    resSpecBuilder.expectBody("ad.company", equalTo("StatusCode Weekly"));
    responseSpec = resSpecBuilder.build();

What is Request Specification in REST Assured?

‘RequestSpecification’ interface provided by Rest Assured is used to club and extract repetitive actions like setting up base URL, headers, HTTP verbs etc which may be common for multiple Rest calls. The extracted common code can be used in different requests thereby reducing number of lines of code and increasing maintainability.

RequestSpecification requestSpecification = 
  RestAssured
    .given()
      .baseUri("path")
      .basePath("/booking");

What is the Difference between Postman and REST Assured?

 REST AssuredPostman
Built-in reportingUse Extent or AllureBuilt-in reporting
Type of ToolCode-BasedGUI
Control Over TestMore due to programmingLess
CostOpen-SourceA Limited Open-Source Version & An Advanced Proprietary Version.    
CollaborationNoBuilt-in
Data-DrivenNo LimitLimited to one file
LearningRequire Programming KnowledgeEasy to Learn
Integration with other toolEasy to integrate with other automation tools like SeleniumUse independent or plugin
InstallationPostman installation requiredJava, Maven, Eclipse or other IDE installation required
Parallel ExecutionSupportedNot-Supported
Type of ApplicationsSuitable for large applicationNot a good choice for large scale application
Type of ToolCode-BasedGUI

What is Method Chaining in REST Assured?

API chaining is a technique used in API testing to make multiple API requests in a sequence, where each request’s response becomes the basis for the next request. 

String createUser =

RestAssured
    .given()
        .contentType(ContentType.JSON)
        .body(“{ \”name\”: \”Ram\”, \”email\”: \”ram@example.com\” }”)
    .when()
         .post(baseURL + “/users”)
    .then()
        .statusCode(201)
        .extract()
        .response()
    .asString();

In Rest Assured given(), when(), then(), all these methods return an object of the same class that implements the RequestSpecfication interface. Since all the methods return the same object, it is possible to chain them.

How to Upload File in REST Assured?

Specify a file to upload to the server using multi-part form data uploading. It will assume that the control name is file and the mime-type is application/octet-stream. If this is not what you want please use an overloaded method.

Code

@Test
public void uploadFileExample()
{
    File fileToUpload = new File("file/path");

    RestAssured.baseURI = "https://reqres.in/";
    
    Response response = 
        given()
            .multiPart(fileToUpload)
        .when()
            .post("/uploadFile");

    Assert.assertTrue(response.asString().contains("Successfully Uplaoded"));
}

What is Resource in REST API?

The REST architecture treats any content as a resource. This content includes HTML pages, text files, images, videos, or dynamic business information. A REST Server gives users access to these resources and modifies them, and URIs or global IDs identify each resource.

What is Client Server Architecture?

The client-server architectural model defines how a server allocates resources and services to one or more clients. Server examples include web servers, mail servers, and file servers. So, the server carries out the request when the client requests a resource.

What is the Protocol used in REST?

A RESTful API uses existing HTTP methodologies defined by the RFC 2616 protocol, such as:

  • GET to retrieve a resource;
  • PUT to change the state of or update a resource, which can be an object, file or block;
  • POST to create that resource; and
  • DELETE to remove it.

What is JSON?

It is a text-based standard used to describe structured data based on JavaScript object syntax. JSON is frequently used in web applications to send data to clients and servers.

teststep banner