How do we pass multiple headers at rest?

I am new to security and Java, I am trying to make a very simple test to check the response, 200 for the API. can you tell me what i need to modify below script to pass multiple Id, Key and ConId headers?

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"));
    }

}
+4
source share
4 answers

The easiest way to add multiple titles is to simply repeat .header(headername,headervalue)several times after.given()

given().
  header("Id", "abc").
  header("name","name").
  header("","")
  ...

You can find different ways to pass headers using the REST-Assured framework in your test suite in github link .

Edit:

To check the status of a response in Rest-Assured:

expect().statusCode(200),log().ifError().given()...... 

, github link

+6

    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);
+2

Headers() RestAssured, .

+1

, :

@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"));
    }
0

Source: https://habr.com/ru/post/1625420/


All Articles