Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to Rest-Assured

Can someone help me in this scenario:

When I invoke this service, http://restcountries.eu/rest/v1/, I get couple of countries information.

But when I want to get any specific country information like say Finland, I invoke the web service as http://restcountries.eu/rest/v1/name/Finland to get the country related info.

To automate the above scenario, how can I parameterize the country name in Rest-Assured? I tried below, but doesn't help me.

RestAssured.given().
                    parameters("name","Finland").
            when().
                    get("http://restcountries.eu/rest/v1/").
            then().
                body("capital", containsString("Helsinki"));
like image 949
Uday Avatar asked Sep 09 '15 09:09

Uday


3 Answers

You can define a request cpecification (say in a @Before) first:

RequestSpecification requestSpecification = new RequestSpecBuilder()
    .setBaseUri (BASE_URL)
    .setBasePath (BASE_PATH)
    .addPathParam(
        "pathParamName", "pathParamValue",
        "anotherPathParamName", "anotherPathParamValue")
    .addQueryParam(
        "queryparamName", "queryParamValue",
        "anotherQueryParamName", "anotherQueryParamValue")
    .setAuth(basic(USERNAME, PASSWORD))
    .setContentType(ContentType.JSON)
    .setAccept(ContentType.JSON)
    .log(LogDetail.ALL)
    .build();

and then reuse it multiple times as follows:

given()
    .spec(requestSpecification)
    .get("someResource/{pathParamName}/{anotherPathParamName}")
.then()
    .statusCode(200);

You can rebuild the request specification wherever you want. And if some changes will happen you can easily change your params names in one place.

I hope it helps.

like image 113
Julian Kolodzey Avatar answered Oct 24 '22 00:10

Julian Kolodzey


As the documentation explains:

REST Assured will automatically try to determine which parameter type (i.e. query or form parameter) based on the HTTP method. In case of GET query parameters will automatically be used and in case of POST form parameters will be used.

But in your case it seems you need path parameter instead query parameters. Note as well that the generic URL for getting a country is https://restcountries.com/v2/name/{country} where {country} is the country name.

Then, there are as well multiple ways to transfers path parameters.

Here are few examples

Example using pathParam():

// Here the key name 'country' must match the url parameter {country}
RestAssured.given()
        .pathParam("country", "Finland")
        .when()
            .get("https://restcountries.com/v2/name/{country}")
        .then()
            .body("capital", containsString("Helsinki"));

Example using variable:

String cty = "Finland";

// Here the name of the variable (`cty`) have no relation with the URL parameter {country}
RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/name/{country}", cty)
        .then()
            .body("capital", containsString("Helsinki"));

Now if you need to call different services, you can also parametrize the "service" like this:

// Search by name
String val = "Finland";
String svc = "name";

RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Helsinki"));


// Search by ISO code (alpha)
val = "CH"
svc = "alpha"

RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Bern"));

// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"

RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Sofia"));

After you can also easily use the JUnit @RunWith(Parameterized.class) to feed the parameters 'svc' and 'value' for a unit test.

like image 37
рüффп Avatar answered Oct 24 '22 00:10

рüффп


You are invoking the GET call incorrectly.

The parameters("name","Finland") will be converted only as query parameter or form parameter for GET and POST/PUT respectively

RestAssured.when().
                    get("http://restcountries.eu/rest/v1/name/Finland").
            then().
                body("capital", containsString("Helsinki"));

is the only way to do. Since it's a java DSL, you can construct the URL all by yourself and pass it to get() if required

If the URL with a GET request had to fetch the same details been like :

http://restcountries.eu/rest/v1?name=Finland,

The your DSL would be something like:

RestAssured.given().parameters("name","Finland").
                when().
                        get("http://restcountries.eu/rest/v1")

When you have a GET request, your parameters transform into queryParameters.

More info from this link: https://code.google.com/p/rest-assured/wiki/Usage#Parameters

like image 1
Karthik R Avatar answered Oct 24 '22 01:10

Karthik R