I am testing logging in POST method in cypress with incorrect credentials. this returns 400 bad requests which I need to test.
This is what I have:
JavaScript
x
23
23
1
describe('Login API Test - Correct user login', () => {
2
it('Validate the header', () => {
3
cy.request({
4
method: 'POST',
5
url: 'https://myrAPI',
6
auth: {
7
username: 'user@user',
8
password: 'user123',
9
},
10
headers: {
11
'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
12
'Content-Type': 'text/plain'
13
}
14
}).then((response) => {
15
// expect(response.body).to.exist // true
16
// expect(response.body).('User.Access: Exception occured:User.Access : CheckUser: Exception occurred:Error with Authentication Header. result =') // true
17
// expect(response.headers).should.contain('text/plain; charset=utf-8')
18
// expect(response.body).statusCode.should.equal(400)
19
response.status.should.equal(400)
20
//expect(response).to.have.property('headers')
21
})
22
}})
23
The request that was sent:
JavaScript
1
13
13
1
Method: POST
2
URL: https://myapi
3
Headers: {
4
"Connection": "keep-alive",
5
"Authorization": "Basic dXNlckB1c2VyOnVzZXIxMjM=",
6
"Content-Type": "text/plain",
7
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
8
(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
9
"accept": "*/*",
10
"accept-encoding": "gzip, deflate",
11
"content-length": 0
12
}
13
This is the response I get:
JavaScript
1
9
1
Status: 400 - Bad Request
2
Headers: {
3
"content-length": "239",
4
"content-type": "text/plain; charset=utf-8",
5
"request-context": "appId=cid-v1:d994e38c-9493-4dd6-ac8c-5395bb9ce790",
6
"date": "Tue, 02 Jul 2019 13:35:18 GMT"
7
}
8
Body: User.Access: Exception occured:User.Access : CheckUser: Exception occurred:Exception when checkin
9
I’d like to know what is in the response or body
Advertisement
Answer
The answer to your question is in the error message:
If you do not want status codes to cause failures pass the option: ‘failOnStatusCode: false’
So pass failOnStatusCode: false
to not fail on bad status codes:
JavaScript
1
16
16
1
cy.request({
2
method: 'POST',
3
url: 'https://myrAPI',
4
failOnStatusCode: false,
5
auth:
6
{
7
username: 'user@user',
8
password: 'user123',
9
},
10
headers:
11
{
12
'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
13
'Content-Type': 'text/plain'
14
}
15
})
16