2018-06-19 01:45:58 +00:00
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' )
2018-11-26 23:38:33 +00:00
const { remote } = require ( 'electron' )
const { session } = 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' )
const isCI = remote . getGlobal ( 'isCi' )
2018-11-26 23:38:33 +00:00
const netLog = session . fromPartition ( 'net-log' ) . netLog
2018-06-19 01:45:58 +00:00
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 {
2018-11-26 23:38:33 +00:00
if ( fs . existsSync ( dumpFile ) ) {
fs . unlinkSync ( dumpFile )
}
if ( fs . existsSync ( dumpFileDynamic ) ) {
fs . unlinkSync ( dumpFileDynamic )
}
2018-06-19 01:45:58 +00:00
} 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 ( )
} )
} )
2018-11-26 23:38:33 +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
}
2018-11-26 23:38:33 +00:00
const appProcess = ChildProcess . spawn ( remote . process . execPath ,
[ appPath ] , {
2018-06-19 01:45:58 +00:00
env : {
2018-11-26 23:38:33 +00:00
TEST _REQUEST _URL : server . url ,
TEST _DUMP _FILE : dumpFile
2018-06-19 01:45:58 +00:00
}
} )
2018-11-26 23:38:33 +00:00
appProcess . once ( 'close' , ( ) => {
2018-06-19 01:45:58 +00:00
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
}
2018-11-26 23:38:33 +00:00
const appProcess = ChildProcess . spawn ( remote . process . execPath ,
[ appPath ] , {
2018-06-19 01:45:58 +00:00
env : {
TEST _REQUEST _URL : server . url ,
2018-11-26 23:38:33 +00:00
TEST _DUMP _FILE : dumpFile ,
TEST _DUMP _FILE _DYNAMIC : dumpFileDynamic ,
2018-06-19 01:45:58 +00:00
TEST _MANUAL _STOP : true
}
} )
2018-11-26 23:38:33 +00:00
appProcess . once ( 'close' , ( ) => {
2018-06-19 01:45:58 +00:00
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 ,
2018-11-26 23:38:33 +00:00
TEST _DUMP _FILE _DYNAMIC : dumpFileDynamic
2018-06-19 01:45:58 +00:00
}
} )
2018-11-26 23:38:33 +00:00
appProcess . once ( 'close' , ( ) => {
2018-06-19 01:45:58 +00:00
assert ( fs . existsSync ( dumpFileDynamic ) )
done ( )
} )
} )
} )