gpt4 book ai didi

javascript - 如何在柏树中测试错误请求

转载 作者:行者123 更新时间:2023-11-28 20:04:27 24 4
gpt4 key购买 nike

我正在使用不正确的凭据测试 cypress 中的 POST 方法日志记录。这将返回 400 个我需要测试的错误请求。

这是我的:

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...

我想知道响应或正文中有什么

最佳答案

您问题的答案在错误消息中:

If you do not want status codes to cause failures pass the option: 'failOnStatusCode: false'

因此,传递 failOnStatusCode: false 不会因错误的状态代码而失败:

    cy.request({
method: 'POST',
url: 'https://myrAPI',
failOnStatusCode: false,
auth:
{
username: 'user@user',
password: 'user123',
},
headers:
{
'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
'Content-Type': 'text/plain'
}
})

关于javascript - 如何在柏树中测试错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56853865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com