Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAssured - passing list as QueryParam

I have a REST-service that takes in a number of query-params, amongst other things a list of strings. I use RestAssured to test this REST-service, but I am experiencing some problems with passing the list to the service.

My REST-service:

@GET
@Consumes(Mediatyper.JSON_UTF8)
@Produces(Mediatyper.JSON_UTF8)
public AggregerteDataDTO doSearch(@QueryParam("param1") final String param1,
                                  @QueryParam("param2") final String param2,
                                  @QueryParam("list") final List<String> list) {

My RestAssured test:

public void someTest() {
    final  String url = BASE_URL + "/search?param1=2014&param2=something&list=item1&list=item2";

    final String json = given()
            .expect()
            .statusCode(200)
            .when()
            .get(url)
            .asString();

When I print the url, it looks like this:

http://localhost:9191/application/rest/search?param1=2014&param2=something&list=item1&list=item2

When I try this url in my browser, the REST-service correctly gets a list containing 2 elements. But, when run through my RestAssured-test, only the latter of the params are noticed, giving me a list of 1 element (containing "item2").

like image 830
Tobb Avatar asked Jul 08 '15 12:07

Tobb


2 Answers

You can also try the below method

RequestSpecification requestSpecifications = RestAssured.given();

//r.parameters()

Map<String , Object > map = new HashMap<String,Object>();
map.put("param1", 2014);
map.put("param2", "something");
List<String> paramList = new ArrayList<String>();
map.put("list",paramList );

final String json =  requestSpecifications.parameters(map).
    when().
    get("/search").
    then().    
    statusCode(200).
    extract().
    body().asString(); 
like image 40
Karan Avatar answered Sep 28 '22 01:09

Karan


You should upgrade REST Assured to the latest version since I believe that this was a bug in older versions. You could also specify parameters like this:

final String json = 
given().
        param("param1", 2014).
        param("param2", "something").
        param("list", "item1", "item2").
when().
        get("/search").
then().
       statusCode(200).
extract().
          body().asString();  
like image 124
Johan Avatar answered Sep 28 '22 03:09

Johan