REST保证和多个帖子

问题描述 投票:4回答:4

我正在尝试使用REST保证测试我的登录/注销功能。是否有可能有一个REST保证测试发布登录然后发布到注销?如果没有,我该如何正确测试?

rest rest-assured
4个回答
1
投票

你可以试试

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

还有一个确定的auth呼叫。

参见documentationexamples


1
投票

只需使用一个assert()/ expect()发送两个post():

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

0
投票

您的登录api调用是否会导致在后续请求中重用的某种身份验证令牌?如果是这样的话,我认为这些是单独的放心电话,可以完全测试它。

(登录焦点)

  1. 向/ login api发出RestAssured调用。保存返回的身份验证令牌。
  2. 使用保存的令牌,向系统中需要在第一步中进行身份验证的另一个api发出RestAssured调用。这确认了身份验证令牌的工作原理。

(退出焦点)

  1. 使用保存的令牌向/ logout api发出RestAssured调用。
  2. 重复步骤2并确认此请求现在失败,因为登录令牌在第3步之后不再有效。

0
投票

你试试这个:

使用xyzjson名称创建JSON文件,并将后有效内容数据保存在该文件中,并使用下面的代码。

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

Assert.assertTrue(rep.StatusCode() == HttpStatus.SC_Ok);
© www.soinside.com 2019 - 2024. All rights reserved.