api-net-log-spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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', async () => {
  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. const path = await netLog.stopLogging()
  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. })
  69. // TODO(miniak): remove when promisification is complete
  70. it('should begin and end logging to file when .startLogging() and .stopLogging() is called (callback)', done => {
  71. expect(netLog.currentlyLogging).to.be.false()
  72. expect(netLog.currentlyLoggingPath).to.equal('')
  73. netLog.startLogging(dumpFileDynamic)
  74. expect(netLog.currentlyLogging).to.be.true()
  75. expect(netLog.currentlyLoggingPath).to.equal(dumpFileDynamic)
  76. netLog.stopLogging((path) => {
  77. expect(netLog.currentlyLogging).to.be.false()
  78. expect(netLog.currentlyLoggingPath).to.equal('')
  79. expect(path).to.equal(dumpFileDynamic)
  80. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  81. done()
  82. })
  83. })
  84. it('should silence when .stopLogging() is called without calling .startLogging()', async () => {
  85. expect(netLog.currentlyLogging).to.be.false()
  86. expect(netLog.currentlyLoggingPath).to.equal('')
  87. const path = await netLog.stopLogging()
  88. expect(netLog.currentlyLogging).to.be.false()
  89. expect(netLog.currentlyLoggingPath).to.equal('')
  90. expect(path).to.equal('')
  91. })
  92. // TODO(miniak): remove when promisification is complete
  93. it('should silence when .stopLogging() is called without calling .startLogging() (callback)', done => {
  94. expect(netLog.currentlyLogging).to.be.false()
  95. expect(netLog.currentlyLoggingPath).to.equal('')
  96. netLog.stopLogging(path => {
  97. expect(netLog.currentlyLogging).to.be.false()
  98. expect(netLog.currentlyLoggingPath).to.equal('')
  99. expect(path).to.equal('')
  100. done()
  101. })
  102. })
  103. it('should begin and end logging automatically when --log-net-log is passed', done => {
  104. if (isCI && process.platform === 'linux') {
  105. done()
  106. return
  107. }
  108. const appProcess = ChildProcess.spawn(remote.process.execPath,
  109. [appPath], {
  110. env: {
  111. TEST_REQUEST_URL: server.url,
  112. TEST_DUMP_FILE: dumpFile
  113. }
  114. })
  115. appProcess.once('exit', () => {
  116. expect(fs.existsSync(dumpFile)).to.be.true()
  117. done()
  118. })
  119. })
  120. // FIXME(deepak1556): Ch69 follow up.
  121. it('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', done => {
  122. if (isCI && process.platform === 'linux') {
  123. done()
  124. return
  125. }
  126. const appProcess = ChildProcess.spawn(remote.process.execPath,
  127. [appPath], {
  128. env: {
  129. TEST_REQUEST_URL: server.url,
  130. TEST_DUMP_FILE: dumpFile,
  131. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
  132. TEST_MANUAL_STOP: true
  133. }
  134. })
  135. appProcess.once('exit', () => {
  136. expect(fs.existsSync(dumpFile)).to.be.true()
  137. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  138. done()
  139. })
  140. })
  141. it('should end logging automatically when only .startLogging() is called', done => {
  142. if (isCI && process.platform === 'linux') {
  143. done()
  144. return
  145. }
  146. const appProcess = ChildProcess.spawn(remote.process.execPath,
  147. [appPath], {
  148. env: {
  149. TEST_REQUEST_URL: server.url,
  150. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
  151. }
  152. })
  153. appProcess.once('close', () => {
  154. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  155. done()
  156. })
  157. })
  158. })