electron/spec/api-net-log-spec.js
Zhuo Lu ab24a1e36d feat: netLog API for dynamic logging control (#13068)
* Introduce `net.{start|stop}Logging()`

- Slight regression right now as Electron won't automatically start logging net-logs at launch, will soon be fixed
- To implement callback for async controls

* Add `net.isLogging` & optional callback param for `net.stopLogging()`

* Fix small regression on --log-net-log

--log-net-log should work again

* Error on empty file path

* Only start with valid file path

* Remove unused var

* Allow setting log file path before URLRequestContextGetter starts logging

* Add net log tests

* Remove redundant checks

* Use brightray::NetLog

* Clean up code

* Should automatically stop listening

* 🎨 Attempt to fix styles

* Only run non-null callback

* Dump file to tmpdir

* Simplify net log spec

Spawned Electron process on Linux CI can fail to launch

* Separate netLog module

* Remove net logging test from net spec

* Add tests for netLog

* Fix header guard

* Clean up code

* Add netLog.currentlyLoggingPath

* Callback with filepath

* Add test for case when only .stopLogging() is called

* Add docs

* Reintroduce error on invalid arg

* Update copyright

* Update error message

* Juggle file path string types
2018-06-19 11:45:58 +10:00

156 lines
3.9 KiB
JavaScript

const assert = require('assert')
const http = require('http')
const fs = require('fs')
const os = require('os')
const path = require('path')
const ChildProcess = require('child_process')
const {remote} = require('electron')
const {netLog} = remote
const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log')
const dumpFile = path.join(os.tmpdir(), 'net_log.json')
const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json')
const isCI = remote.getGlobal('isCi')
describe('netLog module', () => {
let server
const connections = new Set()
before((done) => {
server = http.createServer()
server.listen(0, '127.0.0.1', () => {
server.url = `http://127.0.0.1:${server.address().port}`
done()
})
server.on('connection', (connection) => {
connections.add(connection)
connection.once('close', () => {
connections.delete(connection)
})
})
server.on('request', (request, response) => {
response.end()
})
})
after((done) => {
for (const connection of connections) {
connection.destroy()
}
server.close(() => {
server = null
done()
})
})
afterEach(() => {
try {
fs.unlinkSync(dumpFile)
fs.unlinkSync(dumpFileDynamic)
} catch (e) {
// Ignore error
}
})
it('should begin and end logging to file when .startLogging() and .stopLogging() is called', (done) => {
assert(!netLog.currentlyLogging)
assert.equal(netLog.currentlyLoggingPath, '')
netLog.startLogging(dumpFileDynamic)
assert(netLog.currentlyLogging)
assert.equal(netLog.currentlyLoggingPath, dumpFileDynamic)
netLog.stopLogging((path) => {
assert(!netLog.currentlyLogging)
assert.equal(netLog.currentlyLoggingPath, '')
assert.equal(path, dumpFileDynamic)
assert(fs.existsSync(dumpFileDynamic))
done()
})
})
it('should silence when .stopLogging() is called without calling .startLogging()', (done) => {
assert(!netLog.currentlyLogging)
assert.equal(netLog.currentlyLoggingPath, '')
netLog.stopLogging((path) => {
assert(!netLog.currentlyLogging)
assert.equal(netLog.currentlyLoggingPath, '')
assert.equal(path, '')
done()
})
})
// The following tests are skipped on Linux CI
it('should begin and end logging automatically when --log-net-log is passed', (done) => {
if (isCI && process.platform === 'linux') {
done()
return
}
let appProcess = ChildProcess.spawn(remote.process.execPath,
[appPath, `--log-net-log=${dumpFile}`], {
env: {
TEST_REQUEST_URL: server.url
}
})
appProcess.once('exit', () => {
assert(fs.existsSync(dumpFile))
done()
})
})
it('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', (done) => {
if (isCI && process.platform === 'linux') {
done()
return
}
let appProcess = ChildProcess.spawn(remote.process.execPath,
[appPath, `--log-net-log=${dumpFile}`], {
env: {
TEST_REQUEST_URL: server.url,
TEST_DUMP_FILE: dumpFileDynamic,
TEST_MANUAL_STOP: true
}
})
appProcess.stdout.on('data', (data) => {
console.log(data.toString())
})
appProcess.once('exit', () => {
assert(fs.existsSync(dumpFile))
assert(fs.existsSync(dumpFileDynamic))
done()
})
})
it('should end logging automatically when only .startLogging() is called', (done) => {
if (isCI && process.platform === 'linux') {
done()
return
}
let appProcess = ChildProcess.spawn(remote.process.execPath,
[appPath], {
env: {
TEST_REQUEST_URL: server.url,
TEST_DUMP_FILE: dumpFileDynamic
}
})
appProcess.once('exit', () => {
assert(fs.existsSync(dumpFileDynamic))
done()
})
})
})