【问题标题】:Why is it OK but not KO when username and password are incorrect?为什么用户名和密码错误时正常但不KO?
【发布时间】:2016-01-22 03:22:54
【问题描述】:

我是 gatling 的新用户,我想测试一个 Web 应用程序的登录场景。我遇到了以下问题,我用错误的密码编写了 scala 脚本来测试登录场景,但我发现最终结果不是我预期的失败,发生了什么问题?

我的 scala 程序:

package cnblogsCase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class MueasSimulation extends Simulation{
  val httpConf = http.baseURL("http://localhost:8080")
  var scn = scenario("Search mueas home page")
          .exec(http("Redirect Login").get("/").check(currentLocation.saveAs("post_url")))
          .pause(10 seconds)
          .exec(http("Try login").post("${post_url}")
              .formParam("username", "shihuc")
              .formParam("password", "123456").check(status.is(200)))
          .pause(10 seconds)
  setUp(scn.inject(atOnceUsers(2)).protocols(httpConf))
}

而且,我的 Web 应用程序中登录的表单部分是这样的:

<form method="post" action="/login">  
        <input type="text" required="required" name="username" placeholder="ID or Email-address">
        <input type="password" required="required" name="password" placeholder="Password">
        <div class="remfog">
            <div class="rem">
                <label>
                    <input type="checkbox" name="remember-me" value="true" style="margin-top:10px">
                    Remember Me
                </label>
            </div>
            <div class="fog">
                <a title="Reset password" href="/password/forgot">Forgot Password?</a>
            </div>
        </div>
        <input class="csubmit" type="submit" value="Log in">    
    </form>

我的预期结果是失败,因为scala脚本中的用户名和密码不正确,但实际上是可以的,不是KO。请检查log 文件。我的加特林版本是 2.1.7。

【问题讨论】:

  • 当您提供不正确的凭据时,您真的可以确认您正在测试的 web 应用程序返回的不是“200 OK”吗?并非每个 web 应用程序都是 RESTful 无处不在...
  • 感谢您的反馈!在这里,我确定我的 web APP 是 RESTful 的。

标签: scala gatling


【解决方案1】:

作为explained in the doc,如果您未指定,Gatling 会自动添加对 HTTP 状态的检查。

检查您的日志,您的应用程序回复 302/Found 和重定向回登录页面的位置。 这种行为是错误的。在这种情况下,HTTP 响应应该使用403/Forbidden status code

最好的解决方案是修复应用程序。 解决方法是为页面中可能出现的错误消息添加显式检查。

【讨论】:

  • 感谢您的信息。但我不同意你的“这种行为是错误的。在这种情况下,HTTP 响应应该使用 403/Forbidden 状态码。”我的 web 应用是基于 spring-boot + spring-security,spring-security 会检查用户名和密码,如果不正确,则将 http 请求重定向到登录页面,它不会响应 403。
  • @shihuc 这并不意味着 spring-security/你如何使用它是正确的。它绝对不是 RESTful 的。无论如何,你有你的答案:仅仅依靠 HTTP 状态对于你的应用程序行为是不够的,你必须检查正文中的错误消息。
  • 我检查了gatling的日志,然后发现scala中的检查不合理,我将“status.is(200)”更改为“currentLocation.is() ",那么结果就是我预期的 KO。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
相关资源
最近更新 更多