简体   繁体   中英

How to configure java restassured library not to reuse the cookies

Am trying the reuse the restassured get call in the testng api tests, but the restassured instance in using the cookies received from the previous response. Tried RestAssured.reset(); but this doesn't help in flushing the cookies we got from earlier request/response.

Reason for this question - As the get endpoint behaves differently if there is a session cookie exist on the request.

@Test // TestNG test
public void test_1(){

    //Set Cookie
    Cookie cookie = new Cookie.Builder("COOKIENAME","COOKIEVALUE").setDomain("*.com").setPath("/").setExpiryDate(SOMELATERDATE).build();

    RestAssured.baseURI = https://ENV_URL;
    Response response = RestAssured.given().log().all()
        .cookies(new Cookies(cookie)).when().get("/END_POINT").then().extract().response().prettyPeek();

    RestAssured.reset();
}

@Test // TestNG test
public void test_2(){

    //Set Cookie
    Cookie cookie = new Cookie.Builder("COOKIENAME", "COOKIEVALUE").setDomain("*.com").setPath("/").setExpiryDate(SOMELATERDATE).build();

    RestAssured.baseURI = https://ENV_URL;
    // Still Reuses the cookie received from previous response
    Response response = RestAssured.given().log().all()
        .cookies(new Cookies(cookie)).when().get("/END_POINT").then().extract().response().prettyPeek();

    RestAssured.reset();
}

Use CookieFilter. If you want to exclude the a particular cookie you can use the following code.

    @Test 
    public void sampletest(){

        CookieFilter cookieFilter = new CookieFilter(); 
        Response response = RestAssured.given().
         cookie("foo", "bar").
         filter(cookieFilter). // Reuse the same cookie filter
                      // if "foo" is stored in cookieFilter it won't be applied because it's already applied explicitly
 expect().
         statusCode(200).
 when().
         get("/y");

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM