Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a bad request in cypress

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:

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')
    })
  }})

The request that was sent:

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
  }

This is the response I get:

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

like image 759
ashna naran Avatar asked Jul 02 '19 13:07

ashna naran


People also ask

How do you catch a mistake in Cypress?

Handling Error in Cypress Unlike other Javascript-Based Frameworks, Cypress doesn't allow you to use the try and catch block to handle the exception. Cypress provides a special mechanism for handling exceptions in your code. Handling different types of Exceptions such as: Exception from Webpage Under Test.

What is stubbing Cypress?

Stubbing. Cypress enables you to stub a response and control the body , status , headers , or even delay. cy. intercept() is used to control the behavior of HTTP requests.


1 Answers

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:

    cy.request({
        method: 'POST',
        url: 'https://myrAPI',
        failOnStatusCode: false,
        auth:
        {
            username: 'user@user',
            password: 'user123',
        },
        headers:
        {
            'Authorization': 'Basic dXNlckB1c2VyOnVzZXI=',
            'Content-Type': 'text/plain'
        }
    })
like image 191
Zach Bloomquist Avatar answered Sep 22 '22 15:09

Zach Bloomquist