如何将传入的HTTP请求(响应)与赛普拉斯保持固执

发布于 2025-02-13 01:43:57 字数 2273 浏览 0 评论 0原文

如何存根HTTP请求的响应?

让我用我现在拥有的代码解释一下:

Cypress.Commands.add("FakeLoginWithMsal", (userId) => {
   
    cy.intercept('**/oauth2/v2.0/token', (req) => {
        
        req.reply({
            token_type: "Bearer",
            expires_in: 3795,
            access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
            })

        req.continue((res) => {

        })
    })

使用此代码,我试图为以下请求暂停响应:

”输入图像描述在这里”

,但它仍然给出以下错误,我可以理解该存根不起作用:

我们试图向此URL提出HTTP请求,但是请求 失败没有响应。

我已经尝试了已经不同的柏树拦截方法,但我无法工作。


我什至无法使用以下内容拦截/令牌端点:

   cy.intercept({
    method: 'POST',
    url: 'https://login.microsoftonline.com/xx-xx-xx-xx-/oauth2/v2.0/token',
}).as('apiCheck')


​ @fody再次感谢您的回应。实际上,我正在尝试将所有MSAL端点固化。它不是testscript,而是登录命令。

在这里是:

Cypress.Commands.add("FakeLoginWithMsal", (userId) => {
    cy.intercept('GET', '**/authorize', { fixture: 'authorizeStub.json' })
    cy.intercept('GET', '**/v2.0/.well-known/openid-configuration', { fixture: 'openidConfigurationStub.json' })

cy.request({
    method: 'POST',
    url: 'https://login.microsoftonline.com/xxxxx/oauth2/v2.0/token',
    body: {
        grant_type: "password",
        client_id: "xxxxxxxxxxxxx",
        client_secret: "xxxxxxxxxxx",
        scope: "api://xxxxxxxxxxxxxx/Cp.Use",
        username: "[email protected]",
        password: "xxxxx",
    },
    form: true,
}).then(response => {
    cy.log(JSON.stringify(response))
    cy.intercept('response', { fixture: 'tokenStub.json' })

    })

这些是3个端点,即:

get: /prolemize(用固定装置固定)

get: /openID-configuration(带固定装置的固定)

帖子: /token->这篇文章有一个响应,其中有访问权限。 我需要存根。

​HTTP请求”(请参阅​​附件)。这种传入的HTTP响应正是我想要在固定装置中存根的。

How can I stub a response of a HTTP request?

Let me explain it with my code I have now:

Cypress.Commands.add("FakeLoginWithMsal", (userId) => {
   
    cy.intercept('**/oauth2/v2.0/token', (req) => {
        
        req.reply({
            token_type: "Bearer",
            expires_in: 3795,
            access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
            })

        req.continue((res) => {

        })
    })

With this code I am trying to stub the response for the following request:

enter image description here

But it still gives the following error, where I can understand the stub did not work:

We attempted to make an http request to this URL but the request
failed without a response.

I've tried already different intercept methods of cypress but I couldn't get worked.


I even can't intercept the /token endpoint with the following:

   cy.intercept({
    method: 'POST',
    url: 'https://login.microsoftonline.com/xx-xx-xx-xx-/oauth2/v2.0/token',
}).as('apiCheck')

enter image description here


Update:
@Fody Thankyou vey much (again) for you respond. Actually I am trying to stub all the MSAL endpoints. It is not a testscript, but a login command.

Here it is:

Cypress.Commands.add("FakeLoginWithMsal", (userId) => {
    cy.intercept('GET', '**/authorize', { fixture: 'authorizeStub.json' })
    cy.intercept('GET', '**/v2.0/.well-known/openid-configuration', { fixture: 'openidConfigurationStub.json' })

cy.request({
    method: 'POST',
    url: 'https://login.microsoftonline.com/xxxxx/oauth2/v2.0/token',
    body: {
        grant_type: "password",
        client_id: "xxxxxxxxxxxxx",
        client_secret: "xxxxxxxxxxx",
        scope: "api://xxxxxxxxxxxxxx/Cp.Use",
        username: "[email protected]",
        password: "xxxxx",
    },
    form: true,
}).then(response => {
    cy.log(JSON.stringify(response))
    cy.intercept('response', { fixture: 'tokenStub.json' })

    })

These are 3 endpoints, namely:

GET: /authorize (stubbed with a fixture)

GET: /openid-configuration (stubbed with a fixture)

Post: /token --> This POST has a response and there inside is the accesstoken. This response I need to stub.

enter image description here

And I guess that this response is a "incoming HTTP request" (see attachments). This incoming http response is exactly what I want to stub in a fixture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-20 01:43:57

我不确定没有看到整个测试,但是您是否正在使用cy.request()在测试中向Microsoftonline发出帖子?

如果是这样,您不能使用cy.intercept()来捕获它,只会捕获该应用程序的请求。

但是,您可以将。然后将()附加到cy.request()等待响应。

cy.request({
  method: 'POST',
  url: 'https://login.microsoftonline.com/.../oauth2/v2.0/token',
})
.then(response => {
  // handle response
  
})

另外,在此代码req.reply()req.continue()您既固定(带有回复)并继续使用服务器(继续)动作。您只想做一个或另一个。

cy.intercept('**/oauth2/v2.0/token', (req) => {
        
  req.reply({
    token_type: "Bearer",
    expires_in: 3795,
    access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
  })

  req.continue((res) => {

  })
})

I'm not sure without seeing the whole test, but are you are issuing the POST to microsoftonline from within the test using cy.request()?

If so, you can't use cy.intercept() to catch it, only requests from the app will be caught.

But you can append a .then() to the cy.request() to wait for the response.

cy.request({
  method: 'POST',
  url: 'https://login.microsoftonline.com/.../oauth2/v2.0/token',
})
.then(response => {
  // handle response
  
})

Also in this code req.reply() and req.continue() you are both stubbing (with reply) and continuing to the server (with continue), which are opposite actions. You would only want to do one or the other.

cy.intercept('**/oauth2/v2.0/token', (req) => {
        
  req.reply({
    token_type: "Bearer",
    expires_in: 3795,
    access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
  })

  req.continue((res) => {

  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文