Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the "CURL" operation from each Rest Assured test and print in the console when not pass the test

I need Get the "CURL" operation from each Rest Assured test and print in the console when not pass the test. It's possible?

For axample, convert:

 given().relaxedHTTPSValidation().
   auth().basic("name", "password").
   param("grant_type","client_credentials").
 when().
   post("https://test_url/oauth/token").
 then().
   statusCode(200);

to

curl --user name:passwoed -k -X POST \
https://test_url/oauth/token \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-d grant_type=client_credentials
like image 887
Renzo Parente Avatar asked Dec 24 '22 01:12

Renzo Parente


2 Answers

Include the following dependencies.

Maven:

<dependency>
  <groupId>com.github.dzieciou.testing</groupId>
  <artifactId>curl-logger</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.6</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.9</version>
</dependency>

Once configured use the following piece of code to log the curl request in the console output.

package com.api.test;

import static org.hamcrest.CoreMatchers.is;

import org.json.JSONObject;
import org.testng.annotations.Test;

import com.sample.model.EnrollEmail;
import com.github.dzieciou.testing.curl.CurlLoggingRestAssuredConfigFactory;

import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;

/**
 * @author vamsiravi
 *
 */
public class Sample {

    @Test(enabled=true)
    public void sampleAPITest() {

        RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig();  

        EnrollEmail email = new EnrollEmail();
        email.setEmailAddress("[email protected]");
        RestAssured.given().config(config).contentType(ContentType.JSON).body(email).when().post("https://jsonplaceholder.typicode.com/posts").then()
        .assertThat()
        .statusCode(201);   

    }
}

Output :

[main] DEBUG curl - **
curl 'https://jsonplaceholder.typicode.com/posts' 
-H 'Accept: */*' 
-H 'Content-Type: application/json; charset=UTF-8' 
-H 'Host: jsonplaceholder.typicode.com' 
-H 'Connection: Keep-Alive' 
-H 'User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_181)' 
--data-binary '{"emailAddress":"[email protected]"}' 
--compressed 
-k 
-v**

PASSED: sampleAPITest
like image 122
Vamsi Ravi Avatar answered Dec 25 '22 14:12

Vamsi Ravi


An alternative is to parse the output of the log().all() as seen in my article.

Note: I've not shared the code here, as it's Apache 2.0 licensed, which is different to Stack Overflow's default licensing.

like image 30
jamietanna Avatar answered Dec 25 '22 14:12

jamietanna