Rest-Assured表单身份验证不起作用

时间:2017-01-24 18:13:13

标签: java rest rest-assured web-api-testing

我正在尝试使用我的应用程序中的一个保证,问题是我无法进行身份验证。

以下是我正在尝试的代码

baseURI = "http://localhost:8080/api";
given()
        .auth().form("admin", "admin", new FormAuthConfig("/authentication/", "j_username", "j_password"))
.when()
        .get("/formAuth")
        .then()
        .log().all()
        .statusCode(200);

我也试过以下选项,没有任何效果 以下代码

401错误
form("admin", "admin", new FormAuthConfig("/", "j_username", "j_password"))

以下选项不起作用,显示无法解析登录页面。检查登录页面上的错误或指定FormAuthConfig。错误

.auth().form("admin", "admin", formAuthConfig().withAutoDetectionOfCsrf())

以下是表单的代码

<form class="form ng-pristine ng-valid" role="form" ng-submit="login($event)">
                <div class="form-group">
                    <label for="username" translate="global.form.username" class="ng-scope">Login</label>
                    <input type="text" class="form-control ng-pristine ng-valid ng-touched" id="username" placeholder="Your login" ng-model="username" autocomplete="off">
                </div>
                <div class="form-group">
                    <label for="password" translate="login.form.password" class="ng-scope">Password</label>
                    <input type="password" class="form-control ng-pristine ng-untouched ng-valid" id="password" placeholder="Your password" ng-model="password">
                </div>
                <div class="form-group">
                    <label for="rememberMe">
                        <input type="checkbox" id="rememberMe" ng-model="rememberMe" checked="" class="ng-pristine ng-untouched ng-valid">
                        <span translate="login.form.rememberme" class="ng-scope">Automatic Login</span>
                    </label>
                </div>
                <button type="submit" class="btn btn-primary ng-scope" translate="login.form.button">Authenticate</button>
            </form>

以下是使用 PostMan

的捕获输出
URL: http://localhost:8080/api/authentication?cacheBuster=1485280599118
parameter:  j_username:admin
j_password:admin

2 个答案:

答案 0 :(得分:0)

v3中似乎有些东西被破坏了。今天我从com.jayway.restassured 2.8.0转到io.restassured 3.0.5时遇到同样的问题。我刚刚回滚升级并再次运行:/

答案 1 :(得分:0)

我相信您需要提供有关如何进行身份验证呼叫的更多信息。

定义new FormAuthConfig("/authentication/", "j_username", "j_password")时,是在告诉Rest Assured它应该使用最后两个参数作为表单参数对/authentication端点进行POST调用。

通常,HTML <form>元素具有一个action属性,该属性指示向其发出POST请求的端点,而name标签中的<input>属性指示该属性。应该使用的参数形式的键。

在您的情况下,HTML表单没有提供太多有关如何进行呼叫的信息,因为它可能是使用Javascript处理的。

顺便说一句,在定义FormAuthConfig时,启用日志记录通常很有用:

given().auth()
  .form(
    "admin",
    "admin",
    new FormAuthConfig("/authentication", "j_username", "j_password")
      .withLoggingEnabled())
  // ...