2019-06-14 23:26:07 +00:00
|
|
|
import { expect } from 'chai'
|
2019-11-25 20:56:18 +00:00
|
|
|
import { net, session, ClientRequest, BrowserWindow } from 'electron'
|
2019-06-14 23:26:07 +00:00
|
|
|
import * as http from 'http'
|
|
|
|
import * as url from 'url'
|
2019-11-27 01:01:13 +00:00
|
|
|
import { AddressInfo, Socket } from 'net'
|
2019-11-25 20:56:18 +00:00
|
|
|
import { emittedOnce } from './events-helpers'
|
2019-10-10 12:14:41 +00:00
|
|
|
|
2019-06-14 23:26:07 +00:00
|
|
|
const kOneKiloByte = 1024
|
|
|
|
const kOneMegaByte = kOneKiloByte * kOneKiloByte
|
|
|
|
|
|
|
|
function randomBuffer (size: number, start: number = 0, end: number = 255) {
|
|
|
|
const range = 1 + end - start
|
|
|
|
const buffer = Buffer.allocUnsafe(size)
|
|
|
|
for (let i = 0; i < size; ++i) {
|
|
|
|
buffer[i] = start + Math.floor(Math.random() * range)
|
|
|
|
}
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomString (length: number) {
|
|
|
|
const buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
|
|
|
|
return buffer.toString()
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
const cleanupTasks: (() => void)[] = []
|
|
|
|
|
|
|
|
function cleanUp () {
|
|
|
|
cleanupTasks.forEach(t => t())
|
|
|
|
cleanupTasks.length = 0
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
function respondOnce (fn: http.RequestListener): Promise<string> {
|
2019-06-14 23:26:07 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const server = http.createServer((request, response) => {
|
|
|
|
fn(request, response)
|
|
|
|
// don't close if a redirect was returned
|
2019-11-01 20:37:02 +00:00
|
|
|
if (response.statusCode < 300 || response.statusCode >= 399) { server.close() }
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`)
|
|
|
|
})
|
2019-11-27 01:01:13 +00:00
|
|
|
const sockets: Socket[] = []
|
|
|
|
server.on('connection', s => sockets.push(s))
|
|
|
|
cleanupTasks.push(() => {
|
|
|
|
server.close()
|
|
|
|
sockets.forEach(s => s.destroy())
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOnce.toRoutes = (routes: Record<string, http.RequestListener>) => {
|
|
|
|
return respondOnce((request, response) => {
|
|
|
|
if (routes.hasOwnProperty(request.url || '')) {
|
|
|
|
routes[request.url || ''](request, response)
|
|
|
|
} else {
|
|
|
|
response.statusCode = 500
|
|
|
|
response.end()
|
|
|
|
expect.fail(`Unexpected URL: ${request.url}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOnce.toURL = (url: string, fn: http.RequestListener) => {
|
2019-11-01 20:37:02 +00:00
|
|
|
return respondOnce.toRoutes({ [url]: fn })
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondOnce.toSingleURL = (fn: http.RequestListener) => {
|
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
return respondOnce.toURL(requestUrl, fn).then(url => `${url}${requestUrl}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('net module', () => {
|
2019-11-27 01:01:13 +00:00
|
|
|
afterEach(cleanUp)
|
|
|
|
afterEach(async () => {
|
|
|
|
await session.defaultSession.clearCache()
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
describe('HTTP basics', () => {
|
|
|
|
it('should be able to issue a basic GET request', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.method).to.equal('GET')
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to issue a basic POST request', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.method).to.equal('POST')
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', () => { })
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fetch correct data in a GET request', (done) => {
|
|
|
|
const bodyData = 'Hello World!'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.method).to.equal('GET')
|
|
|
|
response.end(bodyData)
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
let expectedBodyData = ''
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', (chunk) => {
|
|
|
|
expectedBodyData += chunk.toString()
|
|
|
|
})
|
|
|
|
response.on('end', () => {
|
|
|
|
expect(expectedBodyData).to.equal(bodyData)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should post the correct data in a POST request', (done) => {
|
|
|
|
const bodyData = 'Hello World!'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let postedBodyData = ''
|
|
|
|
expect(request.method).to.equal('POST')
|
|
|
|
request.on('data', (chunk: Buffer) => {
|
|
|
|
postedBodyData += chunk.toString()
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
expect(postedBodyData).to.equal(bodyData)
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.write(bodyData)
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support chunked encoding', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.chunkedEncoding = true
|
|
|
|
expect(request.method).to.equal('POST')
|
|
|
|
expect(request.headers['transfer-encoding']).to.equal('chunked')
|
|
|
|
expect(request.headers['content-length']).to.equal(undefined)
|
|
|
|
request.on('data', (chunk: Buffer) => {
|
|
|
|
response.write(chunk)
|
|
|
|
})
|
|
|
|
request.on('end', (chunk: Buffer) => {
|
|
|
|
response.end(chunk)
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
|
|
|
})
|
|
|
|
|
|
|
|
let chunkIndex = 0
|
|
|
|
const chunkCount = 100
|
2019-11-25 20:56:18 +00:00
|
|
|
let sent = Buffer.alloc(0)
|
|
|
|
let received = Buffer.alloc(0)
|
2019-06-14 23:26:07 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', (chunk) => {
|
2019-11-25 20:56:18 +00:00
|
|
|
received = Buffer.concat([received, chunk])
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
response.on('end', () => {
|
2019-11-25 20:56:18 +00:00
|
|
|
expect(sent.equals(received)).to.be.true()
|
2019-06-14 23:26:07 +00:00
|
|
|
expect(chunkIndex).to.be.equal(chunkCount)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
while (chunkIndex < chunkCount) {
|
|
|
|
chunkIndex += 1
|
|
|
|
const chunk = randomBuffer(kOneKiloByte)
|
2019-11-25 20:56:18 +00:00
|
|
|
sent = Buffer.concat([sent, chunk])
|
|
|
|
urlRequest.write(chunk)
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
2019-11-14 18:01:18 +00:00
|
|
|
|
|
|
|
it('should emit the login event when 401', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass']
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end()
|
|
|
|
}
|
|
|
|
response.writeHead(200).end('ok')
|
|
|
|
})
|
|
|
|
let loginAuthInfo: any
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl })
|
|
|
|
request.on('response', (response) => {
|
|
|
|
response.on('error', reject)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => resolve())
|
|
|
|
})
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
loginAuthInfo = authInfo
|
|
|
|
cb(user, pass)
|
|
|
|
})
|
|
|
|
request.on('error', reject)
|
|
|
|
request.end()
|
|
|
|
})
|
|
|
|
expect(loginAuthInfo.realm).to.equal('Foo')
|
|
|
|
expect(loginAuthInfo.scheme).to.equal('basic')
|
|
|
|
})
|
|
|
|
|
2019-11-25 22:34:25 +00:00
|
|
|
it('should response when cancelling authentication', async () => {
|
2019-11-14 18:01:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
2019-11-25 22:34:25 +00:00
|
|
|
response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' })
|
|
|
|
response.end('unauthenticated')
|
|
|
|
} else {
|
|
|
|
response.writeHead(200).end('ok')
|
2019-11-14 18:01:18 +00:00
|
|
|
}
|
|
|
|
})
|
2019-11-25 22:34:25 +00:00
|
|
|
expect(await new Promise((resolve, reject) => {
|
2019-11-14 18:01:18 +00:00
|
|
|
const request = net.request({ method: 'GET', url: serverUrl })
|
|
|
|
request.on('response', (response) => {
|
2019-11-25 22:34:25 +00:00
|
|
|
let data = ''
|
2019-11-14 18:01:18 +00:00
|
|
|
response.on('error', reject)
|
2019-11-25 22:34:25 +00:00
|
|
|
response.on('data', (chunk) => {
|
|
|
|
data += chunk
|
|
|
|
})
|
|
|
|
response.on('end', () => resolve(data))
|
2019-11-14 18:01:18 +00:00
|
|
|
})
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
request.on('error', reject)
|
|
|
|
request.end()
|
2019-11-25 22:34:25 +00:00
|
|
|
})).to.equal('unauthenticated')
|
2019-11-14 18:01:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should share credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass']
|
|
|
|
const serverUrl = await new Promise<string>((resolve) => {
|
|
|
|
const server = http.createServer((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end()
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok')
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`)
|
|
|
|
})
|
|
|
|
after(() => { server.close() })
|
|
|
|
})
|
|
|
|
const bw = new BrowserWindow({ show: false })
|
|
|
|
const loaded = bw.loadURL(serverUrl)
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault()
|
|
|
|
cb(user, pass)
|
|
|
|
})
|
|
|
|
await loaded
|
|
|
|
bw.close()
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl })
|
|
|
|
request.on('response', (response) => {
|
|
|
|
response.on('error', reject)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => resolve())
|
|
|
|
})
|
|
|
|
request.on('login', () => {
|
|
|
|
// we shouldn't receive a login event, because the credentials should
|
|
|
|
// be cached.
|
|
|
|
reject(new Error('unexpected login event'))
|
|
|
|
})
|
|
|
|
request.on('error', reject)
|
|
|
|
request.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should share proxy credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass']
|
|
|
|
const proxyPort = await new Promise<number>((resolve) => {
|
|
|
|
const server = http.createServer((request, response) => {
|
|
|
|
if (!request.headers['proxy-authorization']) {
|
|
|
|
return response.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Foo"' }).end()
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok')
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
resolve((server.address() as AddressInfo).port)
|
|
|
|
})
|
|
|
|
after(() => { server.close() })
|
|
|
|
})
|
|
|
|
const customSession = session.fromPartition(`net-proxy-test-${Math.random()}`)
|
|
|
|
await customSession.setProxy({ proxyRules: `127.0.0.1:${proxyPort}`, proxyBypassRules: '<-loopback>' })
|
|
|
|
const bw = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
|
|
|
|
const loaded = bw.loadURL('http://127.0.0.1:9999')
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault()
|
|
|
|
cb(user, pass)
|
|
|
|
})
|
|
|
|
await loaded
|
|
|
|
bw.close()
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const request = net.request({ method: 'GET', url: 'http://127.0.0.1:9999', session: customSession })
|
|
|
|
request.on('response', (response) => {
|
|
|
|
response.on('error', reject)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => resolve())
|
|
|
|
})
|
|
|
|
request.on('login', () => {
|
|
|
|
// we shouldn't receive a login event, because the credentials should
|
|
|
|
// be cached.
|
|
|
|
reject(new Error('unexpected login event'))
|
|
|
|
})
|
|
|
|
request.on('error', reject)
|
|
|
|
request.end()
|
|
|
|
})
|
|
|
|
})
|
2019-11-27 01:01:13 +00:00
|
|
|
|
|
|
|
it('should upload body when 401', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass']
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end()
|
|
|
|
}
|
|
|
|
response.writeHead(200)
|
|
|
|
request.on('data', (chunk) => { response.write(chunk) })
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const requestData = randomString(kOneKiloByte)
|
|
|
|
const responseData = await new Promise((resolve, reject) => {
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl })
|
|
|
|
request.on('response', (response) => {
|
|
|
|
response.on('error', reject)
|
|
|
|
let data = ''
|
|
|
|
response.on('data', (chunk) => { data += chunk.toString() })
|
|
|
|
response.on('end', () => resolve(data))
|
|
|
|
})
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
cb(user, pass)
|
|
|
|
})
|
|
|
|
request.on('error', reject)
|
|
|
|
request.end(requestData)
|
|
|
|
})
|
|
|
|
expect(responseData).to.equal(requestData)
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('ClientRequest API', () => {
|
|
|
|
it('request/response objects should emit expected events', (done) => {
|
|
|
|
const bodyData = randomString(kOneKiloByte)
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.end(bodyData)
|
|
|
|
}).then(serverUrl => {
|
|
|
|
let requestResponseEventEmitted = false
|
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
|
|
|
let responseDataEventEmitted = false
|
|
|
|
let responseEndEventEmitted = false
|
|
|
|
|
|
|
|
function maybeDone (done: () => void) {
|
|
|
|
if (!requestCloseEventEmitted || !responseEndEventEmitted) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(requestResponseEventEmitted).to.equal(true)
|
|
|
|
expect(requestFinishEventEmitted).to.equal(true)
|
|
|
|
expect(requestCloseEventEmitted).to.equal(true)
|
|
|
|
expect(responseDataEventEmitted).to.equal(true)
|
|
|
|
expect(responseEndEventEmitted).to.equal(true)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
requestResponseEventEmitted = true
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
const buffers: Buffer[] = []
|
|
|
|
response.on('data', (chunk) => {
|
|
|
|
buffers.push(chunk)
|
|
|
|
responseDataEventEmitted = true
|
|
|
|
})
|
|
|
|
response.on('end', () => {
|
|
|
|
const receivedBodyData = Buffer.concat(buffers)
|
|
|
|
expect(receivedBodyData.toString()).to.equal(bodyData)
|
|
|
|
responseEndEventEmitted = true
|
|
|
|
maybeDone(done)
|
|
|
|
})
|
|
|
|
response.on('error', (error: Error) => {
|
|
|
|
expect(error).to.be.an('Error')
|
|
|
|
})
|
|
|
|
response.on('aborted', () => {
|
|
|
|
expect.fail('response aborted')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.on('finish', () => {
|
|
|
|
requestFinishEventEmitted = true
|
|
|
|
})
|
|
|
|
urlRequest.on('error', (error) => {
|
|
|
|
expect(error).to.be.an('Error')
|
|
|
|
})
|
|
|
|
urlRequest.on('abort', () => {
|
|
|
|
expect.fail('request aborted')
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
requestCloseEventEmitted = true
|
|
|
|
maybeDone(done)
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to set a custom HTTP request header before first write', (done) => {
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {
|
|
|
|
})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
|
|
|
urlRequest.write('')
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should be able to set a non-string object as a header value', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
const customHeaderName = 'Some-Integer-Value'
|
|
|
|
const customHeaderValue = 900
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue.toString())
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
const complete = new Promise<number>(resolve => {
|
2019-06-14 23:26:07 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-11-25 20:56:18 +00:00
|
|
|
resolve(response.statusCode)
|
2019-06-14 23:26:07 +00:00
|
|
|
response.on('data', () => {})
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('end', () => {})
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue as any)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
|
|
|
urlRequest.write('')
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
|
|
|
urlRequest.end()
|
|
|
|
expect(await complete).to.equal(200)
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not be able to set a custom HTTP request header after first write', (done) => {
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(undefined)
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.write('')
|
|
|
|
expect(() => {
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
}).to.throw()
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(undefined)
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to remove a custom HTTP request header before first write', (done) => {
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(undefined)
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
urlRequest.removeHeader(customHeaderName)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(undefined)
|
|
|
|
urlRequest.write('')
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not be able to remove a custom HTTP request header after first write', (done) => {
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
urlRequest.write('')
|
|
|
|
expect(() => {
|
|
|
|
urlRequest.removeHeader(customHeaderName)
|
|
|
|
}).to.throw()
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to set cookie header line', (done) => {
|
|
|
|
const cookieHeaderName = 'Cookie'
|
|
|
|
const cookieHeaderValue = 'test=12345'
|
|
|
|
const customSession = session.fromPartition('test-cookie-header')
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[cookieHeaderName.toLowerCase()]).to.equal(cookieHeaderValue)
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
customSession.cookies.set({
|
|
|
|
url: `${serverUrl}`,
|
|
|
|
name: 'test',
|
2019-12-11 07:44:49 +00:00
|
|
|
value: '11111',
|
|
|
|
expirationDate: 0
|
2019-06-14 23:26:07 +00:00
|
|
|
}).then(() => { // resolved
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: serverUrl,
|
|
|
|
session: customSession
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(cookieHeaderName, cookieHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(cookieHeaderName)).to.equal(cookieHeaderValue)
|
|
|
|
urlRequest.end()
|
|
|
|
}, (error) => {
|
|
|
|
done(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-01-14 17:36:08 +00:00
|
|
|
it('should be able to receive cookies', (done) => {
|
|
|
|
const cookie = ['cookie1', 'cookie2']
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader('set-cookie', cookie)
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.headers['set-cookie']).to.have.same.members(cookie)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should be able to abort an HTTP request before first write', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
response.end()
|
|
|
|
expect.fail('Unexpected request event')
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', () => {
|
|
|
|
expect.fail('unexpected response event')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
const aborted = emittedOnce(urlRequest, 'abort')
|
|
|
|
urlRequest.abort()
|
|
|
|
urlRequest.write('')
|
|
|
|
urlRequest.end()
|
|
|
|
await aborted
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('it should be able to abort an HTTP request before request end', (done) => {
|
|
|
|
let requestReceivedByServer = false
|
|
|
|
let urlRequest: ClientRequest | null = null
|
2019-11-01 20:37:02 +00:00
|
|
|
respondOnce.toSingleURL(() => {
|
2019-06-14 23:26:07 +00:00
|
|
|
requestReceivedByServer = true
|
|
|
|
urlRequest!.abort()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
let requestAbortEventEmitted = false
|
|
|
|
|
|
|
|
urlRequest = net.request(serverUrl)
|
2019-11-01 20:37:02 +00:00
|
|
|
urlRequest.on('response', () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
expect.fail('Unexpected response event')
|
|
|
|
})
|
|
|
|
urlRequest.on('finish', () => {
|
|
|
|
expect.fail('Unexpected finish event')
|
|
|
|
})
|
|
|
|
urlRequest.on('error', () => {
|
|
|
|
expect.fail('Unexpected error event')
|
|
|
|
})
|
|
|
|
urlRequest.on('abort', () => {
|
|
|
|
requestAbortEventEmitted = true
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
expect(requestReceivedByServer).to.equal(true)
|
|
|
|
expect(requestAbortEventEmitted).to.equal(true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
urlRequest.write(randomString(kOneKiloByte))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('it should be able to abort an HTTP request after request end and before response', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
let requestReceivedByServer = false
|
|
|
|
let urlRequest: ClientRequest | null = null
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
requestReceivedByServer = true
|
|
|
|
urlRequest!.abort()
|
|
|
|
process.nextTick(() => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
let requestFinishEventEmitted = false
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', () => {
|
|
|
|
expect.fail('Unexpected response event')
|
|
|
|
})
|
|
|
|
urlRequest.on('finish', () => {
|
|
|
|
requestFinishEventEmitted = true
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('error', () => {
|
|
|
|
expect.fail('Unexpected error event')
|
|
|
|
})
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
|
|
|
await emittedOnce(urlRequest, 'abort')
|
|
|
|
expect(requestFinishEventEmitted).to.equal(true)
|
|
|
|
expect(requestReceivedByServer).to.equal(true)
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('it should be able to abort an HTTP request after response start', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
let requestReceivedByServer = false
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
requestReceivedByServer = true
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.write(randomString(kOneKiloByte))
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestResponseEventEmitted = false
|
|
|
|
let responseCloseEventEmitted = false
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
requestResponseEventEmitted = true
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
expect.fail('Unexpected end event')
|
|
|
|
})
|
|
|
|
response.on('error', () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
expect.fail('Unexpected error event')
|
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('close' as any, () => {
|
|
|
|
responseCloseEventEmitted = true
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.abort()
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('finish', () => {
|
|
|
|
requestFinishEventEmitted = true
|
|
|
|
})
|
|
|
|
urlRequest.on('error', () => {
|
|
|
|
expect.fail('Unexpected error event')
|
|
|
|
})
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
|
|
|
await emittedOnce(urlRequest, 'abort')
|
|
|
|
expect(requestFinishEventEmitted).to.be.true('request should emit "finish" event')
|
|
|
|
expect(requestReceivedByServer).to.be.true('request should be received by the server')
|
|
|
|
expect(requestResponseEventEmitted).to.be.true('"response" event should be emitted')
|
|
|
|
expect(responseCloseEventEmitted).to.be.true('response should emit "close" event')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('abort event should be emitted at most once', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
let requestReceivedByServer = false
|
|
|
|
let urlRequest: ClientRequest | null = null
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL(() => {
|
2019-06-14 23:26:07 +00:00
|
|
|
requestReceivedByServer = true
|
|
|
|
urlRequest!.abort()
|
|
|
|
urlRequest!.abort()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let abortsEmitted = 0
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', () => {
|
|
|
|
expect.fail('Unexpected response event')
|
|
|
|
})
|
|
|
|
urlRequest.on('finish', () => {
|
|
|
|
requestFinishEventEmitted = true
|
|
|
|
})
|
|
|
|
urlRequest.on('error', () => {
|
|
|
|
expect.fail('Unexpected error event')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('abort', () => {
|
|
|
|
abortsEmitted++
|
|
|
|
})
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
|
|
|
await emittedOnce(urlRequest, 'abort')
|
|
|
|
expect(requestFinishEventEmitted).to.be.true('request should emit "finish" event')
|
|
|
|
expect(requestReceivedByServer).to.be.true('request should be received by server')
|
|
|
|
expect(abortsEmitted).to.equal(1, 'request should emit exactly 1 "abort" event')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-25 22:34:25 +00:00
|
|
|
it('should allow to read response body from non-2xx response', async () => {
|
|
|
|
const bodyData = randomString(kOneKiloByte)
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 404
|
|
|
|
response.end(bodyData)
|
|
|
|
})
|
|
|
|
|
|
|
|
let requestResponseEventEmitted = false
|
|
|
|
let responseDataEventEmitted = false
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
const eventHandlers = Promise.all([
|
|
|
|
emittedOnce(urlRequest, 'response')
|
|
|
|
.then(async (params: any[]) => {
|
|
|
|
const response: Electron.IncomingMessage = params[0]
|
|
|
|
requestResponseEventEmitted = true
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
expect(statusCode).to.equal(404)
|
|
|
|
const buffers: Buffer[] = []
|
|
|
|
response.on('data', (chunk) => {
|
|
|
|
buffers.push(chunk)
|
|
|
|
responseDataEventEmitted = true
|
|
|
|
})
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
response.on('error', () => {
|
|
|
|
reject(new Error('error emitted'))
|
|
|
|
})
|
|
|
|
emittedOnce(response, 'end')
|
|
|
|
.then(() => {
|
|
|
|
const receivedBodyData = Buffer.concat(buffers)
|
|
|
|
expect(receivedBodyData.toString()).to.equal(bodyData)
|
|
|
|
})
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
emittedOnce(urlRequest, 'close')
|
|
|
|
])
|
|
|
|
|
|
|
|
urlRequest.end()
|
|
|
|
|
|
|
|
await eventHandlers
|
|
|
|
|
|
|
|
expect(requestResponseEventEmitted).to.equal(true)
|
|
|
|
expect(responseDataEventEmitted).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-08-27 16:12:33 +00:00
|
|
|
describe('webRequest', () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
afterEach(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(null)
|
|
|
|
})
|
|
|
|
|
2019-07-25 17:20:02 +00:00
|
|
|
it('Should throw when invalid filters are passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
{ urls: ['*://www.googleapis.com'] },
|
|
|
|
(details, callback) => { callback({ cancel: false }) }
|
|
|
|
)
|
|
|
|
}).to.throw('Invalid url pattern *://www.googleapis.com: Empty path.')
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
{ urls: [ '*://www.googleapis.com/', '*://blahblah.dev' ] },
|
|
|
|
(details, callback) => { callback({ cancel: false }) }
|
|
|
|
)
|
|
|
|
}).to.throw('Invalid url pattern *://blahblah.dev: Empty path.')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not throw when valid filters are passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
{ urls: ['*://www.googleapis.com/'] },
|
|
|
|
(details, callback) => { callback({ cancel: false }) }
|
|
|
|
)
|
|
|
|
}).to.not.throw()
|
|
|
|
})
|
|
|
|
|
2019-06-14 23:26:07 +00:00
|
|
|
it('Requests should be intercepted by webRequest module', (done) => {
|
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
|
|
|
let requestIsRedirected = false
|
|
|
|
respondOnce.toURL(redirectUrl, (request, response) => {
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
|
|
|
let requestIsIntercepted = false
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
(details, callback) => {
|
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request(`${serverUrl}${requestUrl}`)
|
|
|
|
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL')
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should to able to create and intercept a request using a custom session object', (done) => {
|
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
|
|
|
const customPartitionName = 'custom-partition'
|
|
|
|
let requestIsRedirected = false
|
|
|
|
respondOnce.toURL(redirectUrl, (request, response) => {
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
2019-11-01 20:37:02 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(() => {
|
2019-06-14 23:26:07 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session')
|
|
|
|
})
|
|
|
|
|
|
|
|
const customSession = session.fromPartition(customPartitionName, { cache: false })
|
|
|
|
let requestIsIntercepted = false
|
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`,
|
|
|
|
session: customSession
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
2019-11-01 20:37:02 +00:00
|
|
|
response.on('data', () => {})
|
2019-06-14 23:26:07 +00:00
|
|
|
response.on('end', () => {
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL')
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should to able to create and intercept a request using a custom partition name', (done) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
|
|
|
const customPartitionName = 'custom-partition'
|
|
|
|
let requestIsRedirected = false
|
|
|
|
respondOnce.toURL(redirectUrl, (request, response) => {
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrl => {
|
2019-11-01 20:37:02 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(() => {
|
2019-06-14 23:26:07 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session')
|
|
|
|
})
|
|
|
|
|
|
|
|
const customSession = session.fromPartition(customPartitionName, { cache: false })
|
|
|
|
let requestIsIntercepted = false
|
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`,
|
|
|
|
partition: customPartitionName
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
2019-11-01 20:37:02 +00:00
|
|
|
response.on('data', () => {})
|
2019-06-14 23:26:07 +00:00
|
|
|
response.on('end', () => {
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL')
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw when calling getHeader without a name', () => {
|
|
|
|
expect(() => {
|
|
|
|
(net.request({ url: 'https://test' }).getHeader as any)()
|
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)/)
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
net.request({ url: 'https://test' }).getHeader(null as any)
|
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw when calling removeHeader without a name', () => {
|
|
|
|
expect(() => {
|
|
|
|
(net.request({ url: 'https://test' }).removeHeader as any)()
|
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)/)
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
net.request({ url: 'https://test' }).removeHeader(null as any)
|
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)/)
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should follow redirect when no redirect handler is provided', (done) => {
|
2019-11-27 01:01:13 +00:00
|
|
|
const requestUrl = '/302'
|
2019-06-14 23:26:07 +00:00
|
|
|
respondOnce.toRoutes({
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
|
|
|
response.statusCode = 302
|
2019-06-14 23:26:07 +00:00
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.end()
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2019-06-14 23:26:07 +00:00
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should follow redirect chain when no redirect handler is provided', (done) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
respondOnce.toRoutes({
|
|
|
|
'/redirectChain': (request, response) => {
|
2019-11-27 01:01:13 +00:00
|
|
|
response.statusCode = 302
|
|
|
|
response.setHeader('Location', '/302')
|
2019-06-14 23:26:07 +00:00
|
|
|
response.end()
|
|
|
|
},
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
|
|
|
response.statusCode = 302
|
2019-06-14 23:26:07 +00:00
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.end()
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2019-06-14 23:26:07 +00:00
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}/redirectChain`
|
|
|
|
})
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should not follow redirect when request is canceled in redirect handler', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-11-27 01:01:13 +00:00
|
|
|
response.statusCode = 302
|
2019-06-14 23:26:07 +00:00
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
urlRequest.on('redirect', () => { urlRequest.abort() })
|
2019-11-27 01:01:13 +00:00
|
|
|
urlRequest.on('error', () => {})
|
2019-11-25 20:56:18 +00:00
|
|
|
await emittedOnce(urlRequest, 'abort')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
it('should not follow redirect when mode is error', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 302
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
redirect: 'error'
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
await emittedOnce(urlRequest, 'error')
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should follow redirect when handler calls callback', async () => {
|
|
|
|
const serverUrl = await respondOnce.toRoutes({
|
2019-06-14 23:26:07 +00:00
|
|
|
'/redirectChain': (request, response) => {
|
2019-11-27 01:01:13 +00:00
|
|
|
response.statusCode = 302
|
|
|
|
response.setHeader('Location', '/302')
|
2019-06-14 23:26:07 +00:00
|
|
|
response.end()
|
|
|
|
},
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
|
|
|
response.statusCode = 302
|
2019-06-14 23:26:07 +00:00
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
|
|
|
response.statusCode = 200
|
|
|
|
response.end()
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-27 01:01:13 +00:00
|
|
|
const urlRequest = net.request({ url: `${serverUrl}/redirectChain`, redirect: 'manual' })
|
2019-11-25 20:56:18 +00:00
|
|
|
const redirects: string[] = []
|
|
|
|
urlRequest.on('redirect', (status, method, url) => {
|
|
|
|
redirects.push(url)
|
|
|
|
urlRequest.followRedirect()
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.end()
|
|
|
|
const [response] = await emittedOnce(urlRequest, 'response')
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(redirects).to.deep.equal([
|
2019-11-27 01:01:13 +00:00
|
|
|
`${serverUrl}/302`,
|
2019-11-25 20:56:18 +00:00
|
|
|
`${serverUrl}/200`
|
|
|
|
])
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw if given an invalid session option', () => {
|
|
|
|
expect(() => {
|
|
|
|
net.request({
|
|
|
|
url: 'https://foo',
|
2019-08-05 17:45:58 +00:00
|
|
|
session: 1 as any
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-01 20:37:02 +00:00
|
|
|
}).to.throw('`session` should be an instance of the Session class')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw if given an invalid partition option', () => {
|
|
|
|
expect(() => {
|
|
|
|
net.request({
|
|
|
|
url: 'https://foo',
|
2019-08-05 17:45:58 +00:00
|
|
|
partition: 1 as any
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-01 20:37:02 +00:00
|
|
|
}).to.throw('`partition` should be a string')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to create a request with options', (done) => {
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
respondOnce.toURL('/', (request, response) => {
|
2019-11-25 20:56:18 +00:00
|
|
|
try {
|
|
|
|
expect(request.method).to.equal('GET')
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
|
|
|
} catch (e) {
|
|
|
|
return done(e)
|
|
|
|
}
|
2019-06-14 23:26:07 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
}).then(serverUrlUnparsed => {
|
|
|
|
const serverUrl = url.parse(serverUrlUnparsed)
|
|
|
|
const options = {
|
2019-08-05 17:45:58 +00:00
|
|
|
port: serverUrl.port ? parseInt(serverUrl.port, 10) : undefined,
|
2019-06-14 23:26:07 +00:00
|
|
|
hostname: '127.0.0.1',
|
|
|
|
headers: { [customHeaderName]: customHeaderValue }
|
|
|
|
}
|
|
|
|
const urlRequest = net.request(options)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
expect(response.statusCode).to.be.equal(200)
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to pipe a readable stream into a net request', (done) => {
|
|
|
|
const bodyData = randomString(kOneMegaByte)
|
|
|
|
let netRequestReceived = false
|
|
|
|
let netRequestEnded = false
|
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
respondOnce.toSingleURL((request, response) => response.end(bodyData)),
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
netRequestReceived = true
|
|
|
|
let receivedBodyData = ''
|
|
|
|
request.on('data', (chunk) => {
|
|
|
|
receivedBodyData += chunk.toString()
|
|
|
|
})
|
|
|
|
request.on('end', (chunk: Buffer | undefined) => {
|
|
|
|
netRequestEnded = true
|
|
|
|
if (chunk) {
|
|
|
|
receivedBodyData += chunk.toString()
|
|
|
|
}
|
|
|
|
expect(receivedBodyData).to.be.equal(bodyData)
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
]).then(([nodeServerUrl, netServerUrl]) => {
|
|
|
|
const nodeRequest = http.request(nodeServerUrl)
|
|
|
|
nodeRequest.on('response', (nodeResponse) => {
|
|
|
|
const netRequest = net.request(netServerUrl)
|
|
|
|
netRequest.on('response', (netResponse) => {
|
|
|
|
expect(netResponse.statusCode).to.equal(200)
|
2019-11-01 20:37:02 +00:00
|
|
|
netResponse.on('data', () => {})
|
2019-06-14 23:26:07 +00:00
|
|
|
netResponse.on('end', () => {
|
|
|
|
expect(netRequestReceived).to.be.true('net request received')
|
|
|
|
expect(netRequestEnded).to.be.true('net request ended')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
nodeResponse.pipe(netRequest)
|
|
|
|
})
|
|
|
|
nodeRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-06 21:14:44 +00:00
|
|
|
it('should report upload progress', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
const netRequest = net.request({ url: serverUrl, method: 'POST' })
|
|
|
|
expect(netRequest.getUploadProgress()).to.deep.equal({ active: false })
|
|
|
|
netRequest.end(Buffer.from('hello'))
|
|
|
|
const [position, total] = await emittedOnce(netRequest, 'upload-progress')
|
|
|
|
expect(netRequest.getUploadProgress()).to.deep.equal({ active: true, started: true, current: position, total })
|
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should emit error event on server socket destroy', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
request.socket.destroy()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end()
|
|
|
|
const [error] = await emittedOnce(urlRequest, 'error')
|
|
|
|
expect(error.message).to.equal('net::ERR_EMPTY_RESPONSE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should emit error event on server request destroy', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
request.destroy()
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
const [error] = await emittedOnce(urlRequest, 'error')
|
|
|
|
expect(error.message).to.be.oneOf(['net::ERR_CONNECTION_RESET', 'net::ERR_CONNECTION_ABORTED'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not emit any event after close', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end()
|
|
|
|
|
|
|
|
await emittedOnce(urlRequest, 'close')
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
['finish', 'abort', 'close', 'error'].forEach(evName => {
|
|
|
|
urlRequest.on(evName as any, () => {
|
|
|
|
reject(new Error(`Unexpected ${evName} event`))
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
setTimeout(resolve, 50)
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('IncomingMessage API', () => {
|
2019-11-25 20:56:18 +00:00
|
|
|
it('response object should implement the IncomingMessage API', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
response.end()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end()
|
|
|
|
const [response] = await emittedOnce(urlRequest, 'response')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const headers = response.headers
|
|
|
|
expect(headers).to.be.an('object')
|
|
|
|
const headerValue = headers[customHeaderName.toLowerCase()]
|
|
|
|
expect(headerValue).to.equal(customHeaderValue)
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const httpVersion = response.httpVersion
|
|
|
|
expect(httpVersion).to.be.a('string').and.to.have.lengthOf.at.least(1)
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const httpVersionMajor = response.httpVersionMajor
|
|
|
|
expect(httpVersionMajor).to.be.a('number').and.to.be.at.least(1)
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const httpVersionMinor = response.httpVersionMinor
|
|
|
|
expect(httpVersionMinor).to.be.a('number').and.to.be.at.least(0)
|
|
|
|
|
|
|
|
response.on('data', () => {})
|
|
|
|
await emittedOnce(response, 'end')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should discard duplicate headers', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
const includedHeader = 'max-forwards'
|
|
|
|
const discardableHeader = 'Max-Forwards'
|
|
|
|
|
|
|
|
const includedHeaderValue = 'max-fwds-val'
|
|
|
|
const discardableHeaderValue = 'max-fwds-val-two'
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader(discardableHeader, discardableHeaderValue)
|
|
|
|
response.setHeader(includedHeader, includedHeaderValue)
|
|
|
|
response.end()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end()
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const [response] = await emittedOnce(urlRequest, 'response')
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const headers = response.headers
|
|
|
|
expect(headers).to.be.an('object')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
expect(headers).to.have.property(includedHeader)
|
|
|
|
expect(headers).to.not.have.property(discardableHeader)
|
|
|
|
expect(headers[includedHeader]).to.equal(includedHeaderValue)
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('data', () => {})
|
|
|
|
await emittedOnce(response, 'end')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should join repeated non-discardable value with ,', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader('referrer-policy', ['first-text', 'second-text'])
|
|
|
|
response.end()
|
2019-11-25 20:56:18 +00:00
|
|
|
})
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.end()
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const [response] = await emittedOnce(urlRequest, 'response')
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const headers = response.headers
|
|
|
|
expect(headers).to.be.an('object')
|
|
|
|
expect(headers).to.have.property('referrer-policy')
|
|
|
|
expect(headers['referrer-policy']).to.equal('first-text, second-text')
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('data', () => {})
|
|
|
|
await emittedOnce(response, 'end')
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
it('should be able to pipe a net response into a writable stream', async () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
const bodyData = randomString(kOneKiloByte)
|
2019-11-27 01:01:13 +00:00
|
|
|
const [netServerUrl, nodeServerUrl] = await Promise.all([
|
2019-06-14 23:26:07 +00:00
|
|
|
respondOnce.toSingleURL((request, response) => response.end(bodyData)),
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let receivedBodyData = ''
|
|
|
|
request.on('data', (chunk) => {
|
|
|
|
receivedBodyData += chunk.toString()
|
|
|
|
})
|
|
|
|
request.on('end', (chunk: Buffer | undefined) => {
|
|
|
|
if (chunk) {
|
|
|
|
receivedBodyData += chunk.toString()
|
|
|
|
}
|
|
|
|
expect(receivedBodyData).to.be.equal(bodyData)
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
})
|
2019-11-27 01:01:13 +00:00
|
|
|
])
|
|
|
|
const netRequest = net.request(netServerUrl)
|
|
|
|
netRequest.end()
|
|
|
|
await new Promise((resolve) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
netRequest.on('response', (netResponse) => {
|
|
|
|
const serverUrl = url.parse(nodeServerUrl)
|
|
|
|
const nodeOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
path: serverUrl.path,
|
|
|
|
port: serverUrl.port
|
|
|
|
}
|
|
|
|
const nodeRequest = http.request(nodeOptions, res => {
|
2019-11-01 20:37:02 +00:00
|
|
|
res.on('data', () => {})
|
2019-06-14 23:26:07 +00:00
|
|
|
res.on('end', () => {
|
2019-11-27 01:01:13 +00:00
|
|
|
resolve()
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
// TODO: IncomingMessage should properly extend ReadableStream in the
|
|
|
|
// docs
|
|
|
|
(netResponse as any).pipe(nodeRequest)
|
|
|
|
})
|
|
|
|
netRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Stability and performance', () => {
|
|
|
|
it('should free unreferenced, never-started request objects without crash', (done) => {
|
|
|
|
net.request('https://test')
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should collect on-going requests without crash', (done) => {
|
|
|
|
let finishResponse: (() => void) | null = null
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.write(randomString(kOneKiloByte))
|
|
|
|
finishResponse = () => {
|
|
|
|
response.write(randomString(kOneKiloByte))
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => { })
|
|
|
|
response.on('end', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
process.nextTick(() => {
|
|
|
|
// Trigger a garbage collection.
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
finishResponse!()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should collect unreferenced, ended requests without crash', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.end()
|
2019-11-27 01:01:13 +00:00
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => { done() })
|
|
|
|
})
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let received = Buffer.alloc(0)
|
|
|
|
request.on('data', (data) => {
|
|
|
|
received = Buffer.concat([received, data])
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
expect(received.length).to.equal(kOneMegaByte)
|
|
|
|
done()
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {})
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
})
|
|
|
|
})
|
2019-11-27 01:01:13 +00:00
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced for chunked encoding', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let received = Buffer.alloc(0)
|
|
|
|
request.on('data', (data) => {
|
|
|
|
received = Buffer.concat([received, data])
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
expect(received.length).to.equal(kOneMegaByte)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {})
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced before close event for chunked encoding', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let received = Buffer.alloc(0)
|
|
|
|
request.on('data', (data) => {
|
|
|
|
received = Buffer.concat([received, data])
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
expect(received.length).to.equal(kOneMegaByte)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {})
|
|
|
|
})
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
})
|
2019-11-25 20:56:18 +00:00
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let received = Buffer.alloc(0)
|
|
|
|
request.on('data', (data) => {
|
|
|
|
received = Buffer.concat([received, data])
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
expect(received.length).to.equal(kOneMegaByte)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {})
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced for chunked encoding', (done) => {
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
|
|
|
let received = Buffer.alloc(0)
|
|
|
|
request.on('data', (data) => {
|
|
|
|
received = Buffer.concat([received, data])
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
response.end()
|
|
|
|
expect(received.length).to.equal(kOneMegaByte)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}).then(serverUrl => {
|
|
|
|
const urlRequest = net.request(serverUrl)
|
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {})
|
|
|
|
response.on('end', () => {})
|
|
|
|
})
|
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
const v8Util = process.electronBinding('v8_util')
|
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte))
|
|
|
|
})
|
|
|
|
})
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
|
|
|
})
|