This commit is contained in:
Jeremy Apthorp 2019-08-04 17:47:42 -07:00 коммит произвёл Cheng Zhao
Родитель f08be2162a
Коммит 49f2071e22
1 изменённых файлов: 33 добавлений и 34 удалений

Просмотреть файл

@ -1,28 +1,27 @@
const chai = require('chai') import { expect } from 'chai'
const dirtyChai = require('dirty-chai') import * as http from 'http'
const http = require('http') import * as fs from 'fs'
const fs = require('fs') import * as os from 'os'
const os = require('os') import * as path from 'path'
const path = require('path') import * as ChildProcess from 'child_process'
const ChildProcess = require('child_process') import { session, net } from 'electron'
const {session, net} = require('electron') import { Socket, AddressInfo } from 'net';
const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log') const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log')
const dumpFile = path.join(os.tmpdir(), 'net_log.json') const dumpFile = path.join(os.tmpdir(), 'net_log.json')
const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json') const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json')
const { expect } = chai
chai.use(dirtyChai)
const isCI = global.isCI
const testNetLog = () => session.fromPartition('net-log').netLog const testNetLog = () => session.fromPartition('net-log').netLog
describe('netLog module', () => { describe('netLog module', () => {
let server let server: http.Server
const connections = new Set() let serverUrl: string
const connections: Set<Socket> = new Set()
before(done => { before(done => {
server = http.createServer() server = http.createServer()
server.listen(0, '127.0.0.1', () => { server.listen(0, '127.0.0.1', () => {
server.url = `http://127.0.0.1:${server.address().port}` serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
done() done()
}) })
server.on('connection', (connection) => { server.on('connection', (connection) => {
@ -41,13 +40,13 @@ describe('netLog module', () => {
connection.destroy() connection.destroy()
} }
server.close(() => { server.close(() => {
server = null server = null as any
done() done()
}) })
}) })
beforeEach(() => { beforeEach(() => {
expect(testNetLog().currentlyLogging).to.be.false() expect(testNetLog().currentlyLogging).to.be.false('currently logging')
}) })
afterEach(() => { afterEach(() => {
try { try {
@ -60,19 +59,19 @@ describe('netLog module', () => {
} catch (e) { } catch (e) {
// Ignore error // Ignore error
} }
expect(testNetLog().currentlyLogging).to.be.false() expect(testNetLog().currentlyLogging).to.be.false('currently logging')
}) })
it('should begin and end logging to file when .startLogging() and .stopLogging() is called', async () => { it('should begin and end logging to file when .startLogging() and .stopLogging() is called', async () => {
await testNetLog().startLogging(dumpFileDynamic) await testNetLog().startLogging(dumpFileDynamic)
expect(testNetLog().currentlyLogging).to.be.true() expect(testNetLog().currentlyLogging).to.be.true('currently logging')
expect(testNetLog().currentlyLoggingPath).to.equal(dumpFileDynamic) expect(testNetLog().currentlyLoggingPath).to.equal(dumpFileDynamic)
await testNetLog().stopLogging() await testNetLog().stopLogging()
expect(fs.existsSync(dumpFileDynamic)).to.be.true() expect(fs.existsSync(dumpFileDynamic)).to.be.true('currently logging')
}) })
it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => { it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => {
@ -81,17 +80,17 @@ describe('netLog module', () => {
it('should throw an error when .startLogging() is called with an invalid argument', () => { it('should throw an error when .startLogging() is called with an invalid argument', () => {
expect(() => testNetLog().startLogging('')).to.throw() expect(() => testNetLog().startLogging('')).to.throw()
expect(() => testNetLog().startLogging(null)).to.throw() expect(() => testNetLog().startLogging(null as any)).to.throw()
expect(() => testNetLog().startLogging([])).to.throw() expect(() => testNetLog().startLogging([] as any)).to.throw()
expect(() => testNetLog().startLogging('aoeu', {captureMode: 'aoeu'})).to.throw() expect(() => testNetLog().startLogging('aoeu', {captureMode: 'aoeu' as any})).to.throw()
expect(() => testNetLog().startLogging('aoeu', {maxFileSize: null})).to.throw() expect(() => testNetLog().startLogging('aoeu', {maxFileSize: null as any})).to.throw()
}) })
it('should include cookies when requested', async () => { it('should include cookies when requested', async () => {
await testNetLog().startLogging(dumpFileDynamic, {captureMode: "includeSensitive"}) await testNetLog().startLogging(dumpFileDynamic, {captureMode: "includeSensitive"})
const unique = require('uuid').v4() const unique = require('uuid').v4()
await new Promise((resolve) => { await new Promise((resolve) => {
const req = net.request(server.url) const req = net.request(serverUrl)
req.setHeader('Cookie', `foo=${unique}`) req.setHeader('Cookie', `foo=${unique}`)
req.on('response', (response) => { req.on('response', (response) => {
response.on('data', () => {}) // https://github.com/electron/electron/issues/19214 response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
@ -109,7 +108,7 @@ describe('netLog module', () => {
await testNetLog().startLogging(dumpFileDynamic, {captureMode: "everything"}) await testNetLog().startLogging(dumpFileDynamic, {captureMode: "everything"})
const unique = require('uuid').v4() const unique = require('uuid').v4()
await new Promise((resolve) => { await new Promise((resolve) => {
const req = net.request({method: 'POST', url: server.url}) const req = net.request({method: 'POST', url: serverUrl})
req.on('response', (response) => { req.on('response', (response) => {
response.on('data', () => {}) // https://github.com/electron/electron/issues/19214 response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
response.on('end', () => resolve()) response.on('end', () => resolve())
@ -119,7 +118,7 @@ describe('netLog module', () => {
await testNetLog().stopLogging() await testNetLog().stopLogging()
expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists') expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists')
const dump = fs.readFileSync(dumpFileDynamic, 'utf8') const dump = fs.readFileSync(dumpFileDynamic, 'utf8')
expect(JSON.parse(dump).events.some(x => x.params && x.params.bytes && Buffer.from(x.params.bytes, 'base64').includes(unique))).to.be.true('uuid present in dump') expect(JSON.parse(dump).events.some((x: any) => x.params && x.params.bytes && Buffer.from(x.params.bytes, 'base64').includes(unique))).to.be.true('uuid present in dump')
}) })
it('should begin and end logging automatically when --log-net-log is passed', done => { it('should begin and end logging automatically when --log-net-log is passed', done => {
@ -131,13 +130,13 @@ describe('netLog module', () => {
const appProcess = ChildProcess.spawn(process.execPath, const appProcess = ChildProcess.spawn(process.execPath,
[appPath], { [appPath], {
env: { env: {
TEST_REQUEST_URL: server.url, TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE: dumpFile TEST_DUMP_FILE: dumpFile
} }
}) })
appProcess.once('exit', () => { appProcess.once('exit', () => {
expect(fs.existsSync(dumpFile)).to.be.true() expect(fs.existsSync(dumpFile)).to.be.true('dump file exists')
done() done()
}) })
}) })
@ -151,16 +150,16 @@ describe('netLog module', () => {
const appProcess = ChildProcess.spawn(process.execPath, const appProcess = ChildProcess.spawn(process.execPath,
[appPath], { [appPath], {
env: { env: {
TEST_REQUEST_URL: server.url, TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE: dumpFile, TEST_DUMP_FILE: dumpFile,
TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic, TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
TEST_MANUAL_STOP: true TEST_MANUAL_STOP: 'true'
} }
}) })
appProcess.once('exit', () => { appProcess.once('exit', () => {
expect(fs.existsSync(dumpFile)).to.be.true() expect(fs.existsSync(dumpFile)).to.be.true('dump file exists')
expect(fs.existsSync(dumpFileDynamic)).to.be.true() expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists')
done() done()
}) })
}) })
@ -174,13 +173,13 @@ describe('netLog module', () => {
const appProcess = ChildProcess.spawn(process.execPath, const appProcess = ChildProcess.spawn(process.execPath,
[appPath], { [appPath], {
env: { env: {
TEST_REQUEST_URL: server.url, TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
} }
}) })
appProcess.once('close', () => { appProcess.once('close', () => {
expect(fs.existsSync(dumpFileDynamic)).to.be.true() expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists')
done() done()
}) })
}) })