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.
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'})
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.
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.
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.
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}`);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With