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' )
2019-05-23 22:31:38 +00:00
const { session } = require ( 'electron' )
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 )
2019-05-23 22:31:38 +00:00
const isCI = global . isCI
2019-07-24 23:01:08 +00:00
const testNetLog = ( ) => session . fromPartition ( 'net-log' ) . netLog
2018-06-19 01:45:58 +00:00
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 ( )
} )
} )
2019-05-23 22:31:38 +00:00
beforeEach ( ( ) => {
2019-07-24 23:01:08 +00:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . false ( )
2019-05-23 22:31:38 +00:00
} )
2018-06-19 01:45:58 +00:00
afterEach ( ( ) => {
try {
2018-10-04 18:08:56 +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
}
2019-07-24 23:01:08 +00:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . false ( )
2018-06-19 01:45:58 +00:00
} )
2019-02-19 10:48:27 +00:00
it ( 'should begin and end logging to file when .startLogging() and .stopLogging() is called' , async ( ) => {
2019-07-24 23:01:08 +00:00
await testNetLog ( ) . startLogging ( dumpFileDynamic )
2019-02-19 10:48:27 +00:00
2019-07-24 23:01:08 +00:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . true ( )
2019-05-23 22:31:38 +00:00
2019-07-24 23:01:08 +00:00
expect ( testNetLog ( ) . currentlyLoggingPath ) . to . equal ( dumpFileDynamic )
2019-02-19 10:48:27 +00:00
2019-07-24 23:01:08 +00:00
await testNetLog ( ) . stopLogging ( )
2019-02-19 10:48:27 +00:00
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( )
} )
2019-05-23 22:31:38 +00:00
it ( 'should throw an error when .stopLogging() is called without calling .startLogging()' , async ( ) => {
2019-07-24 23:01:08 +00:00
await expect ( testNetLog ( ) . stopLogging ( ) ) . to . be . rejectedWith ( 'No net log in progress' )
2019-05-23 22:31:38 +00:00
} )
2019-02-19 10:48:27 +00:00
2019-05-23 22:31:38 +00:00
it ( 'should throw an error when .startLogging() is called with an invalid argument' , ( ) => {
2019-07-24 23:01:08 +00:00
expect ( ( ) => testNetLog ( ) . startLogging ( '' ) ) . to . throw ( )
expect ( ( ) => testNetLog ( ) . startLogging ( null ) ) . to . throw ( )
expect ( ( ) => testNetLog ( ) . startLogging ( [ ] ) ) . to . throw ( )
2019-02-19 10:48:27 +00:00
} )
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
}
2019-05-23 22:31:38 +00:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-10-04 18:08:56 +00:00
[ appPath ] , {
2018-06-19 01:45:58 +00:00
env : {
2018-10-04 18:08:56 +00:00
TEST _REQUEST _URL : server . url ,
TEST _DUMP _FILE : dumpFile
2018-06-19 01:45:58 +00:00
}
} )
2018-10-08 05:48:25 +00:00
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-10-30 22:45:05 +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
}
2019-05-23 22:31:38 +00:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-10-04 18:08:56 +00:00
[ appPath ] , {
2018-06-19 01:45:58 +00:00
env : {
TEST _REQUEST _URL : server . url ,
2018-10-04 18:08:56 +00:00
TEST _DUMP _FILE : dumpFile ,
TEST _DUMP _FILE _DYNAMIC : dumpFileDynamic ,
2018-06-19 01:45:58 +00:00
TEST _MANUAL _STOP : true
}
} )
2018-10-08 05:48:25 +00:00
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
}
2019-05-23 22:31:38 +00:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-06-19 01:45:58 +00:00
[ appPath ] , {
env : {
TEST _REQUEST _URL : server . url ,
2018-10-04 18:08:56 +00:00
TEST _DUMP _FILE _DYNAMIC : dumpFileDynamic
2018-06-19 01:45:58 +00:00
}
} )
2018-10-04 18:08:56 +00:00
appProcess . once ( 'close' , ( ) => {
2018-06-28 22:40:30 +00:00
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( )
2018-06-19 01:45:58 +00:00
done ( )
} )
} )
} )