api-net-log-spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const assert = require('assert')
  2. const http = require('http')
  3. const fs = require('fs')
  4. const os = require('os')
  5. const path = require('path')
  6. const ChildProcess = require('child_process')
  7. const {remote} = require('electron')
  8. const {netLog} = remote
  9. const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log')
  10. const dumpFile = path.join(os.tmpdir(), 'net_log.json')
  11. const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json')
  12. const isCI = remote.getGlobal('isCi')
  13. describe('netLog module', () => {
  14. let server
  15. const connections = new Set()
  16. before((done) => {
  17. server = http.createServer()
  18. server.listen(0, '127.0.0.1', () => {
  19. server.url = `http://127.0.0.1:${server.address().port}`
  20. done()
  21. })
  22. server.on('connection', (connection) => {
  23. connections.add(connection)
  24. connection.once('close', () => {
  25. connections.delete(connection)
  26. })
  27. })
  28. server.on('request', (request, response) => {
  29. response.end()
  30. })
  31. })
  32. after((done) => {
  33. for (const connection of connections) {
  34. connection.destroy()
  35. }
  36. server.close(() => {
  37. server = null
  38. done()
  39. })
  40. })
  41. afterEach(() => {
  42. try {
  43. fs.unlinkSync(dumpFile)
  44. fs.unlinkSync(dumpFileDynamic)
  45. } catch (e) {
  46. // Ignore error
  47. }
  48. })
  49. it('should begin and end logging to file when .startLogging() and .stopLogging() is called', (done) => {
  50. assert(!netLog.currentlyLogging)
  51. assert.equal(netLog.currentlyLoggingPath, '')
  52. netLog.startLogging(dumpFileDynamic)
  53. assert(netLog.currentlyLogging)
  54. assert.equal(netLog.currentlyLoggingPath, dumpFileDynamic)
  55. netLog.stopLogging((path) => {
  56. assert(!netLog.currentlyLogging)
  57. assert.equal(netLog.currentlyLoggingPath, '')
  58. assert.equal(path, dumpFileDynamic)
  59. assert(fs.existsSync(dumpFileDynamic))
  60. done()
  61. })
  62. })
  63. it('should silence when .stopLogging() is called without calling .startLogging()', (done) => {
  64. assert(!netLog.currentlyLogging)
  65. assert.equal(netLog.currentlyLoggingPath, '')
  66. netLog.stopLogging((path) => {
  67. assert(!netLog.currentlyLogging)
  68. assert.equal(netLog.currentlyLoggingPath, '')
  69. assert.equal(path, '')
  70. done()
  71. })
  72. })
  73. // The following tests are skipped on Linux CI
  74. it('should begin and end logging automatically when --log-net-log is passed', (done) => {
  75. if (isCI && process.platform === 'linux') {
  76. done()
  77. return
  78. }
  79. let appProcess = ChildProcess.spawn(remote.process.execPath,
  80. [appPath, `--log-net-log=${dumpFile}`], {
  81. env: {
  82. TEST_REQUEST_URL: server.url
  83. }
  84. })
  85. appProcess.once('exit', () => {
  86. assert(fs.existsSync(dumpFile))
  87. done()
  88. })
  89. })
  90. it('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', (done) => {
  91. if (isCI && process.platform === 'linux') {
  92. done()
  93. return
  94. }
  95. let appProcess = ChildProcess.spawn(remote.process.execPath,
  96. [appPath, `--log-net-log=${dumpFile}`], {
  97. env: {
  98. TEST_REQUEST_URL: server.url,
  99. TEST_DUMP_FILE: dumpFileDynamic,
  100. TEST_MANUAL_STOP: true
  101. }
  102. })
  103. appProcess.stdout.on('data', (data) => {
  104. console.log(data.toString())
  105. })
  106. appProcess.once('exit', () => {
  107. assert(fs.existsSync(dumpFile))
  108. assert(fs.existsSync(dumpFileDynamic))
  109. done()
  110. })
  111. })
  112. it('should end logging automatically when only .startLogging() is called', (done) => {
  113. if (isCI && process.platform === 'linux') {
  114. done()
  115. return
  116. }
  117. let appProcess = ChildProcess.spawn(remote.process.execPath,
  118. [appPath], {
  119. env: {
  120. TEST_REQUEST_URL: server.url,
  121. TEST_DUMP_FILE: dumpFileDynamic
  122. }
  123. })
  124. appProcess.once('exit', () => {
  125. assert(fs.existsSync(dumpFileDynamic))
  126. done()
  127. })
  128. })
  129. })