Skip to content

Commit

Permalink
test: added xhr tests (#7702)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

## Purpose
Add tests for the [corresponding PR in
HH](DevExpress/testcafe-hammerhead#2890)

## Approach
_Describe how your changes address the issue or implement the desired
functionality in as much detail as possible._

## References
DevExpress/testcafe-hammerhead#2890

## Pre-Merge TODO
- [ ] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
  • Loading branch information
Aleksey28 authored May 17, 2023
1 parent fb6e9e3 commit fba2da1
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"source-map-support": "^0.5.16",
"strip-bom": "^2.0.0",
"testcafe-browser-tools": "2.0.23",
"testcafe-hammerhead": "31.4.0",
"testcafe-hammerhead": "31.4.1",
"testcafe-legacy-api": "5.1.6",
"testcafe-reporter-dashboard": "^0.2.10",
"testcafe-reporter-json": "^2.1.0",
Expand Down
75 changes: 75 additions & 0 deletions test/functional/fixtures/request-pipeline/xhr/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<head>
<title>XMLHttpRequest</title>
</head>
<body>
<form action="submit.html">
<input type="button" id="test-header" value="test-header"/>
<input type="button" id="auth-header" value="auth-header"/>
<input type="button" id="delay" value="delay"/>
</form>

<script>
document.querySelector('#test-header').addEventListener('click', () => {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/xhr/test-header');
xhr.setRequestHeader('test', 'test-string');
xhr.withCredentials = true;
xhr.send();

xhr.onload = function() {
if (xhr.status = 200) {
const xhrResultDiv = document.createElement('div');

xhrResultDiv.id = 'xhr-result';
xhrResultDiv.textContent = xhr.responseText;

document.body.appendChild(xhrResultDiv);
}
};
})

document.querySelector('#auth-header').addEventListener('click', () => {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/xhr/auth-header');
xhr.setRequestHeader('authorization', 'authorization-string');
xhr.send();

xhr.onload = function() {
if (xhr.status = 200) {
const xhrResultDiv = document.createElement('div');

xhrResultDiv.id = 'xhr-result';
xhrResultDiv.textContent = xhr.getResponseHeader('authorization');

document.body.appendChild(xhrResultDiv);
}
};
})

document.querySelector('#delay').addEventListener('click', () => {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/xhr/1000', true);
xhr.send();

xhr.onreadystatechange = function () {
if (xhr.readyState != 4) return;

if (xhr.status === 200) {
const xhrResultDiv = document.createElement('div');

xhrResultDiv.id = 'xhr-result';
xhrResultDiv.textContent = xhr.responseText;

xhrResultDiv.addEventListener('click', function () {
throw new Error('Xhr requests are finished');
});

document.body.appendChild(xhrResultDiv);
}
}
});
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions test/functional/fixtures/request-pipeline/xhr/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { errorInEachBrowserContains } = require('../../../assertion-helper.js');

describe('XHR', () => {
it('Should keep header if request was reopened', () => {
return runTests('./testcafe-fixtures/index.js', 'Click test header button');
});

it('Should not return authorization prefix for the authorization header', () => {
return runTests('./testcafe-fixtures/index.js', 'Click auth header button');
});

it('Should wait for xhr-requests after an action', () => {
return runTests('./testcafe-fixtures/index.js', 'Click delay button', { shouldFail: true })
.catch(errs => {
errorInEachBrowserContains(errs, 'Xhr requests are finished', 0);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Selector } from 'testcafe';

fixture('My fixture')
.page('http://localhost:3000/fixtures/request-pipeline/xhr/pages/index.html');
test('Click test header button', async t => {
await t.click('#test-header');
await t.expect(Selector('#xhr-result').textContent).eql('test-string');
});
test('Click auth header button', async t => {
await t.click('#auth-header');
await t.expect(Selector('#xhr-result').textContent).eql('authorization-string');
});
test('Click delay button', async t => {
await t.click('#delay');
await t.click('#xhr-result');
});
9 changes: 9 additions & 0 deletions test/functional/site/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ Server.prototype._setupRoutes = function (apiRouter) {
res.end(Mustache.render(UPLOAD_SUCCESS_PAGE_TEMPLATE, { uploadedDataArray: filesData }));
});

this.app.post('/xhr/test-header', function (req, res) {
res.send(req.headers.test);
});

this.app.post('/xhr/auth-header', function (req, res) {
res.setHeader('authorization', 'authorization-string');
res.send();
});

this.app.post('/xhr/:delay', function (req, res) {
const delay = req.params.delay || 0;

Expand Down

0 comments on commit fba2da1

Please sign in to comment.