测试重定向 url

Testing redirected url

这是对这个问题的补充:

Testing loging in with Behat

public function __construct()
{
    // Choose a Mink driver. More about it in later chapters.
    $driver = new GoutteDriver();
    $this->session = new Session($driver);

}

我尝试了一些东西:

    // if ( $this->session->getCurrentUrl() != UserController::APP_URL ) {

    $headers = $this->session->getResponseHeaders();

    print_r($headers);
    if ( empty($headers['Location']) && empty($headers['location']) ) {
            throw new Exception('Login failed: ' . json_encode($headers) );
    }

getCurrentUrl() return 而不是重定向到的 url。 (功能已实施且重定向有效)。

Headers 位置不存在。所以测试失败了,当它应该通过时。

怎么了?

问题出在 url 我正在 post- 这是错误的,首先完全错误,然后类似但仍然错误:

$this->session->getDriver()->getClient()
            ->request('POST', static::BASE_URL . '/user/registration', $this->getFormArray());

我没有post这段代码有问题,因为我认为它不相关。 很难看到错误 - 在单词 "registration" 之后没有斜线结尾。

但我使用的是 symfony 3,并且我已经为方法定义了路由

@Route("/user/registration/", name="registration")

结尾有斜杠。在浏览器中它起作用了,因为浏览器知道如何处理它。我什至不认为路由有什么区别——在我 4-5 年的 Web 开发过程中,是有斜杠还是没有斜杠,从来没有遇到过这样的问题。

所以路线不正确 - 没有重定向。

仍然找不到位置 header,因为现在页面已经重定向,但是

$this->session->getCurrentUrl()

现在是正确的。

Restler,一个支持使用 Behat 和 Guzzle 进行行为驱动 API 测试的微框架。

它包括 Redirect class 用于处理重定向。但是,要测试重定向,您需要在响应中查找 Location header,例如

Scenario: Test for Location in the header
  When I request "/some_url"
  Then the response "Location" header should be "/some_other_url"