Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add header to xhr-request

Tags:

cypress

I have the following scenario:

  • A startpage that basically includes other pages through xhr-requests.

  • Some of the xhr-requests are behind an firewall with a single-signon-scheme where each http-request to the server is stripped from a set of headers and then populated according to the users grants.

  • When the request reaches the server it expect some of the reqeusts to have the userId-header.
  • Test-servers are NOT behind "firewall" so we have a plugin in our browsers to emulate the behaviour.

    We would like to run cypress-tests on this page. But we doesn't seem to find how to add this userId-header to the xhr-requests and havn't been able to find an example of this behaviour.

    Could you please provide an example that shows how to do this with a simple example?

An attemt to pseduo-code the scenario: cy.visit('myurl').interceptBeforeXhr('xhr-url').addHeader('xhr-url', {'userId' = 'username'})

like image 945
Roland Avatar asked Jan 09 '20 14:01

Roland


People also ask

How do you set headers in XHR?

The XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header. When using setRequestHeader() , you must call it after calling open() , but before calling send() . If this method is called several times with the same header, the values are merged into one single request header.

What is an XHR header?

HTTP is a protocol. Part of that protocol is the concept of request headers. When an xhr happens, text is exchanged between the client and server. Request headers are part of the text that the client sends to the server. This is a way to set the request headers.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.


1 Answers

You can use headers property to specify a header like this:

describe('Test Cypress Custom Headers', function() {
  it('Visits Cypress Header Test Endpoint', function() {
    const Key = "test-key1";

    cy.visit({
      // An endpoint that expects a x-xhr-url header to grand access
      url: 'https://zikro.gr/dbg/so/59666650', 
      headers: {
        "x-xhr-url": Key
      }
    });

    cy.contains(`Access key: ${Key}`);
  })
});

Or you can use request if you want to make multiple requests using the same headers:

describe('Test Cypress Custom Headers', function() {
  it('Visits Cypress Header Test Endpoint', function() {
    const Key = "test-key1";

    cy.request({
      url: 'https://zikro.gr/dbg/so/59666650',
      headers: {
        "x-xhr-url": Key
      }
    })
    .its('body').should('include', `Access key: ${Key}`);
  });
});

Both tests should be passing.

Just for the record, here is the PHP endpoint header check page code:

$headers = getallheaders();

if(isset($_SERVER['HTTP_X_XHR_URL'])) {
  echo "Access key: {$_SERVER['HTTP_X_XHR_URL']}";
} else {
  echo "Access denied";
}

Lastly, If you want to apply the header value just to all AJAX requests, then you shall use server onAnyRequest and there use proxy to set the xhr header like this:

describe('Test Cypress Custom Headers', function() {
  it('Visits Cypress Header Test Endpoint', function() {
    const Key = "test-key1";

    cy.server({
      onAnyRequest: (route,  proxy) => {
        proxy.xhr.setRequestHeader('x-xhr-url', Key);
      }
    });

    cy.visit('https://zikro.gr/dbg/so/59666650/test-ajax.html');

    // Should pass the test of an AJAX call
    cy.contains(`Access key: ${Key}`);
  });
});
like image 128
Christos Lytras Avatar answered Sep 19 '22 21:09

Christos Lytras