api-net-log-spec.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 {session, net} = require('electron')
  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 { expect } = chai
  13. chai.use(dirtyChai)
  14. const isCI = global.isCI
  15. const testNetLog = () => session.fromPartition('net-log').netLog
  16. describe('netLog module', () => {
  17. let server
  18. const connections = new Set()
  19. before(done => {
  20. server = http.createServer()
  21. server.listen(0, '127.0.0.1', () => {
  22. server.url = `http://127.0.0.1:${server.address().port}`
  23. done()
  24. })
  25. server.on('connection', (connection) => {
  26. connections.add(connection)
  27. connection.once('close', () => {
  28. connections.delete(connection)
  29. })
  30. })
  31. server.on('request', (request, response) => {
  32. response.end()
  33. })
  34. })
  35. after(done => {
  36. for (const connection of connections) {
  37. connection.destroy()
  38. }
  39. server.close(() => {
  40. server = null
  41. done()
  42. })
  43. })
  44. beforeEach(() => {
  45. expect(testNetLog().currentlyLogging).to.be.false()
  46. })
  47. afterEach(() => {
  48. try {
  49. if (fs.existsSync(dumpFile)) {
  50. fs.unlinkSync(dumpFile)
  51. }
  52. if (fs.existsSync(dumpFileDynamic)) {
  53. fs.unlinkSync(dumpFileDynamic)
  54. }
  55. } catch (e) {
  56. // Ignore error
  57. }
  58. expect(testNetLog().currentlyLogging).to.be.false()
  59. })
  60. it('should begin and end logging to file when .startLogging() and .stopLogging() is called', async () => {
  61. await testNetLog().startLogging(dumpFileDynamic)
  62. expect(testNetLog().currentlyLogging).to.be.true()
  63. expect(testNetLog().currentlyLoggingPath).to.equal(dumpFileDynamic)
  64. const path = await testNetLog().stopLogging()
  65. expect(path).to.equal(dumpFileDynamic)
  66. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  67. })
  68. it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => {
  69. await expect(testNetLog().stopLogging()).to.be.rejectedWith('No net log in progress')
  70. })
  71. it('should throw an error when .startLogging() is called with an invalid argument', () => {
  72. expect(() => testNetLog().startLogging('')).to.throw()
  73. expect(() => testNetLog().startLogging(null)).to.throw()
  74. expect(() => testNetLog().startLogging([])).to.throw()
  75. expect(() => testNetLog().startLogging('aoeu', {captureMode: 'aoeu'})).to.throw()
  76. expect(() => testNetLog().startLogging('aoeu', {maxFileSize: null})).to.throw()
  77. })
  78. it('should include cookies when requested', async () => {
  79. await testNetLog().startLogging(dumpFileDynamic, {captureMode: "includeSensitive"})
  80. const unique = require('uuid').v4()
  81. await new Promise((resolve) => {
  82. const req = net.request(server.url)
  83. req.setHeader('Cookie', `foo=${unique}`)
  84. req.on('response', (response) => {
  85. response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
  86. response.on('end', () => resolve())
  87. })
  88. req.end()
  89. })
  90. await testNetLog().stopLogging()
  91. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists')
  92. const dump = fs.readFileSync(dumpFileDynamic, 'utf8')
  93. expect(dump).to.contain(`foo=${unique}`)
  94. })
  95. it('should include socket bytes when requested', async () => {
  96. await testNetLog().startLogging(dumpFileDynamic, {captureMode: "everything"})
  97. const unique = require('uuid').v4()
  98. await new Promise((resolve) => {
  99. const req = net.request({method: 'POST', url: server.url})
  100. req.on('response', (response) => {
  101. response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
  102. response.on('end', () => resolve())
  103. })
  104. req.end(Buffer.from(unique))
  105. })
  106. await testNetLog().stopLogging()
  107. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists')
  108. const dump = fs.readFileSync(dumpFileDynamic, 'utf8')
  109. expect(JSON.parse(dump).events.some(x => x.params && x.params.bytes && Buffer.from(x.params.bytes, 'base64').includes(unique))).to.be.true('uuid present in dump')
  110. })
  111. it('should begin and end logging automatically when --log-net-log is passed', done => {
  112. if (isCI && process.platform === 'linux') {
  113. done()
  114. return
  115. }
  116. const appProcess = ChildProcess.spawn(process.execPath,
  117. [appPath], {
  118. env: {
  119. TEST_REQUEST_URL: server.url,
  120. TEST_DUMP_FILE: dumpFile
  121. }
  122. })
  123. appProcess.once('exit', () => {
  124. expect(fs.existsSync(dumpFile)).to.be.true()
  125. done()
  126. })
  127. })
  128. it('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', done => {
  129. if (isCI && process.platform === 'linux') {
  130. done()
  131. return
  132. }
  133. const appProcess = ChildProcess.spawn(process.execPath,
  134. [appPath], {
  135. env: {
  136. TEST_REQUEST_URL: server.url,
  137. TEST_DUMP_FILE: dumpFile,
  138. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
  139. TEST_MANUAL_STOP: true
  140. }
  141. })
  142. appProcess.once('exit', () => {
  143. expect(fs.existsSync(dumpFile)).to.be.true()
  144. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  145. done()
  146. })
  147. })
  148. it('should end logging automatically when only .startLogging() is called', done => {
  149. if (isCI && process.platform === 'linux') {
  150. done()
  151. return
  152. }
  153. const appProcess = ChildProcess.spawn(process.execPath,
  154. [appPath], {
  155. env: {
  156. TEST_REQUEST_URL: server.url,
  157. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
  158. }
  159. })
  160. appProcess.once('close', () => {
  161. expect(fs.existsSync(dumpFileDynamic)).to.be.true()
  162. done()
  163. })
  164. })
  165. })