I am new to rest-assured and Java, I am trying to do a very basic test of checking the response is 200 ok for API.
can you any one please tell me what do I need to change in the below script in order to pass multiple headers Id, Key and ConId?
import org.junit.Test;
import com.jayway.restassured.*;
import com.jayway.restassured.http.ContentType;
import static org.hamcrest.Matchers.*;
import static com.jayway.restassured.RestAssured.*;
public class APIresponse
{
public static void main(String[] args)
{
APIresponse apiresponse = new APIresponse();
apiresponse.response();
}
@Test
public void response ()
{
baseURI="http://testme/api/";
given().
header("Id", "abc").
param("Key", "NuDVhdsfYmNkDLOZQ").
param("ConId", "xyz").
when().
get("/uk?Id=DT44FR100731").
then().
contentType(ContentType.JSON).
body("response.code", equalTo("200"));
}
}
Simplest way to add multiple headers is to just repeat .header(headername,headervalue)
multiple times after .given()
given().
header("Id", "abc").
header("name","name").
header("","")
...
You can find different ways of passing headers using REST-Assured framework in its test suite at this github link.
Edit:
To verify response status in Rest-Assured:
expect().statusCode(200),log().ifError().given()......
or pick an example of how you want to test response header from this github link
you can also create and add Map Object of multiple headers as below
Header h1= new Header("Accept", "*/*");
Header h2 = new Header("Accept-Language", "en-US");
Header h3 = new Header("User-Agent", "Mozilla/5.0");
List<Header> list = new ArrayList<Header>();
list.add(h1);
list.add(h2);
list.add(h3);
Headers header = new Headers(list);
request.headers(header);
Or you can use Headers() from RestAssured which support you to add multiple headers at the same time to request.
Headers description
Replace like below:
@Test
public void response ()
{
baseURI="http://testme/api";
given()
.header("Id", "abc")
.param("Key", "NuDVhdsfYmNkDLOZQ")
.param("ConId", "xyz")
when()
.get("/uk?Id=DT44FR100731")
then()
.contentType(ContentType.JSON)
.and()
.body("response.code", equalTo("200"));
}