api-net-log-spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { session, net } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as ChildProcess from 'node:child_process';
  4. import { once } from 'node:events';
  5. import * as fs from 'node:fs';
  6. import * as http from 'node:http';
  7. import { Socket } from 'node:net';
  8. import * as os from 'node:os';
  9. import * as path from 'node:path';
  10. import { ifit, listen } from './lib/spec-helpers';
  11. const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log');
  12. const dumpFile = path.join(os.tmpdir(), 'net_log.json');
  13. const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json');
  14. const testNetLog = () => session.fromPartition('net-log').netLog;
  15. describe('netLog module', () => {
  16. let server: http.Server;
  17. let serverUrl: string;
  18. const connections: Set<Socket> = new Set();
  19. before(async () => {
  20. server = http.createServer();
  21. server.on('connection', (connection) => {
  22. connections.add(connection);
  23. connection.once('close', () => {
  24. connections.delete(connection);
  25. });
  26. });
  27. server.on('request', (request, response) => {
  28. response.end();
  29. });
  30. serverUrl = (await listen(server)).url;
  31. });
  32. after(done => {
  33. for (const connection of connections) {
  34. connection.destroy();
  35. }
  36. server.close(() => {
  37. server = null as any;
  38. done();
  39. });
  40. });
  41. beforeEach(() => {
  42. expect(testNetLog().currentlyLogging).to.be.false('currently logging');
  43. });
  44. afterEach(() => {
  45. try {
  46. if (fs.existsSync(dumpFile)) {
  47. fs.unlinkSync(dumpFile);
  48. }
  49. if (fs.existsSync(dumpFileDynamic)) {
  50. fs.unlinkSync(dumpFileDynamic);
  51. }
  52. } catch {
  53. // Ignore error
  54. }
  55. expect(testNetLog().currentlyLogging).to.be.false('currently logging');
  56. });
  57. it('should begin and end logging to file when .startLogging() and .stopLogging() is called', async () => {
  58. await testNetLog().startLogging(dumpFileDynamic);
  59. expect(testNetLog().currentlyLogging).to.be.true('currently logging');
  60. await testNetLog().stopLogging();
  61. expect(fs.existsSync(dumpFileDynamic)).to.be.true('currently logging');
  62. });
  63. it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => {
  64. await expect(testNetLog().stopLogging()).to.be.rejectedWith('No net log in progress');
  65. });
  66. it('should throw an error when .startLogging() is called with an invalid argument', () => {
  67. expect(() => testNetLog().startLogging('')).to.throw();
  68. expect(() => testNetLog().startLogging(null as any)).to.throw();
  69. expect(() => testNetLog().startLogging([] as any)).to.throw();
  70. expect(() => testNetLog().startLogging('aoeu', { captureMode: 'aoeu' as any })).to.throw();
  71. expect(() => testNetLog().startLogging('aoeu', { maxFileSize: null as any })).to.throw();
  72. });
  73. it('should include cookies when requested', async () => {
  74. await testNetLog().startLogging(dumpFileDynamic, { captureMode: 'includeSensitive' });
  75. const unique = require('uuid').v4();
  76. await new Promise<void>((resolve) => {
  77. const req = net.request(serverUrl);
  78. req.setHeader('Cookie', `foo=${unique}`);
  79. req.on('response', (response) => {
  80. response.on('data', () => {}); // https://github.com/electron/electron/issues/19214
  81. response.on('end', () => resolve());
  82. });
  83. req.end();
  84. });
  85. await testNetLog().stopLogging();
  86. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists');
  87. const dump = fs.readFileSync(dumpFileDynamic, 'utf8');
  88. expect(dump).to.contain(`foo=${unique}`);
  89. });
  90. it('should include socket bytes when requested', async () => {
  91. await testNetLog().startLogging(dumpFileDynamic, { captureMode: 'everything' });
  92. const unique = require('uuid').v4();
  93. await new Promise<void>((resolve) => {
  94. const req = net.request({ method: 'POST', url: serverUrl });
  95. req.on('response', (response) => {
  96. response.on('data', () => {}); // https://github.com/electron/electron/issues/19214
  97. response.on('end', () => resolve());
  98. });
  99. req.end(Buffer.from(unique));
  100. });
  101. await testNetLog().stopLogging();
  102. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists');
  103. const dump = fs.readFileSync(dumpFileDynamic, 'utf8');
  104. expect(JSON.parse(dump).events.some((x: any) => x.params && x.params.bytes && Buffer.from(x.params.bytes, 'base64').includes(unique))).to.be.true('uuid present in dump');
  105. });
  106. ifit(process.platform !== 'linux')('should begin and end logging automatically when --log-net-log is passed', async () => {
  107. const appProcess = ChildProcess.spawn(process.execPath,
  108. [appPath], {
  109. env: {
  110. TEST_REQUEST_URL: serverUrl,
  111. TEST_DUMP_FILE: dumpFile
  112. }
  113. });
  114. await once(appProcess, 'exit');
  115. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  116. });
  117. ifit(process.platform !== 'linux')('should begin and end logging automatically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', async () => {
  118. const appProcess = ChildProcess.spawn(process.execPath,
  119. [appPath], {
  120. env: {
  121. TEST_REQUEST_URL: serverUrl,
  122. TEST_DUMP_FILE: dumpFile,
  123. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
  124. TEST_MANUAL_STOP: 'true'
  125. }
  126. });
  127. await once(appProcess, 'exit');
  128. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  129. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  130. });
  131. ifit(process.platform !== 'linux')('should end logging automatically when only .startLogging() is called', async () => {
  132. const appProcess = ChildProcess.spawn(process.execPath,
  133. [appPath], {
  134. env: {
  135. TEST_REQUEST_URL: serverUrl,
  136. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
  137. }
  138. });
  139. await once(appProcess, 'exit');
  140. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  141. });
  142. });