请放心表单身份验证:POST请求

时间:2018-06-14 07:09:53

标签: java rest rest-assured

我有一个登录表单,它使用POST请求将登录数据发送到服务器。我如何使用放心来测试此POST请求?

示例:

@Test
public void testLogin() {
    // @formatter:off
    given()
            .auth().form("testUser", "testPassword", new FormAuthConfig("/login", "username", "password").withCsrfFieldName("_csrf"))
            .filter(new ResponseLoggingFilter())
            .filter(new RequestLoggingFilter())
            .baseUri("https://...")
            .relaxedHTTPSValidation()
    .when()
            .get("/login")
    .then()
            .assertThat()
                .statusCode(200);
    // @formatter:on
}

当我执行此操作时,则没有发出端点/登录的POST请求,我可以在日志输出中看到两个GET请求:

Request method: GET
Request URI:    https://.../login
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
Cookies:        <none>
Multiparts:     <none>
Body:           <none>
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Language: en-US
Content-Type: text/html;charset=UTF-8
Date: Thu, 14 Jun 2018 12:53:41 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: LOGIN_SESSION=...; Path=/; Secure; HttpOnly
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Vcap-Request-Id: 12f31066-a5a5-495d-5427-7b1814b030d0
X-Xss-Protection: 1; mode=block
Transfer-Encoding: chunked
Server: Server

<html>
  <head>
    ...
  <body>
    ...
        <form enctype="application/x-www-form-urlencoded" method="POST" name="f" action="/login">
          <input type="hidden" name="_csrf" value="..."/>
          <div class="mod-textfield" data-js-textfield="data-js-textfield">
            <input type="text" id="1" required="required" name="username" value=""/>
            <span class="m-placeholder">Email</span>
          </div>
          <div class="mod-textfield" data-js-textfield="data-js-textfield">
            <input type="password" id="2" required="required" name="password"/>
            <span class="m-placeholder">Password</span>
          </div>
          <button type="submit" class="mod-button mod-button--fill" name="submit">Einloggen</button>
     ...
  </body>
</html>
Request method: GET
Request URI:    https://.../login
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
Cookies:        LOGIN_SESSION=...
Multiparts:     <none>
Body:           <none>
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Language: en-US
Content-Type: text/html;charset=UTF-8
Date: Thu, 14 Jun 2018 12:53:42 GMT
Expires: 0
Pragma: no-cache
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Vcap-Request-Id: 9f5ba2ab-8c8d-446a-6498-d1416fd2ba3b
X-Xss-Protection: 1; mode=block
Transfer-Encoding: chunked
Server: Server

<html>
  <head>
    ...
  <body>
        <form enctype="application/x-www-form-urlencoded" method="POST" name="f" action="/login">
          <input type="hidden" name="_csrf" value="..."/>
          <div class="mod-textfield" data-js-textfield="data-js-textfield">
            <input type="text" id="1" required="required" name="username" value=""/>
            <span class="m-placeholder">EMail</span>
          </div>
          <div class="mod-textfield" data-js-textfield="data-js-textfield">
            <input type="password" id="2" required="required" name="password"/>
            <span class="m-placeholder">Password</span>
          </div>
          <button type="submit" class="mod-button mod-button--fill" name="submit">Einloggen</button>
     ...
  </body>
</html>

如何在提交表单时告诉您放心执行POST请求?

编辑: 如果我添加withLoggingEnabled(),我可以看到POST已执行,但在POST之后执行了另一个GET,所以我看到的序列是GET - &gt; POST - &gt; GET ...

0 个答案:

没有答案
相关问题