From 17f4e53d6babf7ca04d3b9f305180d4a06801fe5 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 26 Oct 2017 16:30:07 -0400 Subject: [PATCH] match earlier tests to ES6 later ones --- spec/api-protocol-spec.js | 776 ++++++++++++++------------------------ 1 file changed, 292 insertions(+), 484 deletions(-) diff --git a/spec/api-protocol-spec.js b/spec/api-protocol-spec.js index 2d457cb55112..a79bb3b1bcaf 100644 --- a/spec/api-protocol-spec.js +++ b/spec/api-protocol-spec.js @@ -3,42 +3,38 @@ const http = require('http') const path = require('path') const qs = require('querystring') const {closeWindow} = require('./window-helpers') -const remote = require('electron').remote +const {remote} = require('electron') const {BrowserWindow, ipcMain, protocol, session, webContents} = remote -describe('protocol module', function () { - var protocolName = 'sp' - var text = 'valar morghulis' - var postData = { +describe('protocol module', () => { + const protocolName = 'sp' + const text = 'valar morghulis' + const postData = { name: 'post test', type: 'string' } - afterEach(function (done) { - protocol.unregisterProtocol(protocolName, function () { - protocol.uninterceptProtocol('http', function () { - done() - }) + afterEach((done) => { + protocol.unregisterProtocol(protocolName, () => { + protocol.uninterceptProtocol('http', () => done()) }) }) - describe('protocol.register(Any)Protocol', function () { - var emptyHandler = function (request, callback) { - callback() - } + describe('protocol.register(Any)Protocol', () => { + const emptyHandler = (request, callback) => callback() - it('throws error when scheme is already registered', function (done) { - protocol.registerStringProtocol(protocolName, emptyHandler, function (error) { + it('throws error when scheme is already registered', (done) => { + protocol.registerStringProtocol(protocolName, emptyHandler, (error) => { assert.equal(error, null) - protocol.registerBufferProtocol(protocolName, emptyHandler, function (error) { + protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => { assert.notEqual(error, null) done() }) }) }) - it('does not crash when handler is called twice', function (done) { - var doubleHandler = function (request, callback) { + it('does not crash when handler is called twice', (done) => { + const doubleHandler = (request, callback) => { try { callback(text) callback() @@ -46,36 +42,29 @@ describe('protocol module', function () { // Ignore error } } - protocol.registerStringProtocol(protocolName, doubleHandler, function (error) { - if (error) { - return done(error) - } + protocol.registerStringProtocol(protocolName, doubleHandler, (error) => { + if (error) return done(error) + $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sends error when callback is called with nothing', function (done) { - protocol.registerBufferProtocol(protocolName, emptyHandler, function (error) { - if (error) { - return done(error) - } + it('sends error when callback is called with nothing', (done) => { + protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - return done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') return done() } @@ -83,126 +72,98 @@ describe('protocol module', function () { }) }) - it('does not crash when callback is called in next tick', function (done) { - var handler = function (request, callback) { - setImmediate(function () { - callback(text) - }) + it('does not crash when callback is called in next tick', (done) => { + const handler = (request, callback) => { + setImmediate(() => callback(text)) } - protocol.registerStringProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + protocol.registerStringProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) }) - describe('protocol.unregisterProtocol', function () { - it('returns error when scheme does not exist', function (done) { - protocol.unregisterProtocol('not-exist', function (error) { + describe('protocol.unregisterProtocol', () => { + it('returns error when scheme does not exist', (done) => { + protocol.unregisterProtocol('not-exist', (error) => { assert.notEqual(error, null) done() }) }) }) - describe('protocol.registerStringProtocol', function () { - it('sends string as response', function (done) { - var handler = function (request, callback) { - callback(text) - } - protocol.registerStringProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + describe('protocol.registerStringProtocol', () => { + it('sends string as response', (done) => { + const handler = (request, callback) => callback(text) + protocol.registerStringProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sets Access-Control-Allow-Origin', function (done) { - var handler = function (request, callback) { - callback(text) - } - protocol.registerStringProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('sets Access-Control-Allow-Origin', (done) => { + const handler = (request, callback) => callback(text) + protocol.registerStringProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data, status, request) { + success: (data, status, request) => { assert.equal(data, text) assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*') done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sends object as response', function (done) { - var handler = function (request, callback) { + it('sends object as response', (done) => { + const handler = (request, callback) => { callback({ data: text, mimeType: 'text/html' }) } - protocol.registerStringProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + protocol.registerStringProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('fails when sending object other than string', function (done) { - var handler = function (request, callback) { - callback(new Date()) - } - protocol.registerBufferProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending object other than string', (done) => { + const handler = (request, callback) => callback(new Date()) + protocol.registerBufferProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -211,95 +172,71 @@ describe('protocol module', function () { }) }) - describe('protocol.registerBufferProtocol', function () { - var buffer = new Buffer(text) - - it('sends Buffer as response', function (done) { - var handler = function (request, callback) { - callback(buffer) - } - protocol.registerBufferProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + describe('protocol.registerBufferProtocol', () => { + const buffer = new Buffer(text) + it('sends Buffer as response', (done) => { + const handler = (request, callback) => callback(buffer) + protocol.registerBufferProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sets Access-Control-Allow-Origin', function (done) { - var handler = function (request, callback) { - callback(buffer) - } - - protocol.registerBufferProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('sets Access-Control-Allow-Origin', (done) => { + const handler = (request, callback) => callback(buffer) + protocol.registerBufferProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data, status, request) { + success: (data, status, request) => { assert.equal(data, text) assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*') done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sends object as response', function (done) { - var handler = function (request, callback) { + it('sends object as response', (done) => { + const handler = (request, callback) => { callback({ data: buffer, mimeType: 'text/html' }) } - protocol.registerBufferProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + protocol.registerBufferProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('fails when sending string', function (done) { - var handler = function (request, callback) { - callback(text) - } - protocol.registerBufferProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending string', (done) => { + const handler = (request, callback) => callback(text) + protocol.registerBufferProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -308,120 +245,89 @@ describe('protocol module', function () { }) }) - describe('protocol.registerFileProtocol', function () { - var filePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'file1') - var fileContent = require('fs').readFileSync(filePath) - var normalPath = path.join(__dirname, 'fixtures', 'pages', 'a.html') - var normalContent = require('fs').readFileSync(normalPath) + describe('protocol.registerFileProtocol', () => { + const filePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'file1') + const fileContent = require('fs').readFileSync(filePath) + const normalPath = path.join(__dirname, 'fixtures', 'pages', 'a.html') + const normalContent = require('fs').readFileSync(normalPath) - it('sends file path as response', function (done) { - var handler = function (request, callback) { - callback(filePath) - } - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('sends file path as response', (done) => { + const handler = (request, callback) => callback(filePath) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, String(fileContent)) return done() }, - error: function (xhr, errorType, error) { - return done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sets Access-Control-Allow-Origin', function (done) { - var handler = function (request, callback) { - callback(filePath) - } - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('sets Access-Control-Allow-Origin', (done) => { + const handler = (request, callback) => callback(filePath) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data, status, request) { + success: (data, status, request) => { assert.equal(data, String(fileContent)) assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*') done() }, - error: function (xhr, errorType, error) { + error: (xhr, errorType, error) => { done(error) } }) }) }) - it('sends object as response', function (done) { - var handler = function (request, callback) { - callback({ - path: filePath - }) - } - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('sends object as response', (done) => { + const handler = (request, callback) => callback({ path: filePath }) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, String(fileContent)) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('can send normal file', function (done) { - var handler = function (request, callback) { - callback(normalPath) - } - - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('can send normal file', (done) => { + const handler = (request, callback) => callback(normalPath) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, String(normalContent)) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('fails when sending unexist-file', function (done) { - var fakeFilePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'not-exist') - var handler = function (request, callback) { - callback(fakeFilePath) - } - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending unexist-file', (done) => { + const fakeFilePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'not-exist') + const handler = (request, callback) => callback(fakeFilePath) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -429,21 +335,15 @@ describe('protocol module', function () { }) }) - it('fails when sending unsupported content', function (done) { - var handler = function (request, callback) { - callback(new Date()) - } - protocol.registerFileProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending unsupported content', (done) => { + const handler = (request, callback) => callback(new Date()) + protocol.registerFileProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -452,57 +352,41 @@ describe('protocol module', function () { }) }) - describe('protocol.registerHttpProtocol', function () { - it('sends url as response', function (done) { - var server = http.createServer(function (req, res) { + describe('protocol.registerHttpProtocol', () => { + it('sends url as response', (done) => { + const server = http.createServer((req, res) => { assert.notEqual(req.headers.accept, '') res.end(text) server.close() }) - server.listen(0, '127.0.0.1', function () { - var port = server.address().port - var url = 'http://127.0.0.1:' + port - var handler = function (request, callback) { - callback({ - url: url - }) - } - protocol.registerHttpProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + server.listen(0, '127.0.0.1', () => { + const port = server.address().port + const url = 'http://127.0.0.1:' + port + const handler = (request, callback) => callback({url}) + protocol.registerHttpProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) }) - it('fails when sending invalid url', function (done) { - var handler = function (request, callback) { - callback({ - url: 'url' - }) - } - protocol.registerHttpProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending invalid url', (done) => { + const handler = (request, callback) => callback({url: 'url'}) + protocol.registerHttpProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -510,21 +394,17 @@ describe('protocol module', function () { }) }) - it('fails when sending unsupported content', function (done) { - var handler = function (request, callback) { - callback(new Date()) - } - protocol.registerHttpProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + it('fails when sending unsupported content', (done) => { + const handler = (request, callback) => callback(new Date()) + protocol.registerHttpProtocol(protocolName, handler, (error) => { + if (error) return done(error) $.ajax({ url: protocolName + '://fake-host', cache: false, - success: function () { + success: () => { done('request succeeded but it should not') }, - error: function (xhr, errorType) { + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -532,32 +412,26 @@ describe('protocol module', function () { }) }) - it('works when target URL redirects', function (done) { - var contents = null - var server = http.createServer(function (req, res) { + it('works when target URL redirects', (done) => { + let contents = null + const server = http.createServer((req, res) => { if (req.url === '/serverRedirect') { res.statusCode = 301 - res.setHeader('Location', 'http://' + req.rawHeaders[1]) + res.setHeader('Location', `http://${req.rawHeaders[1]}`) res.end() } else { res.end(text) } }) - server.listen(0, '127.0.0.1', function () { - var port = server.address().port - var url = `${protocolName}://fake-host` - var redirectURL = `http://127.0.0.1:${port}/serverRedirect` - var handler = function (request, callback) { - callback({ - url: redirectURL - }) - } - protocol.registerHttpProtocol(protocolName, handler, function (error) { - if (error) { - return done(error) - } + server.listen(0, '127.0.0.1', () => { + const port = server.address().port + const url = `${protocolName}://fake-host` + const redirectURL = `http://127.0.0.1:${port}/serverRedirect` + const handler = (request, callback) => callback({url: redirectURL}) + protocol.registerHttpProtocol(protocolName, handler, (error) => { + if (error) return done(error) contents = webContents.create({}) - contents.on('did-finish-load', function () { + contents.on('did-finish-load', () => { assert.equal(contents.getURL(), url) server.close() contents.destroy() @@ -569,62 +443,58 @@ describe('protocol module', function () { }) }) - describe('protocol.isProtocolHandled', function () { - it('returns true for about:', function (done) { - protocol.isProtocolHandled('about', function (result) { + describe('protocol.isProtocolHandled', () => { + it('returns true for about:', (done) => { + protocol.isProtocolHandled('about', (result) => { assert.equal(result, true) done() }) }) - it('returns true for file:', function (done) { - protocol.isProtocolHandled('file', function (result) { + it('returns true for file:', (done) => { + protocol.isProtocolHandled('file', (result) => { assert.equal(result, true) done() }) }) - it('returns true for http:', function (done) { - protocol.isProtocolHandled('http', function (result) { + it('returns true for http:', (done) => { + protocol.isProtocolHandled('http', (result) => { assert.equal(result, true) done() }) }) - it('returns true for https:', function (done) { - protocol.isProtocolHandled('https', function (result) { + it('returns true for https:', (done) => { + protocol.isProtocolHandled('https', (result) => { assert.equal(result, true) done() }) }) - it('returns false when scheme is not registered', function (done) { - protocol.isProtocolHandled('no-exist', function (result) { + it('returns false when scheme is not registered', (done) => { + protocol.isProtocolHandled('no-exist', (result) => { assert.equal(result, false) done() }) }) - it('returns true for custom protocol', function (done) { - var emptyHandler = function (request, callback) { - callback() - } - protocol.registerStringProtocol(protocolName, emptyHandler, function (error) { + it('returns true for custom protocol', (done) => { + const emptyHandler = (request, callback) => callback() + protocol.registerStringProtocol(protocolName, emptyHandler, (error) => { assert.equal(error, null) - protocol.isProtocolHandled(protocolName, function (result) { + protocol.isProtocolHandled(protocolName, (result) => { assert.equal(result, true) done() }) }) }) - it('returns true for intercepted protocol', function (done) { - var emptyHandler = function (request, callback) { - callback() - } - protocol.interceptStringProtocol('http', emptyHandler, function (error) { + it('returns true for intercepted protocol', (done) => { + const emptyHandler = (request, callback) => callback() + protocol.interceptStringProtocol('http', emptyHandler, (error) => { assert.equal(error, null) - protocol.isProtocolHandled('http', function (result) { + protocol.isProtocolHandled('http', (result) => { assert.equal(result, true) done() }) @@ -632,23 +502,20 @@ describe('protocol module', function () { }) }) - describe('protocol.intercept(Any)Protocol', function () { - var emptyHandler = function (request, callback) { - callback() - } - - it('throws error when scheme is already intercepted', function (done) { - protocol.interceptStringProtocol('http', emptyHandler, function (error) { + describe('protocol.intercept(Any)Protocol', () => { + const emptyHandler = (request, callback) => callback() + it('throws error when scheme is already intercepted', (done) => { + protocol.interceptStringProtocol('http', emptyHandler, (error) => { assert.equal(error, null) - protocol.interceptBufferProtocol('http', emptyHandler, function (error) { + protocol.interceptBufferProtocol('http', emptyHandler, (error) => { assert.notEqual(error, null) done() }) }) }) - it('does not crash when handler is called twice', function (done) { - var doubleHandler = function (request, callback) { + it('does not crash when handler is called twice', (done) => { + var doubleHandler = (request, callback) => { try { callback(text) callback() @@ -656,39 +523,29 @@ describe('protocol module', function () { // Ignore error } } - protocol.interceptStringProtocol('http', doubleHandler, function (error) { - if (error) { - return done(error) - } + protocol.interceptStringProtocol('http', doubleHandler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('sends error when callback is called with nothing', function (done) { - if (process.env.TRAVIS === 'true') { - return done() - } - protocol.interceptBufferProtocol('http', emptyHandler, function (error) { - if (error) { - return done(error) - } + it('sends error when callback is called with nothing', (done) => { + if (process.env.TRAVIS === 'true') return done() + protocol.interceptBufferProtocol('http', emptyHandler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, - success: function () { - done('request succeeded but it should not') - }, - error: function (xhr, errorType) { + success: () => done('request succeeded but it should not'), + error: (xhr, errorType) => { assert.equal(errorType, 'error') done() } @@ -697,149 +554,123 @@ describe('protocol module', function () { }) }) - describe('protocol.interceptStringProtocol', function () { - it('can intercept http protocol', function (done) { - var handler = function (request, callback) { - callback(text) - } - protocol.interceptStringProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + describe('protocol.interceptStringProtocol', () => { + it('can intercept http protocol', (done) => { + const handler = (request, callback) => callback(text) + protocol.interceptStringProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('can set content-type', function (done) { - var handler = function (request, callback) { + it('can set content-type', (done) => { + const handler = (request, callback) => { callback({ mimeType: 'application/json', data: '{"value": 1}' }) } - protocol.interceptStringProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + protocol.interceptStringProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(typeof data, 'object') assert.equal(data.value, 1) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('can receive post data', function (done) { - var handler = function (request, callback) { - var uploadData = request.uploadData[0].bytes.toString() - callback({ - data: uploadData - }) + it('can receive post data', (done) => { + const handler = (request, callback) => { + const uploadData = request.uploadData[0].bytes.toString() + callback({data: uploadData}) } - protocol.interceptStringProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + protocol.interceptStringProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, type: 'POST', data: postData, - success: function (data) { + success: (data) => { assert.deepEqual(qs.parse(data), postData) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) }) - describe('protocol.interceptBufferProtocol', function () { - it('can intercept http protocol', function (done) { - var handler = function (request, callback) { - callback(new Buffer(text)) - } - protocol.interceptBufferProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + describe('protocol.interceptBufferProtocol', () => { + it('can intercept http protocol', (done) => { + const handler = (request, callback) => callback(new Buffer(text)) + protocol.interceptBufferProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, - success: function (data) { + success: (data) => { assert.equal(data, text) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) - it('can receive post data', function (done) { - var handler = function (request, callback) { - var uploadData = request.uploadData[0].bytes + it('can receive post data', (done) => { + const handler = (request, callback) => { + const uploadData = request.uploadData[0].bytes callback(uploadData) } - protocol.interceptBufferProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + protocol.interceptBufferProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, type: 'POST', data: postData, - success: function (data) { + success: (data) => { assert.equal(data, $.param(postData)) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) }) - describe('protocol.interceptHttpProtocol', function () { - it('can send POST request', function (done) { - var server = http.createServer(function (req, res) { - var body = '' - req.on('data', function (chunk) { + describe('protocol.interceptHttpProtocol', () => { + it('can send POST request', (done) => { + const server = http.createServer((req, res) => { + let body = '' + req.on('data', (chunk) => { body += chunk }) - req.on('end', function () { + req.on('end', () => { res.end(body) }) server.close() }) - server.listen(0, '127.0.0.1', function () { - var port = server.address().port - var url = 'http://127.0.0.1:' + port - var handler = function (request, callback) { - var data = { + server.listen(0, '127.0.0.1', () => { + const port = server.address().port + const url = `http://127.0.0.1:${port}` + const handler = (request, callback) => { + const data = { url: url, method: 'POST', uploadData: { @@ -850,48 +681,40 @@ describe('protocol module', function () { } callback(data) } - protocol.interceptHttpProtocol('http', handler, function (error) { - if (error) { - return done(error) - } + protocol.interceptHttpProtocol('http', handler, (error) => { + if (error) return done(error) $.ajax({ url: 'http://fake-host', cache: false, type: 'POST', data: postData, - success: function (data) { + success: (data) => { assert.deepEqual(qs.parse(data), postData) done() }, - error: function (xhr, errorType, error) { - done(error) - } + error: (xhr, errorType, error) => done(error) }) }) }) }) - it('can use custom session', function (done) { - const customSession = session.fromPartition('custom-ses', { - cache: false - }) - customSession.webRequest.onBeforeRequest(function (details, callback) { + it('can use custom session', (done) => { + const customSession = session.fromPartition('custom-ses', {cache: false}) + customSession.webRequest.onBeforeRequest((details, callback) => { assert.equal(details.url, 'http://fake-host/') callback({cancel: true}) }) - const handler = function (request, callback) { + const handler = (request, callback) => { callback({ url: request.url, session: customSession }) } - protocol.interceptHttpProtocol('http', handler, function (error) { - if (error) { - return done(error) - } - fetch('http://fake-host').then(function () { + protocol.interceptHttpProtocol('http', handler, (error) => { + if (error) return done(error) + fetch('http://fake-host').then(() => { done('request succeeded but it should not') - }).catch(function () { + }).catch(() => { customSession.webRequest.onBeforeRequest(null) done() }) @@ -899,47 +722,47 @@ describe('protocol module', function () { }) }) - describe('protocol.uninterceptProtocol', function () { - it('returns error when scheme does not exist', function (done) { - protocol.uninterceptProtocol('not-exist', function (error) { + describe('protocol.uninterceptProtocol', () => { + it('returns error when scheme does not exist', (done) => { + protocol.uninterceptProtocol('not-exist', (error) => { assert.notEqual(error, null) done() }) }) - it('returns error when scheme is not intercepted', function (done) { - protocol.uninterceptProtocol('http', function (error) { + it('returns error when scheme is not intercepted', (done) => { + protocol.uninterceptProtocol('http', (error) => { assert.notEqual(error, null) done() }) }) }) - describe('protocol.registerStandardSchemes', function () { + describe('protocol.registerStandardSchemes', () => { const standardScheme = remote.getGlobal('standardScheme') - const origin = standardScheme + '://fake-host' - const imageURL = origin + '/test.png' + const origin = `${standardScheme}://fake-host` + const imageURL = `${origin}/test.png` const filePath = path.join(__dirname, 'fixtures', 'pages', 'b.html') const fileContent = '' - var w = null - var success = null + let w = null + let success = null - beforeEach(function () { + beforeEach(() => { w = new BrowserWindow({show: false}) success = false }) - afterEach(function (done) { - protocol.unregisterProtocol(standardScheme, function () { - closeWindow(w).then(function () { + afterEach((done) => { + protocol.unregisterProtocol(standardScheme, () => { + closeWindow(w).then(() => { w = null done() }) }) }) - it('resolves relative resources', function (done) { - var handler = function (request, callback) { + it('resolves relative resources', (done) => { + const handler = (request, callback) => { if (request.url === imageURL) { success = true callback() @@ -947,11 +770,9 @@ describe('protocol module', function () { callback(filePath) } } - protocol.registerFileProtocol(standardScheme, handler, function (error) { - if (error) { - return done(error) - } - w.webContents.on('did-finish-load', function () { + protocol.registerFileProtocol(standardScheme, handler, (error) => { + if (error) return done(error) + w.webContents.on('did-finish-load', () => { assert(success) done() }) @@ -959,8 +780,8 @@ describe('protocol module', function () { }) }) - it('resolves absolute resources', function (done) { - var handler = function (request, callback) { + it('resolves absolute resources', (done) => { + const handler = (request, callback) => { if (request.url === imageURL) { success = true callback() @@ -971,11 +792,9 @@ describe('protocol module', function () { }) } } - protocol.registerStringProtocol(standardScheme, handler, function (error) { - if (error) { - return done(error) - } - w.webContents.on('did-finish-load', function () { + protocol.registerStringProtocol(standardScheme, handler, (error) => { + if (error) return done(error) + w.webContents.on('did-finish-load', () => { assert(success) done() }) @@ -983,29 +802,21 @@ describe('protocol module', function () { }) }) - it('can have fetch working in it', function (done) { + it('can have fetch working in it', (done) => { const content = '' - const handler = function (request, callback) { - callback({data: content, mimeType: 'text/html'}) - } - protocol.registerStringProtocol(standardScheme, handler, function (error) { + const handler = (request, callback) => callback({data: content, mimeType: 'text/html'}) + protocol.registerStringProtocol(standardScheme, handler, (error) => { if (error) return done(error) - w.webContents.on('crashed', function () { - done('WebContents crashed') - }) - w.webContents.on('did-finish-load', function () { - done() - }) + w.webContents.on('crashed', () => done('WebContents crashed')) + w.webContents.on('did-finish-load', () => done()) w.loadURL(origin) }) }) - it('can access files through the FileSystem API', function (done) { + it('can access files through the FileSystem API', (done) => { let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html') - const handler = function (request, callback) { - callback({path: filePath}) - } - protocol.registerFileProtocol(standardScheme, handler, function (error) { + const handler = (request, callback) => callback({path: filePath}) + protocol.registerFileProtocol(standardScheme, handler, (error) => { if (error) return done(error) w.loadURL(origin) }) @@ -1013,15 +824,12 @@ describe('protocol module', function () { ipcMain.once('file-system-write-end', () => done()) }) - it('registers secure, when {secure: true}', function (done) { - // the CacheStorage API will only work if secure == true + it('registers secure, when {secure: true}', (done) => { let filePath = path.join(__dirname, 'fixtures', 'pages', 'cache-storage.html') - const handler = function (request, callback) { - callback({path: filePath}) - } + const handler = (request, callback) => callback({path: filePath}) ipcMain.once('success', () => done()) ipcMain.once('failure', (event, err) => done(err)) - protocol.registerFileProtocol(standardScheme, handler, function (error) { + protocol.registerFileProtocol(standardScheme, handler, (error) => { if (error) return done(error) w.loadURL(origin) })