0

我有一个本地端点来检查我是否已登录,如果是,它会生成令牌并将我重定向到另一个 API 端点。我想测试重定向但是,我以以下错误结束:

cy.visit() failed trying to load:

http://getmantaportal.test/authorize/

The response we received from your web server was:

  > 403: Forbidden

This was considered a failure because the status code was not 2xx.

This http request was redirected '1' time to:

  - 302: https://example.com/api/?token=eyJ0eXAiOiJKV1QiL

这是我的代码:

 it('Redirect to Manta Edge if already logged in.', function () {

    // Login first.
    // [...]

    // Go to the endpoint and get redirected immediately.
    cy.visit('/authorize/')
    cy.url().should('contain', '/api/?token=')

  })

非常感谢提前。

4

1 回答 1

1

使用嵌套cy.request调用,您需要知道所有要通过身份验证的重定向。请参阅下面的假设示例。

cy.request({
    method: "POST",
    url: `YOUR FIRST URL`,
    headers: {},
    body: {
      username: YOUR_USERNAME,
      password: YOUR_PASSWORD
    },
  }).then(function (response) {
    var body = response.body;
    cy.request({
      method: "GET",
      url: `YOUR SECOND URL`,
      auth: {
        bearer: body.access_token,
      },
    }).then(function (response) {

      cy.request({
        method: "POST",
        url: `YOUR THIRD URL IF EXISTS`,
        auth: {
          bearer: body.access_token,
        },
        
      }).then(function (response) {
        response.headers
        // If necessary, get the cookies to finish the authentication
    });
  });
于 2021-08-12T01:08:26.273 回答