Cypress intercept 未拦截来自服务器的请求。

huangapple go评论49阅读模式
英文:

Cypress intercept is not intercepting a request sent from the server

问题

我有一段代码,其中一个端点(https://localhost:3000/endpoint/A)触发一个方法,这个方法内部调用另一个端点(https://localhost:3000/endpoint/B)。现在,第二个端点(https://localhost:3000/endpoint/B)的标头必须被修改,因为有一些强制性的自定义标头。

因此,我尝试使用以下代码来拦截第二个端点并修改标头,但是Cypress测试没有拦截此请求。有人可以提供一些输入或告诉我我在这里做错了什么。

cy.intercept('POST', '/endpoint/B', (req) => {
  req.headers['custom-header-one'] = "ch1"
  req.headers['custom-header-two'] = "ch2"
  cy.log(JSON.stringify(req.headers));
})

编辑 1:

我进行的第二次调用是第三方端点,它在节点服务器上执行。

从代码中删除了HTTP,因为它无论是否带有HTTP都不起作用。

英文:

I have a code where an endpoint(https://localhost:3000/endpoint/A) triggers a method and this method internally calls another endpoint(https://localhost:3000/endpoint/B). So now the headers of second endpoint (https://localhost:3000/endpoint/B) have to be modified as there are some mandatory custom headers.

So i tried using the below code to intercept second endpoint and modify headers but the cypress test is not intercepting this request. Can someone throw some inputs or let me know what is the mistake i am doing here.

  cy.intercept('POST', '/endpoint/B', (req) => {
    req.headers['custom-header-one'] = "ch1"
    req.headers['custom-header-two'] = "ch2"
    cy.log(JSON.stringify(req.headers));
  })

Edit 1 :

The second call I am doing is a third party endpoint and it happens on the node server.

Removed the http from the code as its not working with or without http

答案1

得分: 2

这可能是因为您在与“localhost”对抗时使用了“https://”,但应该是“http”。

由于小型匹配是纯粹的模式匹配,对于这种类型的错误不会进行自动更正。

您也可以使用通配符:

cy.intercept('POST', '**/endpoint/B', (req) => {
英文:

This is probably just because you have use https:// against localhost, but it should be http://.

Since the mini-match is pure pattern matching, there would not be any auto-correction for this type of error.

You can also just wildcard it

 cy.intercept('POST', '**/endpoint/B', (req) => {

huangapple
  • 本文由 发表于 2023年2月16日 12:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定