2018-06-28 22:40:30 +00:00
const chai = require ( 'chai' )
const dirtyChai = require ( 'dirty-chai' )
2018-06-19 01:45:58 +00:00
const http = require ( 'http' )
const fs = require ( 'fs' )
const os = require ( 'os' )
const path = require ( 'path' )
const ChildProcess = require ( 'child_process' )
2018-09-13 16:10:51 +00:00
const { remote } = require ( 'electron' )
const { netLog } = remote
2018-06-19 01:45:58 +00:00
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' )
2018-09-13 16:10:51 +00:00
const { expect } = chai
2018-06-28 22:40:30 +00:00
chai . use ( dirtyChai )
2018-06-19 01:45:58 +00:00
const isCI = remote . getGlobal ( 'isCi' )
describe ( 'netLog module' , ( ) => {
let server
const connections = new Set ( )
2018-06-28 22:40:30 +00:00
before ( done => {
2018-06-19 01:45:58 +00:00
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 ( )
} )
} )
2018-06-28 22:40:30 +00:00
after ( done => {
2018-06-19 01:45:58 +00:00
for ( const connection of connections ) {
connection . destroy ( )
}
server . close ( ( ) => {
server = null
done ( )
} )
} )
afterEach ( ( ) => {
try {
fs . unlinkSync ( dumpFile )
fs . unlinkSync ( dumpFileDynamic )
} catch ( e ) {
// Ignore error
}
} )
2018-06-28 22:40:30 +00:00
it ( 'should begin and end logging to file when .startLogging() and .stopLogging() is called' , done => {
expect ( netLog . currentlyLogging ) . to . be . false ( )
expect ( netLog . currentlyLoggingPath ) . to . equal ( '' )
2018-06-19 01:45:58 +00:00
netLog . startLogging ( dumpFileDynamic )
2018-06-28 22:40:30 +00:00
expect ( netLog . currentlyLogging ) . to . be . true ( )
expect ( netLog . currentlyLoggingPath ) . to . equal ( dumpFileDynamic )
2018-06-19 01:45:58 +00:00
netLog . stopLogging ( ( path ) => {
2018-06-28 22:40:30 +00:00
expect ( netLog . currentlyLogging ) . to . be . false ( )
expect ( netLog . currentlyLoggingPath ) . to . equal ( '' )
2018-06-19 01:45:58 +00:00
2018-06-28 22:40:30 +00:00
expect ( path ) . to . equal ( dumpFileDynamic )
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
2018-06-28 22:40:30 +00:00
it ( 'should silence when .stopLogging() is called without calling .startLogging()' , done => {
expect ( netLog . currentlyLogging ) . to . be . false ( )
expect ( netLog . currentlyLoggingPath ) . to . equal ( '' )
2018-06-19 01:45:58 +00:00
2018-06-28 22:40:30 +00:00
netLog . stopLogging ( path => {
expect ( netLog . currentlyLogging ) . to . be . false ( )
expect ( netLog . currentlyLoggingPath ) . to . equal ( '' )
2018-06-19 01:45:58 +00:00
2018-06-28 22:40:30 +00:00
expect ( path ) . to . equal ( '' )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
// The following tests are skipped on Linux CI
2018-06-28 22:40:30 +00:00
it ( 'should begin and end logging automatically when --log-net-log is passed' , done => {
2018-06-19 01:45:58 +00:00
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' , ( ) => {
2018-06-28 22:40:30 +00:00
expect ( fs . existsSync ( dumpFile ) ) . to . be . true ( )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
2018-06-28 22:40:30 +00:00
it ( 'should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called' , done => {
2018-06-19 01:45:58 +00:00
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
}
} )
2018-06-28 22:40:30 +00:00
appProcess . stdout . on ( 'data' , data => {
2018-06-19 01:45:58 +00:00
console . log ( data . toString ( ) )
} )
appProcess . once ( 'exit' , ( ) => {
2018-06-28 22:40:30 +00:00
expect ( fs . existsSync ( dumpFile ) ) . to . be . true ( )
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
2018-06-28 22:40:30 +00:00
it ( 'should end logging automatically when only .startLogging() is called' , done => {
2018-06-19 01:45:58 +00:00
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' , ( ) => {
2018-06-28 22:40:30 +00:00
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
} )