api-net-log-spec.js 4.2 KB

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