Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST Assured and multiple posts

I'm trying to use REST assured to test my login/logout feature. Is it possible to have a REST assured test that posts to login then posts to logout? If not, how can I test it properly?

like image 563
mattklamp Avatar asked Oct 04 '22 11:10

mattklamp


2 Answers

Just send two post() with one assert()/expect() :

import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.Matchers.*;
import static com.jayway.restassured.RestAssured.*;

@Test
public void loginAndLogout(){
    final String login = randomLogin();
    // First post to login()
    given()
    .queryParam("login", login)
    .queryParam("password", randomPassword())
    .when().post("/login/");    

    // Second post to logout() with an assert
    expect().statusCode(200)
    .given()
    .when().post("/logout/");   
}
like image 161
vaugham Avatar answered Oct 07 '22 18:10

vaugham


You can try

expect().statusCode(HttpStatus.SC_OK)
    .given()
    .parameters("user", user, "password", URL)
    .cookie("cookie_name", "cookie_value")
    .post("/someURL");

Also there is a rest-assured auth call.

See the documentation or the examples

like image 36
mihai.ciorobea Avatar answered Oct 07 '22 20:10

mihai.ciorobea