【发布时间】:2021-10-09 00:34:34
【问题描述】:
我目前正在成功拦截authorization.oauth2 请求,但我想做的是向我的本地主机发出请求(应用程序中不存在的路由)。但是,当我这样做时,我会收到 404 错误。
有没有办法拦截到一个不存在的路由的请求?
cy.intercept('*authorization.oauth2*', req => {
cy.request('POST', '/auth/intercept/handle');
req.reply('');
});
// This route doesn't exist:
cy.intercept('POST', '/auth/intercept/handle', req => {
// Handle stuff
});
这是请求的输出:
cy.request() failed on:
https://localhost:4200/auth/intercept/handle
The response we received from your web server was:
> 404: Not Found
This was considered a failure because the status code was not 2xx or 3xx.
If you do not want status codes to cause failures pass the option: failOnStatusCode: false
-----------------------------------------------------------
The request we sent was:
Method: POST
URL: https://localhost:4200/auth/intercept/handle
Headers: {
"Connection": "keep-alive",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/8.1.0 Chrome/89.0.4328.0 Electron/12.0.0-beta.14 Safari/537.36",
"accept": "/",
"accept-encoding": "gzip, deflate",
"content-length": 0
}
-----------------------------------------------------------
The response we got was:
Status: 404 - Not Found
Headers: {
"x-powered-by": "Express",
"access-control-allow-origin": "*",
"content-security-policy": "default-src 'none'",
"x-content-type-options": "nosniff",
"content-type": "text/html; charset=utf-8",
"content-length": "161",
"date": "Tue, 03 Aug 2021 19:57:39 GMT",
"connection": "keep-alive"
}
Body: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /auth/intercept/handle</pre>
</body>
</html>
Because this error occurred during a after each hook we are skipping the remaining tests in the current suite: Roles Page
【问题讨论】:
-
问题似乎出在示例第 2 行的请求上,而不是拦截。当您请求一个不存在的端点时,404 响应是预期的行为。你希望发生什么?
-
我希望拦截器捕获请求并运行回调。
-
我在文档中找到了这个:
cy.intercept() cannot be debugged using cy.request()! Cypress only intercepts requests made by your front-end application.
标签: javascript cypress end-to-end