123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629 |
- const assert = require('assert')
- const {remote} = require('electron')
- const {ipcRenderer} = require('electron')
- const http = require('http')
- const url = require('url')
- const {net} = remote
- const {session} = remote
- /* The whole net API doesn't use standard callbacks */
- /* eslint-disable standard/no-callback-literal */
- function randomBuffer (size, start, end) {
- start = start || 0
- end = end || 255
- let 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) {
- let buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
- return buffer.toString()
- }
- const kOneKiloByte = 1024
- const kOneMegaByte = kOneKiloByte * kOneKiloByte
- describe('net module', () => {
- let server
- const connections = new Set()
- beforeEach((done) => {
- server = http.createServer()
- server.listen(0, '127.0.0.1', () => {
- server.url = `http://127.0.0.1:${server.address().port}`
- done()
- })
- server.on('connection', (connection) => {
- connections.add(connection)
- connection.once('close', () => {
- connections.delete(connection)
- })
- })
- })
- afterEach((done) => {
- for (const connection of connections) {
- connection.destroy()
- }
- server.close(() => {
- server = null
- done()
- })
- })
- describe('HTTP basics', () => {
- it('should be able to issue a basic GET request', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.method, 'GET')
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request(`${server.url}${requestUrl}`)
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should be able to issue a basic POST request', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.method, 'POST')
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'POST',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should fetch correct data in a GET request', (done) => {
- const requestUrl = '/requestUrl'
- const bodyData = 'Hello World!'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.method, 'GET')
- response.write(bodyData)
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request(`${server.url}${requestUrl}`)
- urlRequest.on('response', (response) => {
- let expectedBodyData = ''
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- expectedBodyData += chunk.toString()
- })
- response.on('end', () => {
- assert.equal(expectedBodyData, bodyData)
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should post the correct data in a POST request', (done) => {
- const requestUrl = '/requestUrl'
- const bodyData = 'Hello World!'
- server.on('request', (request, response) => {
- let postedBodyData = ''
- switch (request.url) {
- case requestUrl:
- assert.equal(request.method, 'POST')
- request.on('data', (chunk) => {
- postedBodyData += chunk.toString()
- })
- request.on('end', () => {
- assert.equal(postedBodyData, bodyData)
- response.end()
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'POST',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {})
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.write(bodyData)
- urlRequest.end()
- })
- it('should support chunked encoding', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.chunkedEncoding = true
- assert.equal(request.method, 'POST')
- assert.equal(request.headers['transfer-encoding'], 'chunked')
- assert(!request.headers['content-length'])
- request.on('data', (chunk) => {
- response.write(chunk)
- })
- request.on('end', (chunk) => {
- response.end(chunk)
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'POST',
- url: `${server.url}${requestUrl}`
- })
- let chunkIndex = 0
- const chunkCount = 100
- let sentChunks = []
- let receivedChunks = []
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- receivedChunks.push(chunk)
- })
- response.on('end', () => {
- let sentData = Buffer.concat(sentChunks)
- let receivedData = Buffer.concat(receivedChunks)
- assert.equal(sentData.toString(), receivedData.toString())
- assert.equal(chunkIndex, chunkCount)
- done()
- })
- response.resume()
- })
- urlRequest.chunkedEncoding = true
- while (chunkIndex < chunkCount) {
- chunkIndex += 1
- const chunk = randomBuffer(kOneKiloByte)
- sentChunks.push(chunk)
- assert(urlRequest.write(chunk))
- }
- urlRequest.end()
- })
- })
- describe('ClientRequest API', () => {
- afterEach(() => {
- session.defaultSession.webRequest.onBeforeRequest(null)
- })
- it('request/response objects should emit expected events', (done) => {
- const requestUrl = '/requestUrl'
- const bodyData = randomString(kOneMegaByte)
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.write(bodyData)
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestResponseEventEmitted = false
- let requestFinishEventEmitted = false
- let requestCloseEventEmitted = false
- let responseDataEventEmitted = false
- let responseEndEventEmitted = false
- function maybeDone (done) {
- if (!requestCloseEventEmitted || !responseEndEventEmitted) {
- return
- }
- assert(requestResponseEventEmitted)
- assert(requestFinishEventEmitted)
- assert(requestCloseEventEmitted)
- assert(responseDataEventEmitted)
- assert(responseEndEventEmitted)
- done()
- }
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- requestResponseEventEmitted = true
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- let buffers = []
- response.pause()
- response.on('data', (chunk) => {
- buffers.push(chunk)
- responseDataEventEmitted = true
- })
- response.on('end', () => {
- let receivedBodyData = Buffer.concat(buffers)
- assert(receivedBodyData.toString() === bodyData)
- responseEndEventEmitted = true
- maybeDone(done)
- })
- response.resume()
- response.on('error', (error) => {
- assert.ifError(error)
- })
- response.on('aborted', () => {
- assert.fail('response aborted')
- })
- })
- urlRequest.on('finish', () => {
- requestFinishEventEmitted = true
- })
- urlRequest.on('error', (error) => {
- assert.ifError(error)
- })
- urlRequest.on('abort', () => {
- assert.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 requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.headers[customHeaderName.toLowerCase()],
- customHeaderValue)
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.setHeader(customHeaderName, customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
- customHeaderValue)
- urlRequest.write('')
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
- customHeaderValue)
- urlRequest.end()
- })
- it('should be able to set a non-string object as a header value', (done) => {
- const requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Integer-Value'
- const customHeaderValue = 900
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.headers[customHeaderName.toLowerCase()],
- customHeaderValue.toString())
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- assert.equal(request.url, requestUrl)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.setHeader(customHeaderName, customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
- customHeaderValue)
- urlRequest.write('')
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
- customHeaderValue)
- urlRequest.end()
- })
- it('should not be able to set a custom HTTP request header after first write', (done) => {
- const requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert(!request.headers[customHeaderName.toLowerCase()])
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.write('')
- assert.throws(() => {
- urlRequest.setHeader(customHeaderName, customHeaderValue)
- })
- assert(!urlRequest.getHeader(customHeaderName))
- urlRequest.end()
- })
- it('should be able to remove a custom HTTP request header before first write', (done) => {
- const requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert(!request.headers[customHeaderName.toLowerCase()])
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.setHeader(customHeaderName, customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- urlRequest.removeHeader(customHeaderName)
- assert(!urlRequest.getHeader(customHeaderName))
- urlRequest.write('')
- urlRequest.end()
- })
- it('should not be able to remove a custom HTTP request header after first write', (done) => {
- const requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.headers[customHeaderName.toLowerCase()],
- customHeaderValue)
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.setHeader(customHeaderName, customHeaderValue)
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- urlRequest.write('')
- assert.throws(() => {
- urlRequest.removeHeader(customHeaderName)
- })
- assert.equal(urlRequest.getHeader(customHeaderName),
- customHeaderValue)
- urlRequest.end()
- })
- it('should be able to set cookie header line', (done) => {
- const requestUrl = '/requestUrl'
- const cookieHeaderName = 'Cookie'
- const cookieHeaderValue = 'test=12345'
- const customSession = session.fromPartition('test-cookie-header')
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.headers[cookieHeaderName.toLowerCase()],
- cookieHeaderValue)
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- customSession.cookies.set({
- url: `${server.url}`,
- name: 'test',
- value: '11111'
- }, (error) => {
- if (error) return done(error)
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`,
- session: customSession
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {})
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.setHeader(cookieHeaderName, cookieHeaderValue)
- assert.equal(urlRequest.getHeader(cookieHeaderName),
- cookieHeaderValue)
- urlRequest.end()
- })
- })
- it('should be able to abort an HTTP request before first write', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- response.end()
- assert.fail('Unexpected request event')
- })
- let requestAbortEventEmitted = false
- let requestCloseEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.fail('Unexpected response event')
- })
- urlRequest.on('finish', () => {
- assert.fail('Unexpected finish event')
- })
- urlRequest.on('error', () => {
- assert.fail('Unexpected error event')
- })
- urlRequest.on('abort', () => {
- requestAbortEventEmitted = true
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- assert(requestAbortEventEmitted)
- assert(requestCloseEventEmitted)
- done()
- })
- urlRequest.abort()
- assert(!urlRequest.write(''))
- urlRequest.end()
- })
- it('it should be able to abort an HTTP request before request end', (done) => {
- const requestUrl = '/requestUrl'
- let requestReceivedByServer = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- requestReceivedByServer = true
- cancelRequest()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestAbortEventEmitted = false
- let requestCloseEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.fail('Unexpected response event')
- })
- urlRequest.on('finish', () => {
- assert.fail('Unexpected finish event')
- })
- urlRequest.on('error', () => {
- assert.fail('Unexpected error event')
- })
- urlRequest.on('abort', () => {
- requestAbortEventEmitted = true
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- assert(requestReceivedByServer)
- assert(requestAbortEventEmitted)
- assert(requestCloseEventEmitted)
- done()
- })
- urlRequest.chunkedEncoding = true
- urlRequest.write(randomString(kOneKiloByte))
- function cancelRequest () {
- urlRequest.abort()
- }
- })
- it('it should be able to abort an HTTP request after request end and before response', (done) => {
- const requestUrl = '/requestUrl'
- let requestReceivedByServer = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- requestReceivedByServer = true
- cancelRequest()
- process.nextTick(() => {
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestAbortEventEmitted = false
- let requestFinishEventEmitted = false
- let requestCloseEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.fail('Unexpected response event')
- })
- urlRequest.on('finish', () => {
- requestFinishEventEmitted = true
- })
- urlRequest.on('error', () => {
- assert.fail('Unexpected error event')
- })
- urlRequest.on('abort', () => {
- requestAbortEventEmitted = true
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- assert(requestFinishEventEmitted)
- assert(requestReceivedByServer)
- assert(requestAbortEventEmitted)
- assert(requestCloseEventEmitted)
- done()
- })
- urlRequest.end(randomString(kOneKiloByte))
- function cancelRequest () {
- urlRequest.abort()
- }
- })
- it('it should be able to abort an HTTP request after response start', (done) => {
- const requestUrl = '/requestUrl'
- let requestReceivedByServer = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- requestReceivedByServer = true
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.write(randomString(kOneKiloByte))
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestFinishEventEmitted = false
- let requestResponseEventEmitted = false
- let requestAbortEventEmitted = false
- let requestCloseEventEmitted = false
- let responseAbortedEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- requestResponseEventEmitted = true
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- assert.fail('Unexpected end event')
- })
- response.resume()
- response.on('error', () => {
- assert.fail('Unexpected error event')
- })
- response.on('aborted', () => {
- responseAbortedEventEmitted = true
- })
- urlRequest.abort()
- })
- urlRequest.on('finish', () => {
- requestFinishEventEmitted = true
- })
- urlRequest.on('error', () => {
- assert.fail('Unexpected error event')
- })
- urlRequest.on('abort', () => {
- requestAbortEventEmitted = true
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- assert(requestFinishEventEmitted, 'request should emit "finish" event')
- assert(requestReceivedByServer, 'request should be received by the server')
- assert(requestResponseEventEmitted, '"response" event should be emitted')
- assert(requestAbortEventEmitted, 'request should emit "abort" event')
- assert(responseAbortedEventEmitted, 'response should emit "aborted" event')
- assert(requestCloseEventEmitted, 'request should emit "close" event')
- done()
- })
- urlRequest.end(randomString(kOneKiloByte))
- })
- it('abort event should be emitted at most once', (done) => {
- const requestUrl = '/requestUrl'
- let requestReceivedByServer = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- requestReceivedByServer = true
- cancelRequest()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestFinishEventEmitted = false
- let requestAbortEventCount = 0
- let requestCloseEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', () => {
- assert.fail('Unexpected response event')
- })
- urlRequest.on('finish', () => {
- requestFinishEventEmitted = true
- })
- urlRequest.on('error', () => {
- assert.fail('Unexpected error event')
- })
- urlRequest.on('abort', () => {
- ++requestAbortEventCount
- urlRequest.abort()
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- // Let all pending async events to be emitted
- setTimeout(() => {
- assert(requestFinishEventEmitted)
- assert(requestReceivedByServer)
- assert.equal(requestAbortEventCount, 1)
- assert(requestCloseEventEmitted)
- done()
- }, 500)
- })
- urlRequest.end(randomString(kOneKiloByte))
- function cancelRequest () {
- urlRequest.abort()
- urlRequest.abort()
- }
- })
- it('Requests should be intercepted by webRequest module', (done) => {
- const requestUrl = '/requestUrl'
- const redirectUrl = '/redirectUrl'
- let requestIsRedirected = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case redirectUrl:
- requestIsRedirected = true
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestIsIntercepted = false
- session.defaultSession.webRequest.onBeforeRequest(
- (details, callback) => {
- if (details.url === `${server.url}${requestUrl}`) {
- requestIsIntercepted = true
- // Disabled due to false positive in StandardJS
- // eslint-disable-next-line standard/no-callback-literal
- callback({
- redirectURL: `${server.url}${redirectUrl}`
- })
- } else {
- callback({
- cancel: false
- })
- }
- })
- const urlRequest = net.request(`${server.url}${requestUrl}`)
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- assert(requestIsRedirected, 'The server should receive a request to the forward URL')
- assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
- done()
- })
- response.resume()
- })
- 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
- server.on('request', (request, response) => {
- switch (request.url) {
- case redirectUrl:
- requestIsRedirected = true
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
- assert.fail('Request should not be intercepted by the default session')
- })
- let customSession = session.fromPartition(customPartitionName, {cache: false})
- let requestIsIntercepted = false
- customSession.webRequest.onBeforeRequest((details, callback) => {
- if (details.url === `${server.url}${requestUrl}`) {
- requestIsIntercepted = true
- // Disabled due to false positive in StandardJS
- // eslint-disable-next-line standard/no-callback-literal
- callback({
- redirectURL: `${server.url}${redirectUrl}`
- })
- } else {
- callback({
- cancel: false
- })
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- session: customSession
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- assert(requestIsRedirected, 'The server should receive a request to the forward URL')
- assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should throw if given an invalid redirect mode', () => {
- const requestUrl = '/requestUrl'
- const options = {
- url: `${server.url}${requestUrl}`,
- redirect: 'custom'
- }
- assert.throws(() => {
- net.request(options)
- }, 'redirect mode should be one of follow, error or manual')
- })
- it('should throw when calling getHeader without a name', () => {
- assert.throws(() => {
- net.request({url: `${server.url}/requestUrl`}).getHeader()
- }, /`name` is required for getHeader\(name\)\./)
- assert.throws(() => {
- net.request({url: `${server.url}/requestUrl`}).getHeader(null)
- }, /`name` is required for getHeader\(name\)\./)
- })
- it('should throw when calling removeHeader without a name', () => {
- assert.throws(() => {
- net.request({url: `${server.url}/requestUrl`}).removeHeader()
- }, /`name` is required for removeHeader\(name\)\./)
- assert.throws(() => {
- net.request({url: `${server.url}/requestUrl`}).removeHeader(null)
- }, /`name` is required for removeHeader\(name\)\./)
- })
- it('should follow redirect when no redirect mode is provided', (done) => {
- const requestUrl = '/301'
- server.on('request', (request, response) => {
- switch (request.url) {
- case '/301':
- response.statusCode = '301'
- response.setHeader('Location', '/200')
- response.end()
- break
- case '/200':
- response.statusCode = '200'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- done()
- })
- urlRequest.end()
- })
- it('should follow redirect chain when no redirect mode is provided', (done) => {
- const requestUrl = '/redirectChain'
- server.on('request', (request, response) => {
- 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:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- done()
- })
- urlRequest.end()
- })
- it('should not follow redirect when mode is error', (done) => {
- const requestUrl = '/301'
- server.on('request', (request, response) => {
- switch (request.url) {
- case '/301':
- response.statusCode = '301'
- response.setHeader('Location', '/200')
- response.end()
- break
- case '/200':
- response.statusCode = '200'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- redirect: 'error'
- })
- urlRequest.on('error', (error) => {
- assert.equal(error.message, 'Request cannot follow redirect with the current redirect mode')
- })
- urlRequest.on('close', () => {
- done()
- })
- urlRequest.end()
- })
- it('should allow follow redirect when mode is manual', (done) => {
- const requestUrl = '/redirectChain'
- let redirectCount = 0
- server.on('request', (request, response) => {
- 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:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- redirect: 'manual'
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- assert.equal(redirectCount, 2)
- done()
- })
- urlRequest.on('redirect', (status, method, url) => {
- if (url === `${server.url}/301` || url === `${server.url}/200`) {
- redirectCount += 1
- urlRequest.followRedirect()
- }
- })
- urlRequest.end()
- })
- it('should allow cancelling redirect when mode is manual', (done) => {
- const requestUrl = '/redirectChain'
- let redirectCount = 0
- server.on('request', (request, response) => {
- 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:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- redirect: 'manual'
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- urlRequest.abort()
- })
- response.resume()
- })
- urlRequest.on('close', () => {
- assert.equal(redirectCount, 1)
- done()
- })
- urlRequest.on('redirect', (status, method, url) => {
- if (url === `${server.url}/redirect/1`) {
- redirectCount += 1
- urlRequest.followRedirect()
- }
- })
- urlRequest.end()
- })
- it('should throw if given an invalid session option', (done) => {
- const requestUrl = '/requestUrl'
- try {
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- session: 1
- })
- // eslint-disable-next-line
- urlRequest
- } catch (exception) {
- done()
- }
- })
- it('should to able to create and intercept a request using a custom partition name', (done) => {
- const requestUrl = '/requestUrl'
- const redirectUrl = '/redirectUrl'
- const customPartitionName = 'custom-partition'
- let requestIsRedirected = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case redirectUrl:
- requestIsRedirected = true
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
- assert.fail('Request should not be intercepted by the default session')
- })
- let customSession = session.fromPartition(customPartitionName, {
- cache: false
- })
- let requestIsIntercepted = false
- customSession.webRequest.onBeforeRequest((details, callback) => {
- if (details.url === `${server.url}${requestUrl}`) {
- requestIsIntercepted = true
- callback({
- redirectURL: `${server.url}${redirectUrl}`
- })
- } else {
- callback({
- cancel: false
- })
- }
- })
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- partition: customPartitionName
- })
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- assert(requestIsRedirected, 'The server should receive a request to the forward URL')
- assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should throw if given an invalid partition option', (done) => {
- const requestUrl = '/requestUrl'
- try {
- const urlRequest = net.request({
- url: `${server.url}${requestUrl}`,
- partition: 1
- })
- // eslint-disable-next-line
- urlRequest
- } catch (exception) {
- done()
- }
- })
- it('should be able to create a request with options', (done) => {
- const requestUrl = '/'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- assert.equal(request.method, 'GET')
- assert.equal(request.headers[customHeaderName.toLowerCase()],
- customHeaderValue)
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const serverUrl = url.parse(server.url)
- let options = {
- port: serverUrl.port,
- hostname: '127.0.0.1',
- headers: {}
- }
- options.headers[customHeaderName] = customHeaderValue
- const urlRequest = net.request(options)
- urlRequest.on('response', (response) => {
- assert.equal(response.statusCode, 200)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should be able to pipe a readable stream into a net request', (done) => {
- const nodeRequestUrl = '/nodeRequestUrl'
- const netRequestUrl = '/netRequestUrl'
- const bodyData = randomString(kOneMegaByte)
- let netRequestReceived = false
- let netRequestEnded = false
- server.on('request', (request, response) => {
- switch (request.url) {
- case nodeRequestUrl:
- response.write(bodyData)
- response.end()
- break
- case netRequestUrl:
- netRequestReceived = true
- let receivedBodyData = ''
- request.on('data', (chunk) => {
- receivedBodyData += chunk.toString()
- })
- request.on('end', (chunk) => {
- netRequestEnded = true
- if (chunk) {
- receivedBodyData += chunk.toString()
- }
- assert.equal(receivedBodyData, bodyData)
- response.end()
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let nodeRequest = http.request(`${server.url}${nodeRequestUrl}`)
- nodeRequest.on('response', (nodeResponse) => {
- const netRequest = net.request(`${server.url}${netRequestUrl}`)
- netRequest.on('response', (netResponse) => {
- assert.equal(netResponse.statusCode, 200)
- netResponse.pause()
- netResponse.on('data', (chunk) => {})
- netResponse.on('end', () => {
- assert(netRequestReceived)
- assert(netRequestEnded)
- done()
- })
- netResponse.resume()
- })
- nodeResponse.pipe(netRequest)
- })
- nodeRequest.end()
- })
- it('should emit error event on server socket close', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- request.socket.destroy()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestErrorEventEmitted = false
- const urlRequest = net.request(`${server.url}${requestUrl}`)
- urlRequest.on('error', (error) => {
- assert(error)
- requestErrorEventEmitted = true
- })
- urlRequest.on('close', () => {
- assert(requestErrorEventEmitted)
- done()
- })
- urlRequest.end()
- })
- })
- describe('IncomingMessage API', () => {
- it('response object should implement the IncomingMessage API', (done) => {
- const requestUrl = '/requestUrl'
- const customHeaderName = 'Some-Custom-Header-Name'
- const customHeaderValue = 'Some-Customer-Header-Value'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.setHeader(customHeaderName, customHeaderValue)
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- const statusCode = response.statusCode
- assert(typeof statusCode === 'number')
- assert.equal(statusCode, 200)
- const statusMessage = response.statusMessage
- assert(typeof statusMessage === 'string')
- assert.equal(statusMessage, 'OK')
- const headers = response.headers
- assert(typeof headers === 'object')
- assert.deepEqual(headers[customHeaderName.toLowerCase()],
- [customHeaderValue])
- const httpVersion = response.httpVersion
- assert(typeof httpVersion === 'string')
- assert(httpVersion.length > 0)
- const httpVersionMajor = response.httpVersionMajor
- assert(typeof httpVersionMajor === 'number')
- assert(httpVersionMajor >= 1)
- const httpVersionMinor = response.httpVersionMinor
- assert(typeof httpVersionMinor === 'number')
- assert(httpVersionMinor >= 0)
- response.pause()
- response.on('data', (chunk) => {
- })
- response.on('end', () => {
- done()
- })
- response.resume()
- })
- urlRequest.end()
- })
- it('should be able to pipe a net response into a writable stream', (done) => {
- const nodeRequestUrl = '/nodeRequestUrl'
- const netRequestUrl = '/netRequestUrl'
- const bodyData = randomString(kOneMegaByte)
- server.on('request', (request, response) => {
- switch (request.url) {
- case netRequestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.write(bodyData)
- response.end()
- break
- case nodeRequestUrl:
- let receivedBodyData = ''
- request.on('data', (chunk) => {
- receivedBodyData += chunk.toString()
- })
- request.on('end', (chunk) => {
- if (chunk) {
- receivedBodyData += chunk.toString()
- }
- assert.equal(receivedBodyData, bodyData)
- response.end()
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- ipcRenderer.once('api-net-spec-done', () => {
- 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')
- 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) {
- nodeResponse.on('data', (chunk) => {
- })
- nodeResponse.on('end', (chunk) => {
- event.sender.send('api-net-spec-done')
- })
- })
- netResponse.pipe(nodeRequest)
- })
- netRequest.end()
- `)
- })
- it('should not emit any event after close', (done) => {
- const requestUrl = '/requestUrl'
- let bodyData = randomString(kOneKiloByte)
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.write(bodyData)
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- let requestCloseEventEmitted = false
- const urlRequest = net.request({
- method: 'GET',
- url: `${server.url}${requestUrl}`
- })
- urlRequest.on('response', (response) => {
- assert(!requestCloseEventEmitted)
- const statusCode = response.statusCode
- assert.equal(statusCode, 200)
- response.pause()
- response.on('data', () => {
- })
- response.on('end', () => {
- })
- response.resume()
- response.on('error', () => {
- assert(!requestCloseEventEmitted)
- })
- response.on('aborted', () => {
- assert(!requestCloseEventEmitted)
- })
- })
- urlRequest.on('finish', () => {
- assert(!requestCloseEventEmitted)
- })
- urlRequest.on('error', () => {
- assert(!requestCloseEventEmitted)
- })
- urlRequest.on('abort', () => {
- assert(!requestCloseEventEmitted)
- })
- urlRequest.on('close', () => {
- requestCloseEventEmitted = true
- // Wait so that all async events get scheduled.
- setTimeout(() => {
- done()
- }, 100)
- })
- urlRequest.end()
- })
- })
- describe('Stability and performance', (done) => {
- it('should free unreferenced, never-started request objects without crash', (done) => {
- const requestUrl = '/requestUrl'
- ipcRenderer.once('api-net-spec-done', () => {
- done()
- })
- ipcRenderer.send('eval', `
- const {net} = require('electron')
- const urlRequest = net.request('${server.url}${requestUrl}')
- process.nextTick(() => {
- const v8Util = process.atomBinding('v8_util')
- v8Util.requestGarbageCollectionForTesting()
- event.sender.send('api-net-spec-done')
- })
- `)
- })
- it('should not collect on-going requests without crash', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.write(randomString(kOneKiloByte))
- ipcRenderer.once('api-net-spec-resume', () => {
- response.write(randomString(kOneKiloByte))
- response.end()
- })
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- ipcRenderer.once('api-net-spec-done', () => {
- 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}')
- urlRequest.on('response', (response) => {
- response.on('data', () => {
- })
- response.on('end', () => {
- event.sender.send('api-net-spec-done')
- })
- process.nextTick(() => {
- // Trigger a garbage collection.
- const v8Util = process.atomBinding('v8_util')
- v8Util.requestGarbageCollectionForTesting()
- event.sender.send('api-net-spec-resume')
- })
- })
- urlRequest.end()
- `)
- })
- it('should collect unreferenced, ended requests without crash', (done) => {
- const requestUrl = '/requestUrl'
- server.on('request', (request, response) => {
- switch (request.url) {
- case requestUrl:
- response.statusCode = 200
- response.statusMessage = 'OK'
- response.end()
- break
- default:
- handleUnexpectedURL(request, response)
- }
- })
- ipcRenderer.once('api-net-spec-done', () => {
- done()
- })
- ipcRenderer.send('eval', `
- const {net} = require('electron')
- const urlRequest = net.request('${server.url}${requestUrl}')
- urlRequest.on('response', (response) => {
- response.on('data', () => {
- })
- response.on('end', () => {
- })
- })
- urlRequest.on('close', () => {
- process.nextTick(() => {
- const v8Util = process.atomBinding('v8_util')
- v8Util.requestGarbageCollectionForTesting()
- event.sender.send('api-net-spec-done')
- })
- })
- urlRequest.end()
- `)
- })
- })
- })
- function handleUnexpectedURL (request, response) {
- response.statusCode = '500'
- response.end()
- assert.fail(`Unexpected URL: ${request.url}`)
- }
|