api-net-log-spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { expect } from 'chai';
  2. import * as http from 'http';
  3. import * as fs from 'fs';
  4. import * as os from 'os';
  5. import * as path from 'path';
  6. import * as ChildProcess from 'child_process';
  7. import { session, net } from 'electron/main';
  8. import { Socket, AddressInfo } from 'net';
  9. import { ifit } from './spec-helpers';
  10. import { emittedOnce } from './events-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(done => {
  20. server = http.createServer();
  21. server.listen(0, '127.0.0.1', () => {
  22. serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).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 as any;
  41. done();
  42. });
  43. });
  44. beforeEach(() => {
  45. expect(testNetLog().currentlyLogging).to.be.false('currently logging');
  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('currently logging');
  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('currently logging');
  63. await testNetLog().stopLogging();
  64. expect(fs.existsSync(dumpFileDynamic)).to.be.true('currently logging');
  65. });
  66. it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => {
  67. await expect(testNetLog().stopLogging()).to.be.rejectedWith('No net log in progress');
  68. });
  69. it('should throw an error when .startLogging() is called with an invalid argument', () => {
  70. expect(() => testNetLog().startLogging('')).to.throw();
  71. expect(() => testNetLog().startLogging(null as any)).to.throw();
  72. expect(() => testNetLog().startLogging([] as any)).to.throw();
  73. expect(() => testNetLog().startLogging('aoeu', { captureMode: 'aoeu' as any })).to.throw();
  74. expect(() => testNetLog().startLogging('aoeu', { maxFileSize: null as any })).to.throw();
  75. });
  76. it('should include cookies when requested', async () => {
  77. await testNetLog().startLogging(dumpFileDynamic, { captureMode: 'includeSensitive' });
  78. const unique = require('uuid').v4();
  79. await new Promise<void>((resolve) => {
  80. const req = net.request(serverUrl);
  81. req.setHeader('Cookie', `foo=${unique}`);
  82. req.on('response', (response) => {
  83. response.on('data', () => {}); // https://github.com/electron/electron/issues/19214
  84. response.on('end', () => resolve());
  85. });
  86. req.end();
  87. });
  88. await testNetLog().stopLogging();
  89. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists');
  90. const dump = fs.readFileSync(dumpFileDynamic, 'utf8');
  91. expect(dump).to.contain(`foo=${unique}`);
  92. });
  93. it('should include socket bytes when requested', async () => {
  94. await testNetLog().startLogging(dumpFileDynamic, { captureMode: 'everything' });
  95. const unique = require('uuid').v4();
  96. await new Promise<void>((resolve) => {
  97. const req = net.request({ method: 'POST', url: serverUrl });
  98. req.on('response', (response) => {
  99. response.on('data', () => {}); // https://github.com/electron/electron/issues/19214
  100. response.on('end', () => resolve());
  101. });
  102. req.end(Buffer.from(unique));
  103. });
  104. await testNetLog().stopLogging();
  105. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists');
  106. const dump = fs.readFileSync(dumpFileDynamic, 'utf8');
  107. 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');
  108. });
  109. ifit(process.platform !== 'linux')('should begin and end logging automatically when --log-net-log is passed', async () => {
  110. const appProcess = ChildProcess.spawn(process.execPath,
  111. [appPath], {
  112. env: {
  113. TEST_REQUEST_URL: serverUrl,
  114. TEST_DUMP_FILE: dumpFile
  115. }
  116. });
  117. await emittedOnce(appProcess, 'exit');
  118. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  119. });
  120. ifit(process.platform !== 'linux')('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', async () => {
  121. const appProcess = ChildProcess.spawn(process.execPath,
  122. [appPath], {
  123. env: {
  124. TEST_REQUEST_URL: serverUrl,
  125. TEST_DUMP_FILE: dumpFile,
  126. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
  127. TEST_MANUAL_STOP: 'true'
  128. }
  129. });
  130. await emittedOnce(appProcess, 'exit');
  131. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  132. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  133. });
  134. ifit(process.platform !== 'linux')('should end logging automatically when only .startLogging() is called', async () => {
  135. const appProcess = ChildProcess.spawn(process.execPath,
  136. [appPath], {
  137. env: {
  138. TEST_REQUEST_URL: serverUrl,
  139. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
  140. }
  141. });
  142. await emittedOnce(appProcess, 'exit');
  143. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  144. });
  145. });