electron/spec/api-protocol-spec.js

1064 lines
32 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
const http = require('http')
const path = require('path')
const qs = require('querystring')
const {closeWindow} = require('./window-helpers')
2017-10-26 20:30:07 +00:00
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-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 () {
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(error, null)
2017-10-26 20:30:07 +00:00
protocol.registerBufferProtocol(protocolName, emptyHandler, (error) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(error, null)
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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('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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(error, null)
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data, status, request) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
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('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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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', () => {
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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(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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data, status, request) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
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('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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, String(fileContent))
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data, status, request) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, String(fileContent))
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
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
})
})
})
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, String(fileContent))
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 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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, String(normalContent))
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(req.headers.accept, '')
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
const handler = (request, callback) => callback({url})
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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 invalid url', (done) => {
const handler = (request, callback) => callback({url: 'url'})
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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',
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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`
const handler = (request, callback) => callback({url: redirectURL})
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', () => {
2016-05-29 08:13:26 +00:00
assert.equal(contents.getURL(), url)
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) => {
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}`))
}
})
})
})
})
2017-10-26 20:30:07 +00:00
describe('protocol.isProtocolHandled', () => {
it('returns true for about:', (done) => {
protocol.isProtocolHandled('about', (result) => {
2016-11-09 05:30:50 +00:00
assert.equal(result, true)
done()
})
})
2017-10-26 20:30:07 +00:00
it('returns true for file:', (done) => {
protocol.isProtocolHandled('file', (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, true)
done()
})
})
2017-10-26 20:30:07 +00:00
it('returns true for http:', (done) => {
protocol.isProtocolHandled('http', (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, true)
done()
})
})
2017-10-26 20:30:07 +00:00
it('returns true for https:', (done) => {
protocol.isProtocolHandled('https', (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, true)
done()
})
})
2017-10-26 20:30:07 +00:00
it('returns false when scheme is not registered', (done) => {
protocol.isProtocolHandled('no-exist', (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, false)
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(error, null)
2017-10-26 20:30:07 +00:00
protocol.isProtocolHandled(protocolName, (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, true)
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(error, null)
2017-10-26 20:30:07 +00:00
protocol.isProtocolHandled('http', (result) => {
2016-03-25 20:03:49 +00:00
assert.equal(result, true)
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(error, null)
2017-10-26 20:30:07 +00:00
protocol.interceptBufferProtocol('http', emptyHandler, (error) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(error, null)
done()
})
})
})
2017-10-26 20:30:07 +00:00
it('does not crash when handler is called twice', (done) => {
var 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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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
})
})
})
it('sends error when callback is called with nothing', function (done) {
if (process.env.TRAVIS === 'true') {
// FIXME(alexeykuzmin): Skip the test.
// this.skip()
return 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',
cache: false,
2017-10-26 20:30:07 +00:00
success: () => done('request succeeded but it should not'),
error: (xhr, errorType) => {
2016-03-25 20:03:49 +00:00
assert.equal(errorType, 'error')
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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 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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(typeof data, 'object')
assert.equal(data.value, 1)
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.toString()
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',
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) => {
2016-03-25 20:03:49 +00:00
assert.deepEqual(qs.parse(data), postData)
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) => {
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',
cache: false,
2017-10-26 20:30:07 +00:00
success: (data) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, text)
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',
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) => {
2016-03-25 20:03:49 +00:00
assert.equal(data, $.param(postData))
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',
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) => {
2016-03-25 20:03:49 +00:00
assert.deepEqual(qs.parse(data), postData)
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 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})
})
2017-10-26 20:30:07 +00:00
const handler = (request, callback) => {
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(() => {
done('request succeeded but it should not')
2017-10-26 20:30:07 +00:00
}).catch(() => {
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) => {
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}`))
}
})
})
})
})
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) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(error, null)
done()
})
})
2017-10-26 20:30:07 +00:00
it('returns error when scheme is not intercepted', (done) => {
protocol.uninterceptProtocol('http', (error) => {
2016-03-25 20:03:49 +00:00
assert.notEqual(error, null)
done()
})
})
})
2017-10-26 20:30:07 +00:00
describe('protocol.registerStandardSchemes', () => {
const standardScheme = remote.getGlobal('standardScheme')
2017-10-26 20:30:07 +00:00
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" />'
2017-10-26 20:30:07 +00:00
let w = null
let success = null
2017-10-26 20:30:07 +00:00
beforeEach(() => {
w = new BrowserWindow({show: false})
success = false
})
2017-10-26 20:30:07 +00:00
afterEach((done) => {
protocol.unregisterProtocol(standardScheme, () => {
closeWindow(w).then(() => {
w = null
done()
})
})
})
2017-10-26 20:30:07 +00:00
it('resolves relative resources', (done) => {
const handler = (request, callback) => {
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', () => {
assert(success)
done()
})
w.loadURL(origin)
})
})
2017-10-26 20:30:07 +00:00
it('resolves absolute resources', (done) => {
const handler = (request, callback) => {
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', () => {
assert(success)
done()
})
w.loadURL(origin)
})
2016-06-29 16:37:10 +00:00
})
2017-10-26 20:30:07 +00:00
it('can have fetch working in it', (done) => {
const content = '<html><script>fetch("http://github.com")</script></html>'
2017-10-26 20:30:07 +00:00
const handler = (request, callback) => callback({data: content, mimeType: 'text/html'})
protocol.registerStringProtocol(standardScheme, handler, (error) => {
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())
w.loadURL(origin)
})
})
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) => {
2016-08-24 01:23:14 +00:00
let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
2017-10-26 20:30:07 +00:00
const handler = (request, callback) => callback({path: filePath})
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())
})
2017-10-26 20:30:07 +00:00
it('registers secure, when {secure: true}', (done) => {
let filePath = path.join(__dirname, 'fixtures', 'pages', 'cache-storage.html')
2017-10-26 20:30:07 +00:00
const handler = (request, callback) => callback({path: filePath})
ipcMain.once('success', () => done())
ipcMain.once('failure', (event, err) => done(err))
2017-10-26 20:30:07 +00:00
protocol.registerFileProtocol(standardScheme, handler, (error) => {
if (error) return done(error)
w.loadURL(origin)
})
})
})
2016-03-25 20:03:49 +00:00
})