refactor: implement ajax() in tests using native fetch instead of jQuery (#32579)
This commit is contained in:
parent
7032be660d
commit
9d054755d6
13 changed files with 116 additions and 160 deletions
|
@ -2,7 +2,6 @@
|
|||
"env": {
|
||||
"browser": true,
|
||||
"mocha": true,
|
||||
"jquery": true,
|
||||
"serviceworker": true
|
||||
},
|
||||
"globals": {
|
||||
|
|
|
@ -1599,67 +1599,42 @@ describe('asar package', function () {
|
|||
});
|
||||
|
||||
describe('asar protocol', function () {
|
||||
it('can request a file in package', function (done) {
|
||||
it('can request a file in package', async function () {
|
||||
const p = path.resolve(asarDir, 'a.asar', 'file1');
|
||||
$.get('file://' + p, function (data) {
|
||||
try {
|
||||
expect(data.trim()).to.equal('file1');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
const response = await fetch('file://' + p);
|
||||
const data = await response.text();
|
||||
expect(data.trim()).to.equal('file1');
|
||||
});
|
||||
|
||||
it('can request a file in package with unpacked files', function (done) {
|
||||
it('can request a file in package with unpacked files', async function () {
|
||||
const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
|
||||
$.get('file://' + p, function (data) {
|
||||
try {
|
||||
expect(data.trim()).to.equal('a');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
const response = await fetch('file://' + p);
|
||||
const data = await response.text();
|
||||
expect(data.trim()).to.equal('a');
|
||||
});
|
||||
|
||||
it('can request a linked file in package', function (done) {
|
||||
it('can request a linked file in package', async function () {
|
||||
const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
|
||||
$.get('file://' + p, function (data) {
|
||||
try {
|
||||
expect(data.trim()).to.equal('file1');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
const response = await fetch('file://' + p);
|
||||
const data = await response.text();
|
||||
expect(data.trim()).to.equal('file1');
|
||||
});
|
||||
|
||||
it('can request a file in filesystem', function (done) {
|
||||
it('can request a file in filesystem', async function () {
|
||||
const p = path.resolve(asarDir, 'file');
|
||||
$.get('file://' + p, function (data) {
|
||||
try {
|
||||
expect(data.trim()).to.equal('file');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
const response = await fetch('file://' + p);
|
||||
const data = await response.text();
|
||||
expect(data.trim()).to.equal('file');
|
||||
});
|
||||
|
||||
it('gets 404 when file is not found', function (done) {
|
||||
it('gets error when file is not found', async function () {
|
||||
const p = path.resolve(asarDir, 'a.asar', 'no-exist');
|
||||
$.ajax({
|
||||
url: 'file://' + p,
|
||||
error: function (err) {
|
||||
try {
|
||||
expect(err.status).to.equal(404);
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
const response = await fetch('file://' + p);
|
||||
expect(response.status).to.equal(404);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal('Failed to fetch');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
29
spec/fixtures/pages/basic-auth.html
vendored
29
spec/fixtures/pages/basic-auth.html
vendored
|
@ -1,22 +1,21 @@
|
|||
<html>
|
||||
<body>
|
||||
<script src="../../static/jquery-2.0.3.min.js"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var ipcRenderer = require('electron').ipcRenderer;
|
||||
var port = location.search.substr("?port=".length);
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
url: "http://127.0.0.1:" + port,
|
||||
headers: {
|
||||
"Authorization": "Basic " + btoa("test:test")
|
||||
},
|
||||
success: function(result) {
|
||||
ipcRenderer.sendToHost(result);
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
const { ipcRenderer } = require('electron');
|
||||
const url = new URL(location.href);
|
||||
const port = url.searchParams.get('port');
|
||||
(async () => {
|
||||
try {
|
||||
const response = await fetch(`http://127.0.0.1:${port}`, {
|
||||
headers: {
|
||||
'Authorization': `Basic ${btoa('test:test')}`
|
||||
}
|
||||
});
|
||||
ipcRenderer.sendToHost(await response.text());
|
||||
} catch (error) {
|
||||
ipcRenderer.sendToHost(error);
|
||||
},
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
4
spec/fixtures/pages/dom-ready.html
vendored
4
spec/fixtures/pages/dom-ready.html
vendored
|
@ -1,8 +1,8 @@
|
|||
<html>
|
||||
<body>
|
||||
<script src="../../static/jquery-2.0.3.min.js"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var port = location.search.substr("?port=".length);
|
||||
const url = new URL(location.href);
|
||||
const port = url.searchParams.get('port');
|
||||
document.write("<img src='http://127.0.0.1:" + port + "/logo.png' />");
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<body>
|
||||
<script src="jquery-2.0.3.min.js"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
(async function() {
|
||||
// Deprecated APIs are still supported and should be tested.
|
||||
|
|
6
spec/static/jquery-2.0.3.min.js
vendored
6
spec/static/jquery-2.0.3.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue