mattklamp
mattklamp

Reputation: 293

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?

Upvotes: 5

Views: 8352

Answers (4)

Nitin mahapure
Nitin mahapure

Reputation: 1

Also you try this:

Create your JSON file with xyzjson name and keep your post payload data in that file and use below code.

Response rep  = given()
                  .headers
                  .(headers)
                  .accept(contentType.json)
                  .body (xyzjson)
                .when()
                  .post(someURL);

Assert.assertTrue(rep.StatusCode() == HttpStatus.SC_Ok);

Upvotes: 0

Dave Gordon
Dave Gordon

Reputation: 256

Does your login api call result in some sort of authentication token that is reused in subsequent requests? If so, I see these as separate rest assured calls to test it fully.

(Login Focus)

  1. Issue a RestAssured call to the /login api. Save the returned authentication token.
  2. Using the saved token, issue a RestAssured call to another api in your system that requires the authentication in step one. This confirms the authentication token works.

(Logout Focus)

  1. Issue a RestAssured call to the /logout api using the saved token.
  2. Repeat step two and confirm this request now fails as the login token is no longer valid after step three.

Upvotes: 0

vaugham
vaugham

Reputation: 1851

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

Upvotes: 4

mihai.ciorobea
mihai.ciorobea

Reputation: 741

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

Upvotes: 1

Related Questions