forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-net-log-spec.ts
186 lines (162 loc) · 6.02 KB
/
api-net-log-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { expect } from 'chai'
import * as http from 'http'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as ChildProcess from 'child_process'
import { session, net } from 'electron'
import { Socket, AddressInfo } from 'net';
const appPath = path.join(__dirname, 'fixtures', 'api', 'net-log')
const dumpFile = path.join(os.tmpdir(), 'net_log.json')
const dumpFileDynamic = path.join(os.tmpdir(), 'net_log_dynamic.json')
const testNetLog = () => session.fromPartition('net-log').netLog
describe('netLog module', () => {
let server: http.Server
let serverUrl: string
const connections: Set<Socket> = new Set()
before(done => {
server = http.createServer()
server.listen(0, '127.0.0.1', () => {
serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
done()
})
server.on('connection', (connection) => {
connections.add(connection)
connection.once('close', () => {
connections.delete(connection)
})
})
server.on('request', (request, response) => {
response.end()
})
})
after(done => {
for (const connection of connections) {
connection.destroy()
}
server.close(() => {
server = null as any
done()
})
})
beforeEach(() => {
expect(testNetLog().currentlyLogging).to.be.false('currently logging')
})
afterEach(() => {
try {
if (fs.existsSync(dumpFile)) {
fs.unlinkSync(dumpFile)
}
if (fs.existsSync(dumpFileDynamic)) {
fs.unlinkSync(dumpFileDynamic)
}
} catch (e) {
// Ignore error
}
expect(testNetLog().currentlyLogging).to.be.false('currently logging')
})
it('should begin and end logging to file when .startLogging() and .stopLogging() is called', async () => {
await testNetLog().startLogging(dumpFileDynamic)
expect(testNetLog().currentlyLogging).to.be.true('currently logging')
expect(testNetLog().currentlyLoggingPath).to.equal(dumpFileDynamic)
await testNetLog().stopLogging()
expect(fs.existsSync(dumpFileDynamic)).to.be.true('currently logging')
})
it('should throw an error when .stopLogging() is called without calling .startLogging()', async () => {
await expect(testNetLog().stopLogging()).to.be.rejectedWith('No net log in progress')
})
it('should throw an error when .startLogging() is called with an invalid argument', () => {
expect(() => testNetLog().startLogging('')).to.throw()
expect(() => testNetLog().startLogging(null as any)).to.throw()
expect(() => testNetLog().startLogging([] as any)).to.throw()
expect(() => testNetLog().startLogging('aoeu', {captureMode: 'aoeu' as any})).to.throw()
expect(() => testNetLog().startLogging('aoeu', {maxFileSize: null as any})).to.throw()
})
it('should include cookies when requested', async () => {
await testNetLog().startLogging(dumpFileDynamic, {captureMode: "includeSensitive"})
const unique = require('uuid').v4()
await new Promise((resolve) => {
const req = net.request(serverUrl)
req.setHeader('Cookie', `foo=${unique}`)
req.on('response', (response) => {
response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
response.on('end', () => resolve())
})
req.end()
})
await testNetLog().stopLogging()
expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists')
const dump = fs.readFileSync(dumpFileDynamic, 'utf8')
expect(dump).to.contain(`foo=${unique}`)
})
it('should include socket bytes when requested', async () => {
await testNetLog().startLogging(dumpFileDynamic, {captureMode: "everything"})
const unique = require('uuid').v4()
await new Promise((resolve) => {
const req = net.request({method: 'POST', url: serverUrl})
req.on('response', (response) => {
response.on('data', () => {}) // https://github.com/electron/electron/issues/19214
response.on('end', () => resolve())
})
req.end(Buffer.from(unique))
})
await testNetLog().stopLogging()
expect(fs.existsSync(dumpFileDynamic)).to.be.true('dump file exists')
const dump = fs.readFileSync(dumpFileDynamic, 'utf8')
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')
})
it('should begin and end logging automatically when --log-net-log is passed', done => {
if (isCI && process.platform === 'linux') {
done()
return
}
const appProcess = ChildProcess.spawn(process.execPath,
[appPath], {
env: {
TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE: dumpFile
}
})
appProcess.once('exit', () => {
expect(fs.existsSync(dumpFile)).to.be.true('dump file exists')
done()
})
})
it('should begin and end logging automtically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called', done => {
if (isCI && process.platform === 'linux') {
done()
return
}
const appProcess = ChildProcess.spawn(process.execPath,
[appPath], {
env: {
TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE: dumpFile,
TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic,
TEST_MANUAL_STOP: 'true'
}
})
appProcess.once('exit', () => {
expect(fs.existsSync(dumpFile)).to.be.true('dump file exists')
expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists')
done()
})
})
it('should end logging automatically when only .startLogging() is called', done => {
if (isCI && process.platform === 'linux') {
done()
return
}
const appProcess = ChildProcess.spawn(process.execPath,
[appPath], {
env: {
TEST_REQUEST_URL: serverUrl,
TEST_DUMP_FILE_DYNAMIC: dumpFileDynamic
}
})
appProcess.once('close', () => {
expect(fs.existsSync(dumpFileDynamic)).to.be.true('dynamic dump file exists')
done()
})
})
})