如何将传入的HTTP请求(响应)与赛普拉斯保持固执
如何存根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:
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')
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定没有看到整个测试,但是您是否正在使用
cy.request()
在测试中向Microsoftonline发出帖子?如果是这样,您不能使用
cy.intercept()
来捕获它,只会捕获该应用程序的请求。但是,您可以将
。然后将()
附加到cy.request()
等待响应。另外,在此代码
req.reply()
和req.continue()
您既固定(带有回复)并继续使用服务器(继续)动作。您只想做一个或另一个。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 thecy.request()
to wait for the response.Also in this code
req.reply()
andreq.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.