Skip to content

Cypress concatenates cookies on redirects after POST when using cy.request. #357

@pehota

Description

@pehota

Hi @brian-mann ,

In our company we're using cy.request for form submission in utility commands in order to speed tests up.
The forms are being submitted correctly but certain cookies are being concatenated at the redirects that happen after a successful POST.
Basically what we need is to create a new user before we run the tests and run the tests using him.
The first step is to login as admin and the second one is to create the user.
After submitting the login data the server sends a 302 status and redirects to the admin home page.
When this happens Cypress concatenates the session cookies (i.e. we see something like this when we inspect the request: session_dev =R0g0bVlqRVlnNXJkWHQwTzd1bUdERXNSemoyWDlybEVRcVFITVp4YkoyODhaV2F0Z3BBajdiQXhDUkUzTG4vVzBFOWNzdStLa3VMa0NiNDJoeWFzQjdUSUVuSXlrS1QyL1Q0ci9JS0ZsUGY3VGh2L0p6R2JmMGtpblBhQThuZjFZYWtMZHFTOGt6UFpFa2ZrT2pOUXBxMVRuU2J3NUxVY0tyYzR0U0xmOWZ3bTRZNjJZM0gvLzB1d3NQTlZ5ZXZoLS0wUVRTQktHOU53dWJYVHpJNFZOQkxBPT0%3D--4e98ae3094b98b2967dd2b96170ef1a7d1cb247f; session_dev =dFJnQWp3MkYzVGNpVFQ4MnFXcE1MNjRBbFR4UnB5WFFLU0tJT2tWUXFwMXpmMmdxbUUrUWdIWWJSY3oyYmQ4UjNEN3hFU0NEalQ5WnpBdHJCMk9sSHN3NFFFbC9Sc3JVc0FmWGVvRFRKUDJ3dG42L2dEZ3BVQXU0ZytNOS9UWHFUN2J5aWVlbWlOY0hUU0I1V21YOTRUNzl2K1UvMkE0YUU0QnluRzhtWVhzRXNBTnB2TWJaUE1ENkl0anN0c0NnVUFsU2VHNUFFVUpaeDdmMXZBR0NEWlY2Uk9qZ3RZbU1VNk8zVUcxL3B2dTdMc05Vb0M3eitMMS9ubUJtWkZrNjh4b2RNM001S0NiTms1Tnd5d0o1akJhNmVRU3lVZjJ0Tkk4SERpWklxajQ9LS1hRFIzbmJkRXlZSCtHRFpURlpDN1lBPT0%3D--e52508ff7d14831a0331b69c131fe43878e68e6e
The cookies become invalid and the server redirects to the login page again making it impossible to continue.

We're using a cookies whitelist in order to keep the session cookies.

Code

Cypress.Cookies.defaults({
  whitelist: [
    'session_dev',
    'session_prod',
  ],
});

Cypress.addParentCommand('submitForm', (options = {}) => {
  const requestOpts = Object.assign({
    method: 'get',
    url: '',
    followRedirect: true,
    form: true,
    body: {},
    headers: {},
  }, options);
  const { url } = requestOpts;

  // First get the auth_token and the form action by visiting the url
  // using a GET request
   cy
    .chain()
    .request(url)
    .its('body')
    .then((body) => {
      const $form = Cypress.$(body).find('form');
      const formAction = $form.attr('action') || url;
      const method = $form.attr('method') || $form.prop('method') || 'get';
      const authToken = $form.find('input[name="auth_token"]');
      const submitRequestOpts =
        Object.assign({ method: method.toUpperCase() }, requestOpts, {
          url: formAction,
          body: Object.assign(requestOpts.body, {
            auth_token: authToken.val(),
          }),
        });

      cy
        .chain()
        .request(submitRequestOpts);
    });
});

// Utility command for creating a new user
Cypress.addParentCommand('createUser', () => {
  cy
    .clearCookies()
    // login as admin
    .submitForm({
      url: '/login',
      body: {
         email: 'admin@ourcompany.com',
         password: 'password',
      },
    })
    // We're supposed to be logged in here and create a new user
    // Instead the GET request to `/users/new` gets redirected to the login page
    .submitForm({
      url: '/users/new',
      body: {
        email: 'test@test.com',
      },
    });
});

Basically the following happens:

  • POST to login form
  • Receive 302 to admin home page
  • Request session cookies from previous requests are concatenated here rendering them invalid
  • Redirect to login page because of invalid cookies

Hopefully I managed to explain the issue.

Thanks,
Dimitar Apostolov

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions