2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
|
|
|
const http = require('http')
|
|
|
|
const path = require('path')
|
|
|
|
const qs = require('querystring')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
|
|
|
const { remote } = require('electron')
|
|
|
|
const { BrowserWindow, ipcMain, protocol, session, webContents } = remote
|
2017-11-10 12:35:32 +00:00
|
|
|
// 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')
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* The whole protocol API doesn't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol module', () => {
|
|
|
|
const protocolName = 'sp'
|
|
|
|
const text = 'valar morghulis'
|
|
|
|
const postData = {
|
2016-01-12 02:40:23 +00:00
|
|
|
name: 'post test',
|
|
|
|
type: 'string'
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:35:32 +00:00
|
|
|
function delay (ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStream (chunkSize = text.length, data = text) {
|
|
|
|
const body = stream.PassThrough()
|
|
|
|
|
|
|
|
async function sendChunks () {
|
2017-11-23 22:22:31 +00:00
|
|
|
let buf = Buffer.from(data)
|
2017-11-10 12:35:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
afterEach((done) => {
|
|
|
|
protocol.unregisterProtocol(protocolName, () => {
|
|
|
|
protocol.uninterceptProtocol('http', () => done())
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.register(Any)Protocol', () => {
|
|
|
|
const emptyHandler = (request, callback) => callback()
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('throws error when scheme is already registered', (done) => {
|
|
|
|
protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error, null)
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(error, null)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('does not crash when handler is called twice', (done) => {
|
|
|
|
const doubleHandler = (request, callback) => {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
callback(text)
|
|
|
|
callback()
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-01-19 19:31:47 +00:00
|
|
|
// Ignore error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerStringProtocol(protocolName, doubleHandler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends error when callback is called with nothing', (done) => {
|
|
|
|
protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
return done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('does not crash when callback is called in next tick', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
|
|
|
setImmediate(() => callback(text))
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerStringProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.unregisterProtocol', () => {
|
|
|
|
it('returns error when scheme does not exist', (done) => {
|
|
|
|
protocol.unregisterProtocol('not-exist', (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(error, null)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
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)
|
2016-02-17 01:46:44 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', (done) => {
|
|
|
|
const handler = (request, callback) => callback(text)
|
|
|
|
protocol.registerStringProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data, status, request) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
|
|
|
assert.strictEqual(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends object as response', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
data: text,
|
|
|
|
mimeType: 'text/html'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerStringProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
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)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerBufferProtocol', () => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const buffer = Buffer.from(text)
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends Buffer as response', (done) => {
|
|
|
|
const handler = (request, callback) => callback(buffer)
|
|
|
|
protocol.registerBufferProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', (done) => {
|
|
|
|
const handler = (request, callback) => callback(buffer)
|
|
|
|
protocol.registerBufferProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data, status, request) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
|
|
|
assert.strictEqual(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends object as response', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
data: buffer,
|
|
|
|
mimeType: 'text/html'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerBufferProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('fails when sending string', (done) => {
|
|
|
|
const handler = (request, callback) => callback(text)
|
|
|
|
protocol.registerBufferProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
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)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends file path as response', (done) => {
|
|
|
|
const handler = (request, callback) => callback(filePath)
|
|
|
|
protocol.registerFileProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:46:44 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, String(fileContent))
|
2016-03-25 20:03:49 +00:00
|
|
|
return done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', (done) => {
|
|
|
|
const handler = (request, callback) => callback(filePath)
|
|
|
|
protocol.registerFileProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data, status, request) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, String(fileContent))
|
|
|
|
assert.strictEqual(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
done(error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('sends object as response', (done) => {
|
|
|
|
const handler = (request, callback) => callback({ path: filePath })
|
|
|
|
protocol.registerFileProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, String(fileContent))
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can send normal file', (done) => {
|
|
|
|
const handler = (request, callback) => callback(normalPath)
|
|
|
|
protocol.registerFileProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, String(normalContent))
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
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)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('fails when sending unsupported content', (done) => {
|
|
|
|
const handler = (request, callback) => callback(new Date())
|
|
|
|
protocol.registerFileProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerHttpProtocol', () => {
|
|
|
|
it('sends url as response', (done) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(req.headers.accept, '')
|
2016-03-25 20:03:49 +00:00
|
|
|
res.end(text)
|
|
|
|
server.close()
|
|
|
|
})
|
2017-10-26 20:30:07 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = server.address().port
|
|
|
|
const url = 'http://127.0.0.1:' + port
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ url })
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('fails when sending invalid url', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ url: 'url' })
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('fails when sending unsupported content', (done) => {
|
|
|
|
const handler = (request, callback) => callback(new Date())
|
|
|
|
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: protocolName + '://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
done('request succeeded but it should not')
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-29 08:13:26 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('works when target URL redirects', (done) => {
|
|
|
|
let contents = null
|
|
|
|
const server = http.createServer((req, res) => {
|
2016-06-29 16:37:10 +00:00
|
|
|
if (req.url === '/serverRedirect') {
|
2016-05-29 08:13:26 +00:00
|
|
|
res.statusCode = 301
|
2017-10-26 20:30:07 +00:00
|
|
|
res.setHeader('Location', `http://${req.rawHeaders[1]}`)
|
2016-05-29 08:13:26 +00:00
|
|
|
res.end()
|
|
|
|
} else {
|
|
|
|
res.end(text)
|
|
|
|
}
|
|
|
|
})
|
2017-10-26 20:30:07 +00:00
|
|
|
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`
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ url: redirectURL })
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-05-29 08:13:26 +00:00
|
|
|
contents = webContents.create({})
|
2017-10-26 20:30:07 +00:00
|
|
|
contents.on('did-finish-load', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(contents.getURL(), url)
|
2016-05-29 08:13:26 +00:00
|
|
|
server.close()
|
|
|
|
contents.destroy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
contents.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2017-11-10 12:35:32 +00:00
|
|
|
done()
|
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
|
|
|
done(error || new Error(`Request failed: ${xhr.status}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sends object as response', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ data: getStream() })
|
2017-11-10 12:35:32 +00:00
|
|
|
protocol.registerStreamProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
$.ajax({
|
|
|
|
url: protocolName + '://fake-host',
|
|
|
|
cache: false,
|
|
|
|
success: (data, _, request) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(request.status, 200)
|
|
|
|
assert.strictEqual(data, text)
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(request.status, 200)
|
2018-10-25 00:34:20 +00:00
|
|
|
assert.strictEqual(request.getResponseHeader('x-electron'), 'a, b')
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(request.status, 204)
|
|
|
|
assert.strictEqual(data, undefined)
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data['x-return-headers'], 'yes')
|
2017-11-10 12:35:32 +00:00
|
|
|
done()
|
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
|
|
|
done(error || new Error(`Request failed: ${xhr.status}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-10-25 00:34:20 +00:00
|
|
|
|
|
|
|
it('returns response multiple response headers with the same name', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
|
|
|
callback({
|
|
|
|
headers: {
|
|
|
|
'header1': ['value1', 'value2'],
|
|
|
|
'header2': 'value3'
|
|
|
|
},
|
|
|
|
data: getStream()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol.registerStreamProtocol(protocolName, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
$.ajax({
|
|
|
|
url: protocolName + '://fake-host',
|
|
|
|
cache: false,
|
|
|
|
success: (data, status, request) => {
|
|
|
|
// SUBTLE: when the response headers have multiple values it
|
|
|
|
// separates values by ", ". When the response headers are incorrectly
|
|
|
|
// converting an array to a string it separates values by ",".
|
|
|
|
assert.strictEqual(request.getAllResponseHeaders(), 'header1: value1, value2\r\nheader2: value3\r\n')
|
|
|
|
done()
|
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
|
|
|
done(error || new Error(`Request failed: ${xhr.status}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-11-10 12:35:32 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.isProtocolHandled', () => {
|
|
|
|
it('returns true for about:', (done) => {
|
|
|
|
protocol.isProtocolHandled('about', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-11-09 05:30:50 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns true for file:', (done) => {
|
|
|
|
protocol.isProtocolHandled('file', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns true for http:', (done) => {
|
|
|
|
protocol.isProtocolHandled('http', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns true for https:', (done) => {
|
|
|
|
protocol.isProtocolHandled('https', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns false when scheme is not registered', (done) => {
|
|
|
|
protocol.isProtocolHandled('no-exist', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, false)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns true for custom protocol', (done) => {
|
|
|
|
const emptyHandler = (request, callback) => callback()
|
|
|
|
protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error, null)
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.isProtocolHandled(protocolName, (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns true for intercepted protocol', (done) => {
|
|
|
|
const emptyHandler = (request, callback) => callback()
|
|
|
|
protocol.interceptStringProtocol('http', emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error, null)
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.isProtocolHandled('http', (result) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.intercept(Any)Protocol', () => {
|
|
|
|
const emptyHandler = (request, callback) => callback()
|
|
|
|
it('throws error when scheme is already intercepted', (done) => {
|
|
|
|
protocol.interceptStringProtocol('http', emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error, null)
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptBufferProtocol('http', emptyHandler, (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(error, null)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('does not crash when handler is called twice', (done) => {
|
2018-09-28 23:17:00 +00:00
|
|
|
const doubleHandler = (request, callback) => {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
callback(text)
|
|
|
|
callback()
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-01-19 19:31:47 +00:00
|
|
|
// Ignore error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptStringProtocol('http', doubleHandler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('sends error when callback is called with nothing', function (done) {
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptBufferProtocol('http', emptyHandler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: () => done('request succeeded but it should not'),
|
|
|
|
error: (xhr, errorType) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(errorType, 'error')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
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)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can set content-type', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
mimeType: 'application/json',
|
|
|
|
data: '{"value": 1}'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptStringProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(typeof data, 'object')
|
|
|
|
assert.strictEqual(data.value, 1)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can receive post data', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
|
|
|
const uploadData = request.uploadData[0].bytes.toString()
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ data: uploadData })
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptStringProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2016-03-25 20:03:49 +00:00
|
|
|
type: 'POST',
|
2016-01-12 02:40:23 +00:00
|
|
|
data: postData,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-14 06:46:42 +00:00
|
|
|
assert.deepStrictEqual({ ...qs.parse(data) }, postData)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.interceptBufferProtocol', () => {
|
|
|
|
it('can intercept http protocol', (done) => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const handler = (request, callback) => callback(Buffer.from(text))
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptBufferProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can receive post data', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
|
|
|
const uploadData = request.uploadData[0].bytes
|
2016-03-25 20:03:49 +00:00
|
|
|
callback(uploadData)
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptBufferProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2016-03-25 20:03:49 +00:00
|
|
|
type: 'POST',
|
2016-01-12 02:40:23 +00:00
|
|
|
data: postData,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, $.param(postData))
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.interceptHttpProtocol', () => {
|
|
|
|
it('can send POST request', (done) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
let body = ''
|
|
|
|
req.on('data', (chunk) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
body += chunk
|
|
|
|
})
|
2017-10-26 20:30:07 +00:00
|
|
|
req.on('end', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
res.end(body)
|
|
|
|
})
|
|
|
|
server.close()
|
|
|
|
})
|
2017-10-26 20:30:07 +00:00
|
|
|
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 = {
|
2016-01-12 02:40:23 +00:00
|
|
|
url: url,
|
|
|
|
method: 'POST',
|
|
|
|
uploadData: {
|
|
|
|
contentType: 'application/x-www-form-urlencoded',
|
|
|
|
data: request.uploadData[0].bytes.toString()
|
|
|
|
},
|
|
|
|
session: null
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
callback(data)
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptHttpProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
2016-02-17 01:39:11 +00:00
|
|
|
$.ajax({
|
2016-03-25 20:03:49 +00:00
|
|
|
url: 'http://fake-host',
|
2016-07-04 09:08:41 +00:00
|
|
|
cache: false,
|
2016-03-25 20:03:49 +00:00
|
|
|
type: 'POST',
|
2016-01-12 02:40:23 +00:00
|
|
|
data: postData,
|
2017-10-26 20:30:07 +00:00
|
|
|
success: (data) => {
|
2018-09-14 06:46:42 +00:00
|
|
|
assert.deepStrictEqual({ ...qs.parse(data) }, postData)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2017-10-26 20:30:07 +00:00
|
|
|
error: (xhr, errorType, error) => done(error)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-11-27 19:07:50 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can use custom session', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const customSession = session.fromPartition('custom-ses', { cache: false })
|
2017-10-26 20:30:07 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(details.url, 'http://fake-host/')
|
|
|
|
callback({ cancel: true })
|
2016-11-27 19:07:50 +00:00
|
|
|
})
|
2017-10-26 20:30:07 +00:00
|
|
|
const handler = (request, callback) => {
|
2016-11-27 19:07:50 +00:00
|
|
|
callback({
|
|
|
|
url: request.url,
|
|
|
|
session: customSession
|
|
|
|
})
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.interceptHttpProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
fetch('http://fake-host').then(() => {
|
2016-11-27 19:07:50 +00:00
|
|
|
done('request succeeded but it should not')
|
2017-10-26 20:30:07 +00:00
|
|
|
}).catch(() => {
|
2016-11-27 19:07:50 +00:00
|
|
|
customSession.webRequest.onBeforeRequest(null)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, text)
|
2017-11-10 12:35:32 +00:00
|
|
|
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) => {
|
2018-09-14 06:46:42 +00:00
|
|
|
assert.deepStrictEqual({ ...qs.parse(data) }, postData)
|
2017-11-10 12:35:32 +00:00
|
|
|
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 {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(request.url.indexOf('http://fake-redirect'), 0)
|
2017-11-10 12:35:32 +00:00
|
|
|
callback(getStream(1, 'redirect'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protocol.interceptStreamProtocol('http', handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
$.ajax({
|
|
|
|
url: 'http://fake-host',
|
|
|
|
cache: false,
|
|
|
|
success: (data) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, 'redirect')
|
2017-11-10 12:35:32 +00:00
|
|
|
done()
|
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
|
|
|
done(error || new Error(`Request failed: ${xhr.status}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.uninterceptProtocol', () => {
|
|
|
|
it('returns error when scheme does not exist', (done) => {
|
|
|
|
protocol.uninterceptProtocol('not-exist', (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(error, null)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('returns error when scheme is not intercepted', (done) => {
|
|
|
|
protocol.uninterceptProtocol('http', (error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(error, null)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerStandardSchemes', () => {
|
2016-05-07 18:43:23 +00:00
|
|
|
const standardScheme = remote.getGlobal('standardScheme')
|
2017-10-26 20:30:07 +00:00
|
|
|
const origin = `${standardScheme}://fake-host`
|
|
|
|
const imageURL = `${origin}/test.png`
|
2016-05-05 18:34:16 +00:00
|
|
|
const filePath = path.join(__dirname, 'fixtures', 'pages', 'b.html')
|
|
|
|
const fileContent = '<img src="/test.png" />'
|
2017-10-26 20:30:07 +00:00
|
|
|
let w = null
|
|
|
|
let success = null
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
beforeEach(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w = new BrowserWindow({ show: false })
|
2016-05-05 18:34:16 +00:00
|
|
|
success = false
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
afterEach((done) => {
|
|
|
|
protocol.unregisterProtocol(standardScheme, () => {
|
|
|
|
closeWindow(w).then(() => {
|
2016-08-03 19:47:53 +00:00
|
|
|
w = null
|
|
|
|
done()
|
|
|
|
})
|
2016-05-05 18:34:16 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('resolves relative resources', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
2016-05-05 18:34:16 +00:00
|
|
|
if (request.url === imageURL) {
|
|
|
|
success = true
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
callback(filePath)
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerFileProtocol(standardScheme, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2016-05-05 18:34:16 +00:00
|
|
|
assert(success)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(origin)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('resolves absolute resources', (done) => {
|
|
|
|
const handler = (request, callback) => {
|
2016-05-05 18:34:16 +00:00
|
|
|
if (request.url === imageURL) {
|
|
|
|
success = true
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
data: fileContent,
|
|
|
|
mimeType: 'text/html'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerStringProtocol(standardScheme, handler, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2016-05-05 18:34:16 +00:00
|
|
|
assert(success)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(origin)
|
|
|
|
})
|
2016-06-29 16:37:10 +00:00
|
|
|
})
|
2016-06-08 05:44:30 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can have fetch working in it', (done) => {
|
2016-06-08 05:44:30 +00:00
|
|
|
const content = '<html><script>fetch("http://github.com")</script></html>'
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ data: content, mimeType: 'text/html' })
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerStringProtocol(standardScheme, handler, (error) => {
|
2016-06-08 05:44:30 +00:00
|
|
|
if (error) return done(error)
|
2017-10-26 20:30:07 +00:00
|
|
|
w.webContents.on('crashed', () => done('WebContents crashed'))
|
|
|
|
w.webContents.on('did-finish-load', () => done())
|
2016-06-08 05:44:30 +00:00
|
|
|
w.loadURL(origin)
|
|
|
|
})
|
2016-05-05 18:34:16 +00:00
|
|
|
})
|
2016-08-24 01:23:14 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('can access files through the FileSystem API', (done) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ path: filePath })
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerFileProtocol(standardScheme, handler, (error) => {
|
2016-08-24 01:23:14 +00:00
|
|
|
if (error) return done(error)
|
|
|
|
w.loadURL(origin)
|
|
|
|
})
|
|
|
|
ipcMain.once('file-system-error', (event, err) => done(err))
|
|
|
|
ipcMain.once('file-system-write-end', () => done())
|
|
|
|
})
|
2016-11-14 16:20:04 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('registers secure, when {secure: true}', (done) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const filePath = path.join(__dirname, 'fixtures', 'pages', 'cache-storage.html')
|
2018-09-13 16:10:51 +00:00
|
|
|
const handler = (request, callback) => callback({ path: filePath })
|
2016-11-14 16:20:04 +00:00
|
|
|
ipcMain.once('success', () => done())
|
|
|
|
ipcMain.once('failure', (event, err) => done(err))
|
2017-10-26 20:30:07 +00:00
|
|
|
protocol.registerFileProtocol(standardScheme, handler, (error) => {
|
2016-11-14 16:20:04 +00:00
|
|
|
if (error) return done(error)
|
|
|
|
w.loadURL(origin)
|
|
|
|
})
|
|
|
|
})
|
2016-05-05 18:34:16 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|