赛普拉斯使用不同响应的相同端点(测试 http 竞争条件)
cypress use same endpoint with different response (testing http race condition)
我尝试拦截两个相似的请求,但在每个响应之间延迟两个不同的响应。
- 向 /endpoint 发送第一个请求,响应延迟 3000 毫秒
- 对同一 /endpoint 的第二次请求,响应延迟 1000 毫秒
两个请求都有不同的响应,这里很难覆盖拦截器
cy.intercept('POST', '/validate', (req) => {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
})
cy.intercept('POST', '/validate', (req) => {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
})
在此处查看 Gleb Bahmutov 的回答
使用 times
选项限制拦截将捕获的调用数。
但请注意,最后添加的截距会先检查,因此您可能需要颠倒顺序。
cy.intercept({method: 'POST', url: '/validate', times: 1}, (req) => {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
})
cy.intercept({method: 'POST', url: '/validate', times: 1}, (req) => {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
})
示例应用程序
<script>
setTimeout(() => {
fetch('/validate', { method: 'POST'})
.then(res => res.json())
.then(res => console.log('1st', res))
}, 100)
setTimeout(() => {
fetch('/validate', { method: 'POST'})
.then(res => res.json())
.then(res => console.log('2nd', res))
}, 200)
</script>
控制台按预期顺序输出,夹具数据不同
2nd {data: 'valid'}
1st {data: 'invalid'}
相似的请求意味着您可以区分它们,所以通过请求中的任何 属性 来区分它们。
cy.intercept('POST', '/validate', (req) => {
if (req.body.any.property.only.of.my.first.request === true) {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
} else {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
}
})
您可以使用 Gleb 的 example here 作为起点。我们将使用一个超出拦截范围的变量来跟踪请求。
it('some test', () => {
let requestCount = 0;
cy.intercept('POST', '/validate', (req) => {
requestCount++;
req.reply({
delay: requestCount === 1 ? 1000 : 3000,
fixture: requestCount === 1 ? 'validData.json' : 'invalidData.json'
});
});
// rest of test
})
如果提供的三元组不能实现您的目标,您可以修改您的逻辑以return不同的延迟/装置。
您可以按照您希望应用它们的顺序提供一组 routeHandlers
it('test the same route multiple times', () => {
let request = 0;
const replies = [
{delay: 3000, fixture: 'invalidData.json'},
{delay: 1000, fixture: 'validData.json'},
]
cy.intercept('POST', '/validate', (req) => {
req.reply(replies[request++])
});
cy.visit(...);
})
我尝试拦截两个相似的请求,但在每个响应之间延迟两个不同的响应。
- 向 /endpoint 发送第一个请求,响应延迟 3000 毫秒
- 对同一 /endpoint 的第二次请求,响应延迟 1000 毫秒
两个请求都有不同的响应,这里很难覆盖拦截器
cy.intercept('POST', '/validate', (req) => {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
})
cy.intercept('POST', '/validate', (req) => {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
})
在此处查看 Gleb Bahmutov 的回答
使用 times
选项限制拦截将捕获的调用数。
但请注意,最后添加的截距会先检查,因此您可能需要颠倒顺序。
cy.intercept({method: 'POST', url: '/validate', times: 1}, (req) => {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
})
cy.intercept({method: 'POST', url: '/validate', times: 1}, (req) => {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
})
示例应用程序
<script>
setTimeout(() => {
fetch('/validate', { method: 'POST'})
.then(res => res.json())
.then(res => console.log('1st', res))
}, 100)
setTimeout(() => {
fetch('/validate', { method: 'POST'})
.then(res => res.json())
.then(res => console.log('2nd', res))
}, 200)
</script>
控制台按预期顺序输出,夹具数据不同
2nd {data: 'valid'}
1st {data: 'invalid'}
相似的请求意味着您可以区分它们,所以通过请求中的任何 属性 来区分它们。
cy.intercept('POST', '/validate', (req) => {
if (req.body.any.property.only.of.my.first.request === true) {
req.reply({
delay: 1000,
fixture: 'validData.json'
});
} else {
req.reply({
delay: 3000,
fixture: 'invalidData.json'
});
}
})
您可以使用 Gleb 的 example here 作为起点。我们将使用一个超出拦截范围的变量来跟踪请求。
it('some test', () => {
let requestCount = 0;
cy.intercept('POST', '/validate', (req) => {
requestCount++;
req.reply({
delay: requestCount === 1 ? 1000 : 3000,
fixture: requestCount === 1 ? 'validData.json' : 'invalidData.json'
});
});
// rest of test
})
如果提供的三元组不能实现您的目标,您可以修改您的逻辑以return不同的延迟/装置。
您可以按照您希望应用它们的顺序提供一组 routeHandlers
it('test the same route multiple times', () => {
let request = 0;
const replies = [
{delay: 3000, fixture: 'invalidData.json'},
{delay: 1000, fixture: 'validData.json'},
]
cy.intercept('POST', '/validate', (req) => {
req.reply(replies[request++])
});
cy.visit(...);
})