2019-05-20 17:04:18 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { remote } = require('electron')
|
|
|
|
const { ipcRenderer } = require('electron')
|
2016-10-07 09:07:49 +00:00
|
|
|
const http = require('http')
|
|
|
|
const url = require('url')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { net } = remote
|
|
|
|
const { session } = remote
|
2016-10-05 13:06:35 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
const { expect } = chai
|
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* The whole net API doesn't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
function randomBuffer (size, start, end) {
|
2016-10-07 14:34:36 +00:00
|
|
|
start = start || 0
|
|
|
|
end = end || 255
|
2018-10-02 01:56:31 +00:00
|
|
|
const range = 1 + end - start
|
2016-10-07 14:34:36 +00:00
|
|
|
const buffer = Buffer.allocUnsafe(size)
|
|
|
|
for (let i = 0; i < size; ++i) {
|
2016-10-12 10:29:25 +00:00
|
|
|
buffer[i] = start + Math.floor(Math.random() * range)
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
2016-10-12 10:29:25 +00:00
|
|
|
return buffer
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
function randomString (length) {
|
2018-10-02 01:56:31 +00:00
|
|
|
const buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
|
2016-10-12 10:29:25 +00:00
|
|
|
return buffer.toString()
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const kOneKiloByte = 1024
|
|
|
|
const kOneMegaByte = kOneKiloByte * kOneKiloByte
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('net module', () => {
|
2017-04-07 23:42:01 +00:00
|
|
|
let server
|
|
|
|
const connections = new Set()
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach((done) => {
|
2017-04-07 23:42:01 +00:00
|
|
|
server = http.createServer()
|
2017-10-27 20:45:58 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2017-04-07 23:42:01 +00:00
|
|
|
server.url = `http://127.0.0.1:${server.address().port}`
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
server.on('connection', (connection) => {
|
|
|
|
connections.add(connection)
|
|
|
|
connection.once('close', () => {
|
|
|
|
connections.delete(connection)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
})
|
2017-04-07 23:42:01 +00:00
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach((done) => {
|
2017-04-07 23:42:01 +00:00
|
|
|
for (const connection of connections) {
|
|
|
|
connection.destroy()
|
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
server.close(() => {
|
2016-10-07 09:07:49 +00:00
|
|
|
server = null
|
2017-04-07 23:42:01 +00:00
|
|
|
done()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2017-04-07 23:42:01 +00:00
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('HTTP basics', () => {
|
|
|
|
it('should be able to issue a basic GET request', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-07 09:07:49 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('GET')
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-07 09:07:49 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request(`${server.url}${requestUrl}`)
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-07 14:34:36 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-07 09:07:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-10-07 14:34:36 +00:00
|
|
|
response.resume()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to issue a basic POST request', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-07 09:07:49 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('POST')
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-07 09:07:49 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-07 14:34:36 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-07 09:07:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-10-07 14:34:36 +00:00
|
|
|
response.resume()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should fetch correct data in a GET request', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const bodyData = 'Hello World!'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-07 09:07:49 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('GET')
|
2016-10-12 10:29:25 +00:00
|
|
|
response.write(bodyData)
|
|
|
|
response.end()
|
|
|
|
break
|
2016-10-07 09:07:49 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request(`${server.url}${requestUrl}`)
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
let expectedBodyData = ''
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-07 14:34:36 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
expectedBodyData += chunk.toString()
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(expectedBodyData).to.equal(bodyData)
|
2016-10-07 09:07:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-10-07 14:34:36 +00:00
|
|
|
response.resume()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should post the correct data in a POST request', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const bodyData = 'Hello World!'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
let postedBodyData = ''
|
2016-10-07 09:07:49 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('POST')
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
postedBodyData += chunk.toString()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(postedBodyData).to.equal(bodyData)
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
break
|
2016-10-07 09:07:49 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-07 14:34:36 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {})
|
|
|
|
response.on('end', () => {
|
2016-10-07 09:07:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-10-07 14:34:36 +00:00
|
|
|
response.resume()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.write(bodyData)
|
|
|
|
urlRequest.end()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should support chunked encoding', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2016-10-07 14:34:36 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.chunkedEncoding = true
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('POST')
|
|
|
|
expect(request.headers['transfer-encoding']).to.equal('chunked')
|
|
|
|
expect(request.headers['content-length']).to.be.undefined()
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('data', (chunk) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
response.write(chunk)
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('end', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end(chunk)
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
break
|
2016-10-07 14:34:36 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let chunkIndex = 0
|
2017-10-27 20:45:58 +00:00
|
|
|
const chunkCount = 100
|
2018-10-02 01:56:31 +00:00
|
|
|
const sentChunks = []
|
|
|
|
const receivedChunks = []
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-07 14:34:36 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
receivedChunks.push(chunk)
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const sentData = Buffer.concat(sentChunks)
|
|
|
|
const receivedData = Buffer.concat(receivedChunks)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(sentData.toString()).to.equal(receivedData.toString())
|
|
|
|
expect(chunkIndex).to.be.equal(chunkCount)
|
2016-10-07 14:34:36 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.chunkedEncoding = true
|
2016-10-12 10:29:25 +00:00
|
|
|
while (chunkIndex < chunkCount) {
|
2017-10-27 20:45:58 +00:00
|
|
|
chunkIndex += 1
|
|
|
|
const chunk = randomBuffer(kOneKiloByte)
|
2016-10-12 10:29:25 +00:00
|
|
|
sentChunks.push(chunk)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.write(chunk)).to.be.true()
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-07 14:34:36 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ClientRequest API', () => {
|
|
|
|
afterEach(() => {
|
2016-10-11 17:16:23 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(null)
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('request/response objects should emit expected events', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
const bodyData = randomString(kOneMegaByte)
|
|
|
|
server.on('request', (request, response) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2016-10-07 14:34:36 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.write(bodyData)
|
|
|
|
response.end()
|
|
|
|
break
|
2016-10-07 14:34:36 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-07 14:34:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestResponseEventEmitted = false
|
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
|
|
|
let responseDataEventEmitted = false
|
|
|
|
let responseEndEventEmitted = false
|
2016-10-07 14:34:36 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
function maybeDone (done) {
|
|
|
|
if (!requestCloseEventEmitted || !responseEndEventEmitted) {
|
2016-10-07 14:34:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestResponseEventEmitted).to.be.true()
|
|
|
|
expect(requestFinishEventEmitted).to.be.true()
|
|
|
|
expect(requestCloseEventEmitted).to.be.true()
|
|
|
|
expect(responseDataEventEmitted).to.be.true()
|
|
|
|
expect(responseEndEventEmitted).to.be.true()
|
2016-10-07 14:34:36 +00:00
|
|
|
done()
|
|
|
|
}
|
2016-10-05 13:06:35 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-07 14:34:36 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestResponseEventEmitted = true
|
2016-10-05 13:06:35 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2018-10-02 01:56:31 +00:00
|
|
|
const buffers = []
|
2016-10-12 10:29:25 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-07 14:34:36 +00:00
|
|
|
buffers.push(chunk)
|
2016-10-12 10:29:25 +00:00
|
|
|
responseDataEventEmitted = true
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const receivedBodyData = Buffer.concat(buffers)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(receivedBodyData.toString()).to.equal(bodyData)
|
2016-10-12 10:29:25 +00:00
|
|
|
responseEndEventEmitted = true
|
2016-10-07 14:34:36 +00:00
|
|
|
maybeDone(done)
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
response.resume()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('error', (error) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error).to.be.an('Error')
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('aborted', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('response aborted')
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestFinishEventEmitted = true
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', (error) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error).to.be.an('Error')
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('request aborted')
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2016-10-07 14:34:36 +00:00
|
|
|
maybeDone(done)
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-07 14:34:36 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to set a custom HTTP request header before first write', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-10 08:11:22 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 08:11:22 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 08:11:22 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-10 08:11:22 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.write('')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to set a non-string object as a header value', (done) => {
|
2017-03-30 16:24:40 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Integer-Value'
|
|
|
|
const customHeaderValue = 900
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-30 16:24:40 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue.toString())
|
2017-03-30 16:24:40 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.url).to.equal(requestUrl)
|
2017-03-30 16:24:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2017-03-30 16:24:40 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2017-03-30 16:24:40 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2017-03-30 16:24:40 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
2017-03-30 16:24:40 +00:00
|
|
|
urlRequest.write('')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue)
|
2017-03-30 16:24:40 +00:00
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should not be able to set a custom HTTP request header after first write', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.be.undefined()
|
2016-10-10 08:11:22 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-10 08:11:22 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 08:11:22 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 08:11:22 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-10 08:11:22 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.write('')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw()
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.be.undefined()
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to remove a custom HTTP request header before first write', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.be.undefined()
|
2016-10-10 08:11:22 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-10 08:11:22 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 08:11:22 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 08:11:22 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-10 08:11:22 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.removeHeader(customHeaderName)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.be.undefined()
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.write('')
|
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should not be able to remove a custom HTTP request header after first write', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-10 08:11:22 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 08:11:22 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 08:11:22 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-10 08:11:22 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-10 08:11:22 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-10 08:11:22 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.write('')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.removeHeader(customHeaderName)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw()
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue)
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to set cookie header line', (done) => {
|
2016-12-21 06:22:27 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const cookieHeaderName = 'Cookie'
|
|
|
|
const cookieHeaderValue = 'test=12345'
|
|
|
|
const customSession = session.fromPartition('test-cookie-header')
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-12-21 06:22:27 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.headers[cookieHeaderName.toLowerCase()]).to.equal(cookieHeaderValue)
|
2016-12-21 06:22:27 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-12-21 06:22:27 +00:00
|
|
|
}
|
|
|
|
})
|
2019-01-25 18:11:35 +00:00
|
|
|
|
2016-12-21 06:22:27 +00:00
|
|
|
customSession.cookies.set({
|
|
|
|
url: `${server.url}`,
|
|
|
|
name: 'test',
|
|
|
|
value: '11111'
|
2019-01-25 18:11:35 +00:00
|
|
|
}).then(() => { // resolved
|
2016-12-21 06:22:27 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
session: customSession
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-12-21 06:22:27 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-12-21 06:22:27 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {})
|
|
|
|
response.on('end', () => {
|
2016-12-21 06:22:27 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.setHeader(cookieHeaderName, cookieHeaderValue)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.getHeader(cookieHeaderName)).to.equal(cookieHeaderValue)
|
2016-12-21 06:22:27 +00:00
|
|
|
urlRequest.end()
|
2019-01-25 18:11:35 +00:00
|
|
|
}, (error) => {
|
|
|
|
done(error)
|
2016-12-21 06:22:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to abort an HTTP request before first write', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-08-07 20:30:10 +00:00
|
|
|
response.end()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected request event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2016-10-10 08:11:22 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestAbortEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
2016-10-07 09:07:49 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 14:21:12 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected response event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected finish event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestAbortEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestAbortEventEmitted).to.be.true()
|
|
|
|
expect(requestCloseEventEmitted).to.be.true()
|
2016-10-12 10:29:25 +00:00
|
|
|
done()
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
urlRequest.abort()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(urlRequest.write('')).to.be.false()
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('it should be able to abort an HTTP request before request end', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
let requestReceivedByServer = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
|
|
|
requestReceivedByServer = true
|
|
|
|
cancelRequest()
|
|
|
|
break
|
2016-10-10 14:21:12 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestAbortEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 14:21:12 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected response event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected finish event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestAbortEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestReceivedByServer).to.be.true()
|
|
|
|
expect(requestAbortEventEmitted).to.be.true()
|
|
|
|
expect(requestCloseEventEmitted).to.be.true()
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.chunkedEncoding = true
|
|
|
|
urlRequest.write(randomString(kOneKiloByte))
|
2016-10-12 10:29:25 +00:00
|
|
|
function cancelRequest () {
|
2016-10-10 14:21:12 +00:00
|
|
|
urlRequest.abort()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('it should be able to abort an HTTP request after request end and before response', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
let requestReceivedByServer = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
|
|
|
requestReceivedByServer = true
|
|
|
|
cancelRequest()
|
|
|
|
process.nextTick(() => {
|
2016-10-10 14:21:12 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
break
|
2016-10-10 14:21:12 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestAbortEventEmitted = false
|
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 14:21:12 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected response event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestFinishEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestAbortEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestFinishEventEmitted).to.be.true()
|
|
|
|
expect(requestReceivedByServer).to.be.true()
|
|
|
|
expect(requestAbortEventEmitted).to.be.true()
|
|
|
|
expect(requestCloseEventEmitted).to.be.true()
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
2016-10-12 10:29:25 +00:00
|
|
|
function cancelRequest () {
|
2016-10-10 14:21:12 +00:00
|
|
|
urlRequest.abort()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('it should be able to abort an HTTP request after response start', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
let requestReceivedByServer = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
|
|
|
requestReceivedByServer = true
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.write(randomString(kOneKiloByte))
|
|
|
|
break
|
2016-10-10 14:21:12 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestResponseEventEmitted = false
|
|
|
|
let requestAbortEventEmitted = false
|
|
|
|
let requestCloseEventEmitted = false
|
|
|
|
let responseAbortedEventEmitted = false
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-10 14:21:12 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestResponseEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-12 10:29:25 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected end event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
response.resume()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('aborted', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
responseAbortedEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
urlRequest.abort()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestFinishEventEmitted = true
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestAbortEventEmitted = true
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2019-05-20 17:04:18 +00:00
|
|
|
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(requestAbortEventEmitted).to.be.true('request should emit "abort" event')
|
|
|
|
expect(responseAbortedEventEmitted).to.be.true('response should emit "aborted" event')
|
|
|
|
expect(requestCloseEventEmitted).to.be.true('request should emit "close" event')
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('abort event should be emitted at most once', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
let requestReceivedByServer = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-11 13:25:26 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
|
|
|
requestReceivedByServer = true
|
|
|
|
cancelRequest()
|
|
|
|
break
|
2016-10-11 13:25:26 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestFinishEventEmitted = false
|
|
|
|
let requestAbortEventCount = 0
|
|
|
|
let requestCloseEventEmitted = false
|
2016-10-11 13:25:26 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-11 13:25:26 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected response event')
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestFinishEventEmitted = true
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Unexpected error event')
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
++requestAbortEventCount
|
2016-10-11 13:25:26 +00:00
|
|
|
urlRequest.abort()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-12 10:29:25 +00:00
|
|
|
requestCloseEventEmitted = true
|
2016-10-11 13:25:26 +00:00
|
|
|
// Let all pending async events to be emitted
|
2017-10-27 20:45:58 +00:00
|
|
|
setTimeout(() => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestFinishEventEmitted).to.be.true()
|
|
|
|
expect(requestReceivedByServer).to.be.true()
|
|
|
|
expect(requestAbortEventCount).to.equal(1)
|
|
|
|
expect(requestCloseEventEmitted).to.be.true()
|
2016-10-11 13:25:26 +00:00
|
|
|
done()
|
|
|
|
}, 500)
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.end(randomString(kOneKiloByte))
|
2016-10-12 10:29:25 +00:00
|
|
|
function cancelRequest () {
|
2016-10-11 13:25:26 +00:00
|
|
|
urlRequest.abort()
|
|
|
|
urlRequest.abort()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('Requests should be intercepted by webRequest module', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
|
|
|
let requestIsRedirected = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case redirectUrl:
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestIsIntercepted = false
|
2016-10-10 14:21:12 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
2017-10-27 20:45:58 +00:00
|
|
|
(details, callback) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
if (details.url === `${server.url}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
2017-11-23 22:22:43 +00:00
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2016-10-10 14:21:12 +00:00
|
|
|
callback({
|
2016-10-12 10:29:25 +00:00
|
|
|
redirectURL: `${server.url}${redirectUrl}`
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
} else {
|
2016-10-12 10:29:25 +00:00
|
|
|
callback({
|
2016-10-10 14:21:12 +00:00
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request(`${server.url}${requestUrl}`)
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-10 14:21:12 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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')
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should to able to create and intercept a request using a custom session object', (done) => {
|
2016-10-25 14:19:26 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
|
|
|
const customPartitionName = 'custom-partition'
|
|
|
|
let requestIsRedirected = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-25 14:19:26 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case redirectUrl:
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-25 14:19:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session')
|
2016-10-25 14:19:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const customSession = session.fromPartition(customPartitionName, { cache: false })
|
2016-10-25 14:19:26 +00:00
|
|
|
let requestIsIntercepted = false
|
2017-10-27 20:45:58 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${server.url}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
2017-11-23 22:22:43 +00:00
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2017-10-27 20:45:58 +00:00
|
|
|
callback({
|
|
|
|
redirectURL: `${server.url}${redirectUrl}`
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2016-10-25 14:19:26 +00:00
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
session: customSession
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-25 14:19:26 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-25 14:19:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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')
|
2016-10-25 14:19:26 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should throw if given an invalid redirect mode', () => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-03-28 13:35:44 +00:00
|
|
|
const options = {
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
redirect: 'custom'
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2017-03-28 13:35:44 +00:00
|
|
|
net.request(options)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('redirect mode should be one of follow, error or manual')
|
2017-03-23 19:37:54 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should throw when calling getHeader without a name', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
net.request({ url: `${server.url}/requestUrl` }).getHeader()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)\./)
|
2017-03-30 19:37:40 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
net.request({ url: `${server.url}/requestUrl` }).getHeader(null)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)\./)
|
2017-03-30 19:37:40 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should throw when calling removeHeader without a name', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
net.request({ url: `${server.url}/requestUrl` }).removeHeader()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)\./)
|
2017-03-30 19:37:40 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
net.request({ url: `${server.url}/requestUrl` }).removeHeader(null)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)\./)
|
2017-03-30 19:37:40 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should follow redirect when no redirect mode is provided', (done) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/301'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2017-03-23 19:37:54 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should follow redirect chain when no redirect mode is provided', (done) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/redirectChain'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case '/redirectChain':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/301')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2017-03-23 19:37:54 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should not follow redirect when mode is error', (done) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/301'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
redirect: 'error'
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', (error) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error.message).to.equal('Request cannot follow redirect with the current redirect mode')
|
2017-03-23 19:37:54 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2017-03-23 19:37:54 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should allow follow redirect when mode is manual', (done) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/redirectChain'
|
|
|
|
let redirectCount = 0
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case '/redirectChain':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/301')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
redirect: 'manual'
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(redirectCount).to.equal(2)
|
2017-03-23 19:37:54 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('redirect', (status, method, url) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
if (url === `${server.url}/301` || url === `${server.url}/200`) {
|
|
|
|
redirectCount += 1
|
|
|
|
urlRequest.followRedirect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should allow cancelling redirect when mode is manual', (done) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
const requestUrl = '/redirectChain'
|
|
|
|
let redirectCount = 0
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case '/redirectChain':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/redirect/1')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/redirect/1':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.setHeader('Location', '/redirect/2')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/redirect/2':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
redirect: 'manual'
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).that.equal(200)
|
2017-03-23 19:37:54 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2017-03-23 19:37:54 +00:00
|
|
|
urlRequest.abort()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(redirectCount).to.equal(1)
|
2017-03-23 19:37:54 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('redirect', (status, method, url) => {
|
2017-03-23 19:37:54 +00:00
|
|
|
if (url === `${server.url}/redirect/1`) {
|
|
|
|
redirectCount += 1
|
|
|
|
urlRequest.followRedirect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should throw if given an invalid session option', (done) => {
|
2016-10-25 14:55:17 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2016-10-25 14:19:26 +00:00
|
|
|
try {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
session: 1
|
|
|
|
})
|
2017-11-23 22:22:43 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line
|
2016-10-25 14:55:17 +00:00
|
|
|
urlRequest
|
2016-10-25 14:19:26 +00:00
|
|
|
} catch (exception) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should to able to create and intercept a request using a custom partition name', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const redirectUrl = '/redirectUrl'
|
2016-10-25 13:47:54 +00:00
|
|
|
const customPartitionName = 'custom-partition'
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestIsRedirected = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case redirectUrl:
|
|
|
|
requestIsRedirected = true
|
|
|
|
response.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session')
|
2017-10-27 20:45:58 +00:00
|
|
|
})
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const customSession = session.fromPartition(customPartitionName, {
|
2016-10-10 14:21:12 +00:00
|
|
|
cache: false
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
let requestIsIntercepted = false
|
2017-10-27 20:45:58 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${server.url}${requestUrl}`) {
|
|
|
|
requestIsIntercepted = true
|
|
|
|
callback({
|
|
|
|
redirectURL: `${server.url}${redirectUrl}`
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2016-10-10 14:21:12 +00:00
|
|
|
|
|
|
|
const urlRequest = net.request({
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`,
|
2016-10-25 13:47:54 +00:00
|
|
|
partition: customPartitionName
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2016-10-10 14:21:12 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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')
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should throw if given an invalid partition option', (done) => {
|
2016-10-25 14:55:17 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2016-10-25 14:19:26 +00:00
|
|
|
try {
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${server.url}${requestUrl}`,
|
|
|
|
partition: 1
|
|
|
|
})
|
2017-11-23 22:22:43 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line
|
2016-10-25 14:55:17 +00:00
|
|
|
urlRequest
|
2016-10-25 14:19:26 +00:00
|
|
|
} catch (exception) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to create a request with options', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(request.method).to.equal('GET')
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue)
|
2016-10-11 17:16:23 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.end()
|
|
|
|
break
|
2016-10-10 14:21:12 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-10 14:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-11 17:16:23 +00:00
|
|
|
const serverUrl = url.parse(server.url)
|
2018-10-02 01:56:31 +00:00
|
|
|
const options = {
|
2016-10-11 17:16:23 +00:00
|
|
|
port: serverUrl.port,
|
|
|
|
hostname: '127.0.0.1',
|
2016-10-10 14:21:12 +00:00
|
|
|
headers: {}
|
|
|
|
}
|
2016-10-12 10:29:25 +00:00
|
|
|
options.headers[customHeaderName] = customHeaderValue
|
2016-10-10 14:21:12 +00:00
|
|
|
const urlRequest = net.request(options)
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(response.statusCode).to.be.equal(200)
|
2016-10-10 14:21:12 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-10 14:21:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 14:21:12 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to pipe a readable stream into a net request', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const nodeRequestUrl = '/nodeRequestUrl'
|
|
|
|
const netRequestUrl = '/netRequestUrl'
|
|
|
|
const bodyData = randomString(kOneMegaByte)
|
|
|
|
let netRequestReceived = false
|
|
|
|
let netRequestEnded = false
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-11 13:25:26 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case nodeRequestUrl:
|
|
|
|
response.write(bodyData)
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case netRequestUrl:
|
|
|
|
netRequestReceived = true
|
|
|
|
let receivedBodyData = ''
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
receivedBodyData += chunk.toString()
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('end', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
netRequestEnded = true
|
2016-10-11 13:25:26 +00:00
|
|
|
if (chunk) {
|
2016-10-12 10:29:25 +00:00
|
|
|
receivedBodyData += chunk.toString()
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(receivedBodyData).to.be.equal(bodyData)
|
2016-10-11 13:25:26 +00:00
|
|
|
response.end()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
break
|
2016-10-11 13:25:26 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const nodeRequest = http.request(`${server.url}${nodeRequestUrl}`)
|
2017-10-27 20:45:58 +00:00
|
|
|
nodeRequest.on('response', (nodeResponse) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const netRequest = net.request(`${server.url}${netRequestUrl}`)
|
2017-10-27 20:45:58 +00:00
|
|
|
netRequest.on('response', (netResponse) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(netResponse.statusCode).to.be.equal(200)
|
2016-10-11 13:25:26 +00:00
|
|
|
netResponse.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
netResponse.on('data', (chunk) => {})
|
|
|
|
netResponse.on('end', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(netRequestReceived).to.be.true()
|
|
|
|
expect(netRequestEnded).to.be.true()
|
2016-10-11 13:25:26 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
netResponse.resume()
|
|
|
|
})
|
|
|
|
nodeResponse.pipe(netRequest)
|
|
|
|
})
|
|
|
|
nodeRequest.end()
|
|
|
|
})
|
2016-10-11 17:16:23 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should emit error event on server socket close', (done) => {
|
2016-10-13 15:14:23 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-13 15:14:23 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
request.socket.destroy()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-13 15:14:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
let requestErrorEventEmitted = false
|
|
|
|
const urlRequest = net.request(`${server.url}${requestUrl}`)
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', (error) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error).to.be.an('Error')
|
2016-10-13 15:14:23 +00:00
|
|
|
requestErrorEventEmitted = true
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestErrorEventEmitted).to.be.true()
|
2016-10-13 15:14:23 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('IncomingMessage API', () => {
|
|
|
|
it('response object should implement the IncomingMessage API', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
const customHeaderName = 'Some-Custom-Header-Name'
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value'
|
2019-04-02 14:41:19 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-11 13:25:26 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case requestUrl:
|
2016-10-11 13:25:26 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.setHeader(customHeaderName, customHeaderValue)
|
|
|
|
response.end()
|
|
|
|
break
|
2016-10-11 13:25:26 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
|
|
|
})
|
2019-04-02 14:41:19 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-11 13:25:26 +00:00
|
|
|
method: 'GET',
|
2016-10-12 10:29:25 +00:00
|
|
|
url: `${server.url}${requestUrl}`
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2019-04-02 14:41:19 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-04-02 14:41:19 +00:00
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
const headers = response.headers
|
2019-04-02 14:41:19 +00:00
|
|
|
expect(headers).to.be.an('object')
|
|
|
|
const headerValue = headers[customHeaderName.toLowerCase()]
|
|
|
|
expect(headerValue).to.equal(customHeaderValue)
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const httpVersion = response.httpVersion
|
2019-04-02 14:41:19 +00:00
|
|
|
expect(httpVersion).to.be.a('string').and.to.have.lengthOf.at.least(1)
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const httpVersionMajor = response.httpVersionMajor
|
2019-04-02 14:41:19 +00:00
|
|
|
expect(httpVersionMajor).to.be.a('number').and.to.be.at.least(1)
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
const httpVersionMinor = response.httpVersionMinor
|
2019-04-02 14:41:19 +00:00
|
|
|
expect(httpVersionMinor).to.be.a('number').and.to.be.at.least(0)
|
|
|
|
|
2016-10-11 13:25:26 +00:00
|
|
|
response.pause()
|
2019-04-02 14:41:19 +00:00
|
|
|
response.on('data', chunk => {})
|
|
|
|
response.on('end', () => { done() })
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should discard duplicate headers', (done) => {
|
|
|
|
const requestUrl = '/duplicateRequestUrl'
|
|
|
|
const includedHeader = 'max-forwards'
|
|
|
|
const discardableHeader = 'Max-Forwards'
|
|
|
|
|
|
|
|
const includedHeaderValue = 'max-fwds-val'
|
|
|
|
const discardableHeaderValue = 'max-fwds-val-two'
|
|
|
|
|
|
|
|
server.on('request', (request, response) => {
|
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader(discardableHeader, discardableHeaderValue)
|
|
|
|
response.setHeader(includedHeader, includedHeaderValue)
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
handleUnexpectedURL(request, response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.on('response', response => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
|
|
|
|
|
|
|
const headers = response.headers
|
|
|
|
expect(headers).to.be.an('object')
|
|
|
|
|
|
|
|
expect(headers).to.have.property(includedHeader)
|
|
|
|
expect(headers).to.not.have.property(discardableHeader)
|
|
|
|
expect(headers[includedHeader]).to.equal(includedHeaderValue)
|
|
|
|
|
|
|
|
response.pause()
|
|
|
|
response.on('data', chunk => {})
|
|
|
|
response.on('end', () => { done() })
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should join repeated non-discardable value with ,', (done) => {
|
|
|
|
const requestUrl = '/requestUrl'
|
|
|
|
|
|
|
|
server.on('request', (request, response) => {
|
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.setHeader('referrer-policy', ['first-text', 'second-text'])
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
handleUnexpectedURL(request, response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
|
|
|
|
|
|
|
urlRequest.on('response', response => {
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.statusMessage).to.equal('OK')
|
|
|
|
|
|
|
|
const headers = response.headers
|
|
|
|
expect(headers).to.be.an('object')
|
|
|
|
expect(headers).to.have.a.property('referrer-policy')
|
|
|
|
expect(headers['referrer-policy']).to.equal('first-text, second-text')
|
|
|
|
|
|
|
|
response.pause()
|
|
|
|
response.on('data', chunk => {})
|
|
|
|
response.on('end', () => { done() })
|
2016-10-11 13:25:26 +00:00
|
|
|
response.resume()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
urlRequest.end()
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2016-10-11 13:25:26 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should be able to pipe a net response into a writable stream', (done) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
const nodeRequestUrl = '/nodeRequestUrl'
|
|
|
|
const netRequestUrl = '/netRequestUrl'
|
|
|
|
const bodyData = randomString(kOneMegaByte)
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-11 13:25:26 +00:00
|
|
|
switch (request.url) {
|
2016-10-12 10:29:25 +00:00
|
|
|
case netRequestUrl:
|
2016-10-11 17:16:23 +00:00
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
2016-10-12 10:29:25 +00:00
|
|
|
response.write(bodyData)
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case nodeRequestUrl:
|
|
|
|
let receivedBodyData = ''
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('data', (chunk) => {
|
2016-10-12 10:29:25 +00:00
|
|
|
receivedBodyData += chunk.toString()
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('end', (chunk) => {
|
2016-10-11 13:25:26 +00:00
|
|
|
if (chunk) {
|
2016-10-12 10:29:25 +00:00
|
|
|
receivedBodyData += chunk.toString()
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(receivedBodyData).to.equal(bodyData)
|
2016-10-11 13:25:26 +00:00
|
|
|
response.end()
|
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
break
|
2016-10-11 13:25:26 +00:00
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-11 13:25:26 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('api-net-spec-done', () => {
|
2016-10-18 08:28:35 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
// Execute below code directly within the browser context without
|
|
|
|
// using the remote module.
|
|
|
|
ipcRenderer.send('eval', `
|
|
|
|
const {net} = require('electron')
|
|
|
|
const http = require('http')
|
2018-09-11 07:56:49 +00:00
|
|
|
const url = require('url')
|
2016-10-18 08:28:35 +00:00
|
|
|
const netRequest = net.request('${server.url}${netRequestUrl}')
|
|
|
|
netRequest.on('response', function (netResponse) {
|
|
|
|
const serverUrl = url.parse('${server.url}')
|
|
|
|
const nodeOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
path: '${nodeRequestUrl}',
|
|
|
|
port: serverUrl.port
|
|
|
|
}
|
|
|
|
let nodeRequest = http.request(nodeOptions)
|
|
|
|
nodeRequest.on('response', function (nodeResponse) {
|
2017-10-27 20:45:58 +00:00
|
|
|
nodeResponse.on('data', (chunk) => {
|
2016-10-18 08:28:35 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
nodeResponse.on('end', (chunk) => {
|
2016-10-18 08:28:35 +00:00
|
|
|
event.sender.send('api-net-spec-done')
|
|
|
|
})
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2016-10-18 08:28:35 +00:00
|
|
|
netResponse.pipe(nodeRequest)
|
2016-10-11 13:25:26 +00:00
|
|
|
})
|
2016-10-18 08:28:35 +00:00
|
|
|
netRequest.end()
|
|
|
|
`)
|
2016-10-10 08:11:22 +00:00
|
|
|
})
|
2016-10-11 17:16:23 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should not emit any event after close', (done) => {
|
2016-10-13 15:14:23 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2018-10-02 01:56:31 +00:00
|
|
|
const bodyData = randomString(kOneKiloByte)
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-13 15:14:23 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.write(bodyData)
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-13 15:14:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
let requestCloseEventEmitted = false
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${server.url}${requestUrl}`
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
const statusCode = response.statusCode
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(statusCode).to.equal(200)
|
2016-10-13 15:14:23 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', () => {
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
|
|
|
response.resume()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('aborted', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('error', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(requestCloseEventEmitted).to.be.false()
|
2016-10-13 15:14:23 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
2016-10-13 15:14:23 +00:00
|
|
|
requestCloseEventEmitted = true
|
|
|
|
// Wait so that all async events get scheduled.
|
2017-10-27 20:45:58 +00:00
|
|
|
setTimeout(() => {
|
2016-10-13 15:14:23 +00:00
|
|
|
done()
|
|
|
|
}, 100)
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
2016-10-11 17:16:23 +00:00
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-17 08:38:10 +00:00
|
|
|
|
2018-06-19 01:45:58 +00:00
|
|
|
describe('Stability and performance', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should free unreferenced, never-started request objects without crash', (done) => {
|
2016-10-17 08:38:10 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('api-net-spec-done', () => {
|
2016-10-17 09:51:20 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-10-17 17:02:25 +00:00
|
|
|
ipcRenderer.send('eval', `
|
|
|
|
const {net} = require('electron')
|
|
|
|
const urlRequest = net.request('${server.url}${requestUrl}')
|
2017-10-27 20:45:58 +00:00
|
|
|
process.nextTick(() => {
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2016-10-25 10:41:01 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
2016-10-18 08:28:35 +00:00
|
|
|
event.sender.send('api-net-spec-done')
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
|
|
|
`)
|
2016-10-17 08:38:10 +00:00
|
|
|
})
|
2017-04-07 23:42:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should not collect on-going requests without crash', (done) => {
|
2016-10-17 17:02:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-17 17:02:25 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.write(randomString(kOneKiloByte))
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('api-net-spec-resume', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
response.write(randomString(kOneKiloByte))
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-17 17:02:25 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('api-net-spec-done', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
// Execute below code directly within the browser context without
|
|
|
|
// using the remote module.
|
|
|
|
ipcRenderer.send('eval', `
|
|
|
|
const {net} = require('electron')
|
|
|
|
const urlRequest = net.request('${server.url}${requestUrl}')
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-18 08:28:35 +00:00
|
|
|
event.sender.send('api-net-spec-done')
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
process.nextTick(() => {
|
2016-10-17 17:02:25 +00:00
|
|
|
// Trigger a garbage collection.
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2016-10-25 10:41:01 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
2016-10-18 08:28:35 +00:00
|
|
|
event.sender.send('api-net-spec-resume')
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
`)
|
2016-10-17 08:38:10 +00:00
|
|
|
})
|
2017-04-07 23:42:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should collect unreferenced, ended requests without crash', (done) => {
|
2016-10-17 17:02:25 +00:00
|
|
|
const requestUrl = '/requestUrl'
|
2017-10-27 20:45:58 +00:00
|
|
|
server.on('request', (request, response) => {
|
2016-10-17 17:02:25 +00:00
|
|
|
switch (request.url) {
|
|
|
|
case requestUrl:
|
|
|
|
response.statusCode = 200
|
|
|
|
response.statusMessage = 'OK'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
default:
|
2017-08-07 20:30:10 +00:00
|
|
|
handleUnexpectedURL(request, response)
|
2016-10-17 17:02:25 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('api-net-spec-done', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('eval', `
|
|
|
|
const {net} = require('electron')
|
|
|
|
const urlRequest = net.request('${server.url}${requestUrl}')
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
|
|
|
response.on('data', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2016-10-17 17:02:25 +00:00
|
|
|
})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2016-10-25 10:41:01 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting()
|
2016-10-17 17:02:25 +00:00
|
|
|
event.sender.send('api-net-spec-done')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end()
|
|
|
|
`)
|
2016-10-17 08:38:10 +00:00
|
|
|
})
|
2016-10-17 09:51:20 +00:00
|
|
|
})
|
2016-10-12 10:29:25 +00:00
|
|
|
})
|
2017-08-07 20:30:10 +00:00
|
|
|
|
|
|
|
function handleUnexpectedURL (request, response) {
|
|
|
|
response.statusCode = '500'
|
|
|
|
response.end()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect.fail(`Unexpected URL: ${request.url}`)
|
2017-08-07 20:30:10 +00:00
|
|
|
}
|