12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- const assert = require('assert')
- const http = require('http')
- const path = require('path')
- const qs = require('querystring')
- const {closeWindow} = require('./window-helpers')
- const {remote} = require('electron')
- const {BrowserWindow, ipcMain, protocol, session, webContents} = remote
- // The RPC API doesn't seem to support calling methods on remote objects very
- // well. In order to test stream protocol, we must work around this limitation
- // and use Stream instances created in the browser process.
- const stream = remote.require('stream')
- /* The whole protocol API doesn't use standard callbacks */
- /* eslint-disable standard/no-callback-literal */
- describe('protocol module', () => {
- const protocolName = 'sp'
- const text = 'valar morghulis'
- const postData = {
- name: 'post test',
- type: 'string'
- }
- function delay (ms) {
- return new Promise((resolve) => {
- setTimeout(resolve, ms)
- })
- }
- function getStream (chunkSize = text.length, data = text) {
- const body = stream.PassThrough()
- async function sendChunks () {
- let buf = Buffer.from(data)
- for (;;) {
- body.push(buf.slice(0, chunkSize))
- buf = buf.slice(chunkSize)
- if (!buf.length) {
- break
- }
- // emulate network delay
- await delay(50)
- }
- body.push(null)
- }
- sendChunks()
- return body
- }
- afterEach((done) => {
- protocol.unregisterProtocol(protocolName, () => {
- protocol.uninterceptProtocol('http', () => done())
- })
- })
- describe('protocol.register(Any)Protocol', () => {
- const emptyHandler = (request, callback) => callback()
- it('throws error when scheme is already registered', (done) => {
- protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
- assert.equal(error, null)
- protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => {
- assert.notEqual(error, null)
- done()
- })
- })
- })
- it('does not crash when handler is called twice', (done) => {
- const doubleHandler = (request, callback) => {
- try {
- callback(text)
- callback()
- } catch (error) {
- // Ignore error
- }
- }
- protocol.registerStringProtocol(protocolName, doubleHandler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- return done()
- }
- })
- })
- })
- it('does not crash when callback is called in next tick', (done) => {
- const handler = (request, callback) => {
- setImmediate(() => callback(text))
- }
- protocol.registerStringProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => done(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', () => {
- 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: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: (data, status, request) => {
- assert.equal(data, text)
- assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('sends object as response', (done) => {
- const handler = (request, callback) => {
- callback({
- data: text,
- mimeType: 'text/html'
- })
- }
- protocol.registerStringProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- })
- describe('protocol.registerBufferProtocol', () => {
- const buffer = Buffer.from(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: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: (data, status, request) => {
- assert.equal(data, text)
- assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('sends object as response', (done) => {
- const handler = (request, callback) => {
- callback({
- data: buffer,
- mimeType: 'text/html'
- })
- }
- protocol.registerBufferProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- })
- 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', (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: (data) => {
- assert.equal(data, String(fileContent))
- return done()
- },
- error: (xhr, errorType, error) => 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: (data, status, request) => {
- assert.equal(data, String(fileContent))
- assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
- done()
- },
- error: (xhr, errorType, error) => {
- 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: (data) => {
- assert.equal(data, String(fileContent))
- done()
- },
- error: (xhr, errorType, error) => 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: (data) => {
- assert.equal(data, String(normalContent))
- done()
- },
- error: (xhr, errorType, error) => 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- })
- 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', () => {
- 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: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => 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: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- 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: () => {
- done('request succeeded but it should not')
- },
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- 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.end()
- } else {
- res.end(text)
- }
- })
- 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', () => {
- assert.equal(contents.getURL(), url)
- server.close()
- contents.destroy()
- done()
- })
- contents.loadURL(url)
- })
- })
- })
- })
- describe('protocol.registerStreamProtocol', () => {
- it('sends Stream as response', (done) => {
- const handler = (request, callback) => callback(getStream())
- protocol.registerStreamProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('sends object as response', (done) => {
- const handler = (request, callback) => callback({data: getStream()})
- protocol.registerStreamProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data, _, request) => {
- assert.equal(request.status, 200)
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('sends custom response headers', (done) => {
- const handler = (request, callback) => callback({
- data: getStream(3),
- headers: {
- 'x-electron': ['a', 'b']
- }
- })
- protocol.registerStreamProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data, _, request) => {
- assert.equal(request.status, 200)
- assert.equal(request.getResponseHeader('x-electron'), 'a,b')
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('sends custom status code', (done) => {
- const handler = (request, callback) => callback({
- statusCode: 204,
- data: null
- })
- protocol.registerStreamProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- cache: false,
- success: (data, _, request) => {
- assert.equal(request.status, 204)
- assert.equal(data, undefined)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('receives request headers', (done) => {
- const handler = (request, callback) => {
- callback({
- headers: {
- 'content-type': 'application/json'
- },
- data: getStream(5, JSON.stringify(Object.assign({}, request.headers)))
- })
- }
- protocol.registerStreamProtocol(protocolName, handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: protocolName + '://fake-host',
- headers: {
- 'x-return-headers': 'yes'
- },
- cache: false,
- success: (data) => {
- assert.equal(data['x-return-headers'], 'yes')
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- })
- describe('protocol.isProtocolHandled', () => {
- it('returns true for about:', (done) => {
- protocol.isProtocolHandled('about', (result) => {
- assert.equal(result, true)
- done()
- })
- })
- it('returns true for file:', (done) => {
- protocol.isProtocolHandled('file', (result) => {
- assert.equal(result, true)
- done()
- })
- })
- it('returns true for http:', (done) => {
- protocol.isProtocolHandled('http', (result) => {
- assert.equal(result, true)
- done()
- })
- })
- it('returns true for https:', (done) => {
- protocol.isProtocolHandled('https', (result) => {
- assert.equal(result, true)
- done()
- })
- })
- 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', (done) => {
- const emptyHandler = (request, callback) => callback()
- protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
- assert.equal(error, null)
- protocol.isProtocolHandled(protocolName, (result) => {
- assert.equal(result, true)
- done()
- })
- })
- })
- it('returns true for intercepted protocol', (done) => {
- const emptyHandler = (request, callback) => callback()
- protocol.interceptStringProtocol('http', emptyHandler, (error) => {
- assert.equal(error, null)
- protocol.isProtocolHandled('http', (result) => {
- assert.equal(result, true)
- done()
- })
- })
- })
- })
- 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, (error) => {
- assert.notEqual(error, null)
- done()
- })
- })
- })
- it('does not crash when handler is called twice', (done) => {
- var doubleHandler = (request, callback) => {
- try {
- callback(text)
- callback()
- } catch (error) {
- // Ignore error
- }
- }
- protocol.interceptStringProtocol('http', doubleHandler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('sends error when callback is called with nothing', function (done) {
- protocol.interceptBufferProtocol('http', emptyHandler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: () => done('request succeeded but it should not'),
- error: (xhr, errorType) => {
- assert.equal(errorType, 'error')
- done()
- }
- })
- })
- })
- })
- 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: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('can set content-type', (done) => {
- const handler = (request, callback) => {
- callback({
- mimeType: 'application/json',
- data: '{"value": 1}'
- })
- }
- protocol.interceptStringProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(typeof data, 'object')
- assert.equal(data.value, 1)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('can receive post data', (done) => {
- const handler = (request, callback) => {
- const uploadData = request.uploadData[0].bytes.toString()
- callback({data: uploadData})
- }
- protocol.interceptStringProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- type: 'POST',
- data: postData,
- success: (data) => {
- assert.deepEqual(qs.parse(data), postData)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- })
- describe('protocol.interceptBufferProtocol', () => {
- it('can intercept http protocol', (done) => {
- const handler = (request, callback) => callback(Buffer.from(text))
- protocol.interceptBufferProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- it('can receive post data', (done) => {
- const handler = (request, callback) => {
- const uploadData = request.uploadData[0].bytes
- callback(uploadData)
- }
- protocol.interceptBufferProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- type: 'POST',
- data: postData,
- success: (data) => {
- assert.equal(data, $.param(postData))
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- })
- 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', () => {
- res.end(body)
- })
- server.close()
- })
- 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: {
- contentType: 'application/x-www-form-urlencoded',
- data: request.uploadData[0].bytes.toString()
- },
- session: null
- }
- callback(data)
- }
- protocol.interceptHttpProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- type: 'POST',
- data: postData,
- success: (data) => {
- assert.deepEqual(qs.parse(data), postData)
- done()
- },
- error: (xhr, errorType, error) => done(error)
- })
- })
- })
- })
- 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 = (request, callback) => {
- callback({
- url: request.url,
- session: customSession
- })
- }
- protocol.interceptHttpProtocol('http', handler, (error) => {
- if (error) return done(error)
- fetch('http://fake-host').then(() => {
- done('request succeeded but it should not')
- }).catch(() => {
- customSession.webRequest.onBeforeRequest(null)
- done()
- })
- })
- })
- })
- describe('protocol.interceptStreamProtocol', () => {
- it('can intercept http protocol', (done) => {
- const handler = (request, callback) => callback(getStream())
- protocol.interceptStreamProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, text)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('can receive post data', (done) => {
- const handler = (request, callback) => {
- callback(getStream(3, request.uploadData[0].bytes.toString()))
- }
- protocol.interceptStreamProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- type: 'POST',
- data: postData,
- success: (data) => {
- assert.deepEqual(qs.parse(data), postData)
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- it('can execute redirects', (done) => {
- const handler = (request, callback) => {
- if (request.url.indexOf('http://fake-host') === 0) {
- setTimeout(() => {
- callback({
- data: null,
- statusCode: 302,
- headers: {
- Location: 'http://fake-redirect'
- }
- })
- }, 300)
- } else {
- assert.equal(request.url.indexOf('http://fake-redirect'), 0)
- callback(getStream(1, 'redirect'))
- }
- }
- protocol.interceptStreamProtocol('http', handler, (error) => {
- if (error) return done(error)
- $.ajax({
- url: 'http://fake-host',
- cache: false,
- success: (data) => {
- assert.equal(data, 'redirect')
- done()
- },
- error: (xhr, errorType, error) => {
- done(error || new Error(`Request failed: ${xhr.status}`))
- }
- })
- })
- })
- })
- 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', (done) => {
- protocol.uninterceptProtocol('http', (error) => {
- assert.notEqual(error, null)
- done()
- })
- })
- })
- describe('protocol.registerStandardSchemes', () => {
- const standardScheme = remote.getGlobal('standardScheme')
- const origin = `${standardScheme}://fake-host`
- const imageURL = `${origin}/test.png`
- const filePath = path.join(__dirname, 'fixtures', 'pages', 'b.html')
- const fileContent = '<img src="/test.png" />'
- let w = null
- let success = null
- beforeEach(() => {
- w = new BrowserWindow({show: false})
- success = false
- })
- afterEach((done) => {
- protocol.unregisterProtocol(standardScheme, () => {
- closeWindow(w).then(() => {
- w = null
- done()
- })
- })
- })
- it('resolves relative resources', (done) => {
- const handler = (request, callback) => {
- if (request.url === imageURL) {
- success = true
- callback()
- } else {
- callback(filePath)
- }
- }
- protocol.registerFileProtocol(standardScheme, handler, (error) => {
- if (error) return done(error)
- w.webContents.on('did-finish-load', () => {
- assert(success)
- done()
- })
- w.loadURL(origin)
- })
- })
- it('resolves absolute resources', (done) => {
- const handler = (request, callback) => {
- if (request.url === imageURL) {
- success = true
- callback()
- } else {
- callback({
- data: fileContent,
- mimeType: 'text/html'
- })
- }
- }
- protocol.registerStringProtocol(standardScheme, handler, (error) => {
- if (error) return done(error)
- w.webContents.on('did-finish-load', () => {
- assert(success)
- done()
- })
- w.loadURL(origin)
- })
- })
- it('can have fetch working in it', (done) => {
- const content = '<html><script>fetch("http://github.com")</script></html>'
- const handler = (request, callback) => callback({data: content, mimeType: 'text/html'})
- protocol.registerStringProtocol(standardScheme, handler, (error) => {
- if (error) return done(error)
- 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', (done) => {
- let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
- const handler = (request, callback) => callback({path: filePath})
- protocol.registerFileProtocol(standardScheme, handler, (error) => {
- if (error) return done(error)
- w.loadURL(origin)
- })
- ipcMain.once('file-system-error', (event, err) => done(err))
- ipcMain.once('file-system-write-end', () => done())
- })
- it('registers secure, when {secure: true}', (done) => {
- let filePath = path.join(__dirname, 'fixtures', 'pages', 'cache-storage.html')
- const handler = (request, callback) => callback({path: filePath})
- ipcMain.once('success', () => done())
- ipcMain.once('failure', (event, err) => done(err))
- protocol.registerFileProtocol(standardScheme, handler, (error) => {
- if (error) return done(error)
- w.loadURL(origin)
- })
- })
- })
- })
|