如何在 cypress 中测试错误的请求 [英] How to test a bad request in cypress
本文介绍了如何在 cypress 中测试错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用不正确的凭据测试在 cypress 中登录 POST 方法.这将返回 400 个我需要测试的错误请求.
I am testing logging in POST method in cypress with incorrect credentials. this returns 400 bad requests which I need to test.
这就是我所拥有的:
describe('Login API Test - Correct user login', () => {
it('Validate the header', () => {
cy.request({
method: 'POST',
url: 'https://myrAPI',
auth: {
username: 'user@user',
password: 'user123',
},
headers: {
'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
'Content-Type': 'text/plain'
}
}).then((response) => {
// expect(response.body).to.exist // true
// expect(response.body).('User.Access: Exception occured:User.Access : CheckUser: Exception occurred:Error with Authentication Header. result =') // true
// expect(response.headers).should.contain('text/plain; charset=utf-8')
// expect(response.body).statusCode.should.equal(400)
response.status.should.equal(400)
//expect(response).to.have.property('headers')
})
}})
发送的请求:
Method: POST
URL: https://myapi
Headers: {
"Connection": "keep-alive",
"Authorization": "Basic dXNlckB1c2VyOnVzZXIxMjM=",
"Content-Type": "text/plain",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"accept": "*/*",
"accept-encoding": "gzip, deflate",
"content-length": 0
}
这是我得到的回复:
Status: 400 - Bad Request
Headers: {
"content-length": "239",
"content-type": "text/plain; charset=utf-8",
"request-context": "appId=cid-v1:d994e38c-9493-4dd6-ac8c-5395bb9ce790",
"date": "Tue, 02 Jul 2019 13:35:18 GMT"
}
Body: User.Access: Exception occured:User.Access : CheckUser: Exception occurred:Exception when checkin...
我想知道响应或正文中的内容
I'd like to know what is in the response or body
推荐答案
您的问题的答案在错误消息中:
The answer to your question is in the error message:
如果您不希望状态代码导致失败,请传递选项:'failOnStatusCode: false'
If you do not want status codes to cause failures pass the option: 'failOnStatusCode: false'
因此通过 failOnStatusCode: false
不会在错误状态代码上失败:
So pass failOnStatusCode: false
to not fail on bad status codes:
cy.request({
method: 'POST',
url: 'https://myrAPI',
failOnStatusCode: false,
auth:
{
username: 'user@user',
password: 'user123',
},
headers:
{
'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
'Content-Type': 'text/plain'
}
})
这篇关于如何在 cypress 中测试错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文