api-net-log-spec.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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';
  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. 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('currently logging');
  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 as any)).to.throw();
  74. expect(() => testNetLog().startLogging([] as any)).to.throw();
  75. expect(() => testNetLog().startLogging('aoeu', { captureMode: 'aoeu' as any })).to.throw();
  76. expect(() => testNetLog().startLogging('aoeu', { maxFileSize: null as any })).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(serverUrl);
  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: serverUrl });
  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: any) => x.params && x.params.bytes && Buffer.from(x.params.bytes, 'base64').includes(unique))).to.be.true('uuid present in dump');
  110. });
  111. ifit(process.platform !== 'linux')('should begin and end logging automatically when --log-net-log is passed', async () => {
  112. const appProcess = ChildProcess.spawn(process.execPath,
  113. [appPath], {
  114. env: {
  115. TEST_REQUEST_URL: serverUrl,
  116. TEST_DUMP_FILE: dumpFile
  117. }
  118. });
  119. await emittedOnce(appProcess, 'exit');
  120. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  121. });
  122. 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 () => {
  123. const appProcess = ChildProcess.spawn(process.execPath,
  124. [appPath], {
  125. env: {
  126. TEST_REQUEST_URL: serverUrl,
  127. TEST_DUMP_FILE: dumpFile,
  128. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
  129. TEST_MANUAL_STOP: 'true'
  130. }
  131. });
  132. await emittedOnce(appProcess, 'exit');
  133. expect(fs.existsSync(dumpFile)).to.be.true('dump file exists');
  134. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  135. });
  136. ifit(process.platform !== 'linux')('should end logging automatically when only .startLogging() is called', async () => {
  137. const appProcess = ChildProcess.spawn(process.execPath,
  138. [appPath], {
  139. env: {
  140. TEST_REQUEST_URL: serverUrl,
  141. TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
  142. }
  143. });
  144. await emittedOnce(appProcess, 'exit');
  145. expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists');
  146. });
  147. });