api-net-spec.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. const assert = require('assert')
  2. const {remote} = require('electron')
  3. const {ipcRenderer} = require('electron')
  4. const http = require('http')
  5. const url = require('url')
  6. const {net} = remote
  7. const {session} = remote
  8. /* The whole net API doesn't use standard callbacks */
  9. /* eslint-disable standard/no-callback-literal */
  10. function randomBuffer (size, start, end) {
  11. start = start || 0
  12. end = end || 255
  13. let range = 1 + end - start
  14. const buffer = Buffer.allocUnsafe(size)
  15. for (let i = 0; i < size; ++i) {
  16. buffer[i] = start + Math.floor(Math.random() * range)
  17. }
  18. return buffer
  19. }
  20. function randomString (length) {
  21. let buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
  22. return buffer.toString()
  23. }
  24. const kOneKiloByte = 1024
  25. const kOneMegaByte = kOneKiloByte * kOneKiloByte
  26. describe('net module', () => {
  27. let server
  28. const connections = new Set()
  29. beforeEach((done) => {
  30. server = http.createServer()
  31. server.listen(0, '127.0.0.1', () => {
  32. server.url = `http://127.0.0.1:${server.address().port}`
  33. done()
  34. })
  35. server.on('connection', (connection) => {
  36. connections.add(connection)
  37. connection.once('close', () => {
  38. connections.delete(connection)
  39. })
  40. })
  41. })
  42. afterEach((done) => {
  43. for (const connection of connections) {
  44. connection.destroy()
  45. }
  46. server.close(() => {
  47. server = null
  48. done()
  49. })
  50. })
  51. describe('HTTP basics', () => {
  52. it('should be able to issue a basic GET request', (done) => {
  53. const requestUrl = '/requestUrl'
  54. server.on('request', (request, response) => {
  55. switch (request.url) {
  56. case requestUrl:
  57. assert.equal(request.method, 'GET')
  58. response.end()
  59. break
  60. default:
  61. handleUnexpectedURL(request, response)
  62. }
  63. })
  64. const urlRequest = net.request(`${server.url}${requestUrl}`)
  65. urlRequest.on('response', (response) => {
  66. assert.equal(response.statusCode, 200)
  67. response.pause()
  68. response.on('data', (chunk) => {
  69. })
  70. response.on('end', () => {
  71. done()
  72. })
  73. response.resume()
  74. })
  75. urlRequest.end()
  76. })
  77. it('should be able to issue a basic POST request', (done) => {
  78. const requestUrl = '/requestUrl'
  79. server.on('request', (request, response) => {
  80. switch (request.url) {
  81. case requestUrl:
  82. assert.equal(request.method, 'POST')
  83. response.end()
  84. break
  85. default:
  86. handleUnexpectedURL(request, response)
  87. }
  88. })
  89. const urlRequest = net.request({
  90. method: 'POST',
  91. url: `${server.url}${requestUrl}`
  92. })
  93. urlRequest.on('response', (response) => {
  94. assert.equal(response.statusCode, 200)
  95. response.pause()
  96. response.on('data', (chunk) => {
  97. })
  98. response.on('end', () => {
  99. done()
  100. })
  101. response.resume()
  102. })
  103. urlRequest.end()
  104. })
  105. it('should fetch correct data in a GET request', (done) => {
  106. const requestUrl = '/requestUrl'
  107. const bodyData = 'Hello World!'
  108. server.on('request', (request, response) => {
  109. switch (request.url) {
  110. case requestUrl:
  111. assert.equal(request.method, 'GET')
  112. response.write(bodyData)
  113. response.end()
  114. break
  115. default:
  116. handleUnexpectedURL(request, response)
  117. }
  118. })
  119. const urlRequest = net.request(`${server.url}${requestUrl}`)
  120. urlRequest.on('response', (response) => {
  121. let expectedBodyData = ''
  122. assert.equal(response.statusCode, 200)
  123. response.pause()
  124. response.on('data', (chunk) => {
  125. expectedBodyData += chunk.toString()
  126. })
  127. response.on('end', () => {
  128. assert.equal(expectedBodyData, bodyData)
  129. done()
  130. })
  131. response.resume()
  132. })
  133. urlRequest.end()
  134. })
  135. it('should post the correct data in a POST request', (done) => {
  136. const requestUrl = '/requestUrl'
  137. const bodyData = 'Hello World!'
  138. server.on('request', (request, response) => {
  139. let postedBodyData = ''
  140. switch (request.url) {
  141. case requestUrl:
  142. assert.equal(request.method, 'POST')
  143. request.on('data', (chunk) => {
  144. postedBodyData += chunk.toString()
  145. })
  146. request.on('end', () => {
  147. assert.equal(postedBodyData, bodyData)
  148. response.end()
  149. })
  150. break
  151. default:
  152. handleUnexpectedURL(request, response)
  153. }
  154. })
  155. const urlRequest = net.request({
  156. method: 'POST',
  157. url: `${server.url}${requestUrl}`
  158. })
  159. urlRequest.on('response', (response) => {
  160. assert.equal(response.statusCode, 200)
  161. response.pause()
  162. response.on('data', (chunk) => {})
  163. response.on('end', () => {
  164. done()
  165. })
  166. response.resume()
  167. })
  168. urlRequest.write(bodyData)
  169. urlRequest.end()
  170. })
  171. it('should support chunked encoding', (done) => {
  172. const requestUrl = '/requestUrl'
  173. server.on('request', (request, response) => {
  174. switch (request.url) {
  175. case requestUrl:
  176. response.statusCode = 200
  177. response.statusMessage = 'OK'
  178. response.chunkedEncoding = true
  179. assert.equal(request.method, 'POST')
  180. assert.equal(request.headers['transfer-encoding'], 'chunked')
  181. assert(!request.headers['content-length'])
  182. request.on('data', (chunk) => {
  183. response.write(chunk)
  184. })
  185. request.on('end', (chunk) => {
  186. response.end(chunk)
  187. })
  188. break
  189. default:
  190. handleUnexpectedURL(request, response)
  191. }
  192. })
  193. const urlRequest = net.request({
  194. method: 'POST',
  195. url: `${server.url}${requestUrl}`
  196. })
  197. let chunkIndex = 0
  198. const chunkCount = 100
  199. let sentChunks = []
  200. let receivedChunks = []
  201. urlRequest.on('response', (response) => {
  202. assert.equal(response.statusCode, 200)
  203. response.pause()
  204. response.on('data', (chunk) => {
  205. receivedChunks.push(chunk)
  206. })
  207. response.on('end', () => {
  208. let sentData = Buffer.concat(sentChunks)
  209. let receivedData = Buffer.concat(receivedChunks)
  210. assert.equal(sentData.toString(), receivedData.toString())
  211. assert.equal(chunkIndex, chunkCount)
  212. done()
  213. })
  214. response.resume()
  215. })
  216. urlRequest.chunkedEncoding = true
  217. while (chunkIndex < chunkCount) {
  218. chunkIndex += 1
  219. const chunk = randomBuffer(kOneKiloByte)
  220. sentChunks.push(chunk)
  221. assert(urlRequest.write(chunk))
  222. }
  223. urlRequest.end()
  224. })
  225. })
  226. describe('ClientRequest API', () => {
  227. afterEach(() => {
  228. session.defaultSession.webRequest.onBeforeRequest(null)
  229. })
  230. it('request/response objects should emit expected events', (done) => {
  231. const requestUrl = '/requestUrl'
  232. const bodyData = randomString(kOneMegaByte)
  233. server.on('request', (request, response) => {
  234. switch (request.url) {
  235. case requestUrl:
  236. response.statusCode = 200
  237. response.statusMessage = 'OK'
  238. response.write(bodyData)
  239. response.end()
  240. break
  241. default:
  242. handleUnexpectedURL(request, response)
  243. }
  244. })
  245. let requestResponseEventEmitted = false
  246. let requestFinishEventEmitted = false
  247. let requestCloseEventEmitted = false
  248. let responseDataEventEmitted = false
  249. let responseEndEventEmitted = false
  250. function maybeDone (done) {
  251. if (!requestCloseEventEmitted || !responseEndEventEmitted) {
  252. return
  253. }
  254. assert(requestResponseEventEmitted)
  255. assert(requestFinishEventEmitted)
  256. assert(requestCloseEventEmitted)
  257. assert(responseDataEventEmitted)
  258. assert(responseEndEventEmitted)
  259. done()
  260. }
  261. const urlRequest = net.request({
  262. method: 'GET',
  263. url: `${server.url}${requestUrl}`
  264. })
  265. urlRequest.on('response', (response) => {
  266. requestResponseEventEmitted = true
  267. const statusCode = response.statusCode
  268. assert.equal(statusCode, 200)
  269. let buffers = []
  270. response.pause()
  271. response.on('data', (chunk) => {
  272. buffers.push(chunk)
  273. responseDataEventEmitted = true
  274. })
  275. response.on('end', () => {
  276. let receivedBodyData = Buffer.concat(buffers)
  277. assert(receivedBodyData.toString() === bodyData)
  278. responseEndEventEmitted = true
  279. maybeDone(done)
  280. })
  281. response.resume()
  282. response.on('error', (error) => {
  283. assert.ifError(error)
  284. })
  285. response.on('aborted', () => {
  286. assert.fail('response aborted')
  287. })
  288. })
  289. urlRequest.on('finish', () => {
  290. requestFinishEventEmitted = true
  291. })
  292. urlRequest.on('error', (error) => {
  293. assert.ifError(error)
  294. })
  295. urlRequest.on('abort', () => {
  296. assert.fail('request aborted')
  297. })
  298. urlRequest.on('close', () => {
  299. requestCloseEventEmitted = true
  300. maybeDone(done)
  301. })
  302. urlRequest.end()
  303. })
  304. it('should be able to set a custom HTTP request header before first write', (done) => {
  305. const requestUrl = '/requestUrl'
  306. const customHeaderName = 'Some-Custom-Header-Name'
  307. const customHeaderValue = 'Some-Customer-Header-Value'
  308. server.on('request', (request, response) => {
  309. switch (request.url) {
  310. case requestUrl:
  311. assert.equal(request.headers[customHeaderName.toLowerCase()],
  312. customHeaderValue)
  313. response.statusCode = 200
  314. response.statusMessage = 'OK'
  315. response.end()
  316. break
  317. default:
  318. handleUnexpectedURL(request, response)
  319. }
  320. })
  321. const urlRequest = net.request({
  322. method: 'GET',
  323. url: `${server.url}${requestUrl}`
  324. })
  325. urlRequest.on('response', (response) => {
  326. const statusCode = response.statusCode
  327. assert.equal(statusCode, 200)
  328. response.pause()
  329. response.on('data', (chunk) => {
  330. })
  331. response.on('end', () => {
  332. done()
  333. })
  334. response.resume()
  335. })
  336. urlRequest.setHeader(customHeaderName, customHeaderValue)
  337. assert.equal(urlRequest.getHeader(customHeaderName),
  338. customHeaderValue)
  339. assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
  340. customHeaderValue)
  341. urlRequest.write('')
  342. assert.equal(urlRequest.getHeader(customHeaderName),
  343. customHeaderValue)
  344. assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
  345. customHeaderValue)
  346. urlRequest.end()
  347. })
  348. it('should be able to set a non-string object as a header value', (done) => {
  349. const requestUrl = '/requestUrl'
  350. const customHeaderName = 'Some-Integer-Value'
  351. const customHeaderValue = 900
  352. server.on('request', (request, response) => {
  353. switch (request.url) {
  354. case requestUrl:
  355. assert.equal(request.headers[customHeaderName.toLowerCase()],
  356. customHeaderValue.toString())
  357. response.statusCode = 200
  358. response.statusMessage = 'OK'
  359. response.end()
  360. break
  361. default:
  362. assert.equal(request.url, requestUrl)
  363. }
  364. })
  365. const urlRequest = net.request({
  366. method: 'GET',
  367. url: `${server.url}${requestUrl}`
  368. })
  369. urlRequest.on('response', (response) => {
  370. const statusCode = response.statusCode
  371. assert.equal(statusCode, 200)
  372. response.pause()
  373. response.on('end', () => {
  374. done()
  375. })
  376. response.resume()
  377. })
  378. urlRequest.setHeader(customHeaderName, customHeaderValue)
  379. assert.equal(urlRequest.getHeader(customHeaderName),
  380. customHeaderValue)
  381. assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
  382. customHeaderValue)
  383. urlRequest.write('')
  384. assert.equal(urlRequest.getHeader(customHeaderName),
  385. customHeaderValue)
  386. assert.equal(urlRequest.getHeader(customHeaderName.toLowerCase()),
  387. customHeaderValue)
  388. urlRequest.end()
  389. })
  390. it('should not be able to set a custom HTTP request header after first write', (done) => {
  391. const requestUrl = '/requestUrl'
  392. const customHeaderName = 'Some-Custom-Header-Name'
  393. const customHeaderValue = 'Some-Customer-Header-Value'
  394. server.on('request', (request, response) => {
  395. switch (request.url) {
  396. case requestUrl:
  397. assert(!request.headers[customHeaderName.toLowerCase()])
  398. response.statusCode = 200
  399. response.statusMessage = 'OK'
  400. response.end()
  401. break
  402. default:
  403. handleUnexpectedURL(request, response)
  404. }
  405. })
  406. const urlRequest = net.request({
  407. method: 'GET',
  408. url: `${server.url}${requestUrl}`
  409. })
  410. urlRequest.on('response', (response) => {
  411. const statusCode = response.statusCode
  412. assert.equal(statusCode, 200)
  413. response.pause()
  414. response.on('data', (chunk) => {
  415. })
  416. response.on('end', () => {
  417. done()
  418. })
  419. response.resume()
  420. })
  421. urlRequest.write('')
  422. assert.throws(() => {
  423. urlRequest.setHeader(customHeaderName, customHeaderValue)
  424. })
  425. assert(!urlRequest.getHeader(customHeaderName))
  426. urlRequest.end()
  427. })
  428. it('should be able to remove a custom HTTP request header before first write', (done) => {
  429. const requestUrl = '/requestUrl'
  430. const customHeaderName = 'Some-Custom-Header-Name'
  431. const customHeaderValue = 'Some-Customer-Header-Value'
  432. server.on('request', (request, response) => {
  433. switch (request.url) {
  434. case requestUrl:
  435. assert(!request.headers[customHeaderName.toLowerCase()])
  436. response.statusCode = 200
  437. response.statusMessage = 'OK'
  438. response.end()
  439. break
  440. default:
  441. handleUnexpectedURL(request, response)
  442. }
  443. })
  444. const urlRequest = net.request({
  445. method: 'GET',
  446. url: `${server.url}${requestUrl}`
  447. })
  448. urlRequest.on('response', (response) => {
  449. const statusCode = response.statusCode
  450. assert.equal(statusCode, 200)
  451. response.pause()
  452. response.on('data', (chunk) => {
  453. })
  454. response.on('end', () => {
  455. done()
  456. })
  457. response.resume()
  458. })
  459. urlRequest.setHeader(customHeaderName, customHeaderValue)
  460. assert.equal(urlRequest.getHeader(customHeaderName),
  461. customHeaderValue)
  462. urlRequest.removeHeader(customHeaderName)
  463. assert(!urlRequest.getHeader(customHeaderName))
  464. urlRequest.write('')
  465. urlRequest.end()
  466. })
  467. it('should not be able to remove a custom HTTP request header after first write', (done) => {
  468. const requestUrl = '/requestUrl'
  469. const customHeaderName = 'Some-Custom-Header-Name'
  470. const customHeaderValue = 'Some-Customer-Header-Value'
  471. server.on('request', (request, response) => {
  472. switch (request.url) {
  473. case requestUrl:
  474. assert.equal(request.headers[customHeaderName.toLowerCase()],
  475. customHeaderValue)
  476. response.statusCode = 200
  477. response.statusMessage = 'OK'
  478. response.end()
  479. break
  480. default:
  481. handleUnexpectedURL(request, response)
  482. }
  483. })
  484. const urlRequest = net.request({
  485. method: 'GET',
  486. url: `${server.url}${requestUrl}`
  487. })
  488. urlRequest.on('response', (response) => {
  489. const statusCode = response.statusCode
  490. assert.equal(statusCode, 200)
  491. response.pause()
  492. response.on('data', (chunk) => {
  493. })
  494. response.on('end', () => {
  495. done()
  496. })
  497. response.resume()
  498. })
  499. urlRequest.setHeader(customHeaderName, customHeaderValue)
  500. assert.equal(urlRequest.getHeader(customHeaderName),
  501. customHeaderValue)
  502. urlRequest.write('')
  503. assert.throws(() => {
  504. urlRequest.removeHeader(customHeaderName)
  505. })
  506. assert.equal(urlRequest.getHeader(customHeaderName),
  507. customHeaderValue)
  508. urlRequest.end()
  509. })
  510. it('should be able to set cookie header line', (done) => {
  511. const requestUrl = '/requestUrl'
  512. const cookieHeaderName = 'Cookie'
  513. const cookieHeaderValue = 'test=12345'
  514. const customSession = session.fromPartition('test-cookie-header')
  515. server.on('request', (request, response) => {
  516. switch (request.url) {
  517. case requestUrl:
  518. assert.equal(request.headers[cookieHeaderName.toLowerCase()],
  519. cookieHeaderValue)
  520. response.statusCode = 200
  521. response.statusMessage = 'OK'
  522. response.end()
  523. break
  524. default:
  525. handleUnexpectedURL(request, response)
  526. }
  527. })
  528. customSession.cookies.set({
  529. url: `${server.url}`,
  530. name: 'test',
  531. value: '11111'
  532. }, (error) => {
  533. if (error) return done(error)
  534. const urlRequest = net.request({
  535. method: 'GET',
  536. url: `${server.url}${requestUrl}`,
  537. session: customSession
  538. })
  539. urlRequest.on('response', (response) => {
  540. const statusCode = response.statusCode
  541. assert.equal(statusCode, 200)
  542. response.pause()
  543. response.on('data', (chunk) => {})
  544. response.on('end', () => {
  545. done()
  546. })
  547. response.resume()
  548. })
  549. urlRequest.setHeader(cookieHeaderName, cookieHeaderValue)
  550. assert.equal(urlRequest.getHeader(cookieHeaderName),
  551. cookieHeaderValue)
  552. urlRequest.end()
  553. })
  554. })
  555. it('should be able to abort an HTTP request before first write', (done) => {
  556. const requestUrl = '/requestUrl'
  557. server.on('request', (request, response) => {
  558. response.end()
  559. assert.fail('Unexpected request event')
  560. })
  561. let requestAbortEventEmitted = false
  562. let requestCloseEventEmitted = false
  563. const urlRequest = net.request({
  564. method: 'GET',
  565. url: `${server.url}${requestUrl}`
  566. })
  567. urlRequest.on('response', (response) => {
  568. assert.fail('Unexpected response event')
  569. })
  570. urlRequest.on('finish', () => {
  571. assert.fail('Unexpected finish event')
  572. })
  573. urlRequest.on('error', () => {
  574. assert.fail('Unexpected error event')
  575. })
  576. urlRequest.on('abort', () => {
  577. requestAbortEventEmitted = true
  578. })
  579. urlRequest.on('close', () => {
  580. requestCloseEventEmitted = true
  581. assert(requestAbortEventEmitted)
  582. assert(requestCloseEventEmitted)
  583. done()
  584. })
  585. urlRequest.abort()
  586. assert(!urlRequest.write(''))
  587. urlRequest.end()
  588. })
  589. it('it should be able to abort an HTTP request before request end', (done) => {
  590. const requestUrl = '/requestUrl'
  591. let requestReceivedByServer = false
  592. server.on('request', (request, response) => {
  593. switch (request.url) {
  594. case requestUrl:
  595. requestReceivedByServer = true
  596. cancelRequest()
  597. break
  598. default:
  599. handleUnexpectedURL(request, response)
  600. }
  601. })
  602. let requestAbortEventEmitted = false
  603. let requestCloseEventEmitted = false
  604. const urlRequest = net.request({
  605. method: 'GET',
  606. url: `${server.url}${requestUrl}`
  607. })
  608. urlRequest.on('response', (response) => {
  609. assert.fail('Unexpected response event')
  610. })
  611. urlRequest.on('finish', () => {
  612. assert.fail('Unexpected finish event')
  613. })
  614. urlRequest.on('error', () => {
  615. assert.fail('Unexpected error event')
  616. })
  617. urlRequest.on('abort', () => {
  618. requestAbortEventEmitted = true
  619. })
  620. urlRequest.on('close', () => {
  621. requestCloseEventEmitted = true
  622. assert(requestReceivedByServer)
  623. assert(requestAbortEventEmitted)
  624. assert(requestCloseEventEmitted)
  625. done()
  626. })
  627. urlRequest.chunkedEncoding = true
  628. urlRequest.write(randomString(kOneKiloByte))
  629. function cancelRequest () {
  630. urlRequest.abort()
  631. }
  632. })
  633. it('it should be able to abort an HTTP request after request end and before response', (done) => {
  634. const requestUrl = '/requestUrl'
  635. let requestReceivedByServer = false
  636. server.on('request', (request, response) => {
  637. switch (request.url) {
  638. case requestUrl:
  639. requestReceivedByServer = true
  640. cancelRequest()
  641. process.nextTick(() => {
  642. response.statusCode = 200
  643. response.statusMessage = 'OK'
  644. response.end()
  645. })
  646. break
  647. default:
  648. handleUnexpectedURL(request, response)
  649. }
  650. })
  651. let requestAbortEventEmitted = false
  652. let requestFinishEventEmitted = false
  653. let requestCloseEventEmitted = false
  654. const urlRequest = net.request({
  655. method: 'GET',
  656. url: `${server.url}${requestUrl}`
  657. })
  658. urlRequest.on('response', (response) => {
  659. assert.fail('Unexpected response event')
  660. })
  661. urlRequest.on('finish', () => {
  662. requestFinishEventEmitted = true
  663. })
  664. urlRequest.on('error', () => {
  665. assert.fail('Unexpected error event')
  666. })
  667. urlRequest.on('abort', () => {
  668. requestAbortEventEmitted = true
  669. })
  670. urlRequest.on('close', () => {
  671. requestCloseEventEmitted = true
  672. assert(requestFinishEventEmitted)
  673. assert(requestReceivedByServer)
  674. assert(requestAbortEventEmitted)
  675. assert(requestCloseEventEmitted)
  676. done()
  677. })
  678. urlRequest.end(randomString(kOneKiloByte))
  679. function cancelRequest () {
  680. urlRequest.abort()
  681. }
  682. })
  683. it('it should be able to abort an HTTP request after response start', (done) => {
  684. const requestUrl = '/requestUrl'
  685. let requestReceivedByServer = false
  686. server.on('request', (request, response) => {
  687. switch (request.url) {
  688. case requestUrl:
  689. requestReceivedByServer = true
  690. response.statusCode = 200
  691. response.statusMessage = 'OK'
  692. response.write(randomString(kOneKiloByte))
  693. break
  694. default:
  695. handleUnexpectedURL(request, response)
  696. }
  697. })
  698. let requestFinishEventEmitted = false
  699. let requestResponseEventEmitted = false
  700. let requestAbortEventEmitted = false
  701. let requestCloseEventEmitted = false
  702. let responseAbortedEventEmitted = false
  703. const urlRequest = net.request({
  704. method: 'GET',
  705. url: `${server.url}${requestUrl}`
  706. })
  707. urlRequest.on('response', (response) => {
  708. requestResponseEventEmitted = true
  709. const statusCode = response.statusCode
  710. assert.equal(statusCode, 200)
  711. response.pause()
  712. response.on('data', (chunk) => {
  713. })
  714. response.on('end', () => {
  715. assert.fail('Unexpected end event')
  716. })
  717. response.resume()
  718. response.on('error', () => {
  719. assert.fail('Unexpected error event')
  720. })
  721. response.on('aborted', () => {
  722. responseAbortedEventEmitted = true
  723. })
  724. urlRequest.abort()
  725. })
  726. urlRequest.on('finish', () => {
  727. requestFinishEventEmitted = true
  728. })
  729. urlRequest.on('error', () => {
  730. assert.fail('Unexpected error event')
  731. })
  732. urlRequest.on('abort', () => {
  733. requestAbortEventEmitted = true
  734. })
  735. urlRequest.on('close', () => {
  736. requestCloseEventEmitted = true
  737. assert(requestFinishEventEmitted, 'request should emit "finish" event')
  738. assert(requestReceivedByServer, 'request should be received by the server')
  739. assert(requestResponseEventEmitted, '"response" event should be emitted')
  740. assert(requestAbortEventEmitted, 'request should emit "abort" event')
  741. assert(responseAbortedEventEmitted, 'response should emit "aborted" event')
  742. assert(requestCloseEventEmitted, 'request should emit "close" event')
  743. done()
  744. })
  745. urlRequest.end(randomString(kOneKiloByte))
  746. })
  747. it('abort event should be emitted at most once', (done) => {
  748. const requestUrl = '/requestUrl'
  749. let requestReceivedByServer = false
  750. server.on('request', (request, response) => {
  751. switch (request.url) {
  752. case requestUrl:
  753. requestReceivedByServer = true
  754. cancelRequest()
  755. break
  756. default:
  757. handleUnexpectedURL(request, response)
  758. }
  759. })
  760. let requestFinishEventEmitted = false
  761. let requestAbortEventCount = 0
  762. let requestCloseEventEmitted = false
  763. const urlRequest = net.request({
  764. method: 'GET',
  765. url: `${server.url}${requestUrl}`
  766. })
  767. urlRequest.on('response', () => {
  768. assert.fail('Unexpected response event')
  769. })
  770. urlRequest.on('finish', () => {
  771. requestFinishEventEmitted = true
  772. })
  773. urlRequest.on('error', () => {
  774. assert.fail('Unexpected error event')
  775. })
  776. urlRequest.on('abort', () => {
  777. ++requestAbortEventCount
  778. urlRequest.abort()
  779. })
  780. urlRequest.on('close', () => {
  781. requestCloseEventEmitted = true
  782. // Let all pending async events to be emitted
  783. setTimeout(() => {
  784. assert(requestFinishEventEmitted)
  785. assert(requestReceivedByServer)
  786. assert.equal(requestAbortEventCount, 1)
  787. assert(requestCloseEventEmitted)
  788. done()
  789. }, 500)
  790. })
  791. urlRequest.end(randomString(kOneKiloByte))
  792. function cancelRequest () {
  793. urlRequest.abort()
  794. urlRequest.abort()
  795. }
  796. })
  797. it('Requests should be intercepted by webRequest module', (done) => {
  798. const requestUrl = '/requestUrl'
  799. const redirectUrl = '/redirectUrl'
  800. let requestIsRedirected = false
  801. server.on('request', (request, response) => {
  802. switch (request.url) {
  803. case redirectUrl:
  804. requestIsRedirected = true
  805. response.end()
  806. break
  807. default:
  808. handleUnexpectedURL(request, response)
  809. }
  810. })
  811. let requestIsIntercepted = false
  812. session.defaultSession.webRequest.onBeforeRequest(
  813. (details, callback) => {
  814. if (details.url === `${server.url}${requestUrl}`) {
  815. requestIsIntercepted = true
  816. // Disabled due to false positive in StandardJS
  817. // eslint-disable-next-line standard/no-callback-literal
  818. callback({
  819. redirectURL: `${server.url}${redirectUrl}`
  820. })
  821. } else {
  822. callback({
  823. cancel: false
  824. })
  825. }
  826. })
  827. const urlRequest = net.request(`${server.url}${requestUrl}`)
  828. urlRequest.on('response', (response) => {
  829. assert.equal(response.statusCode, 200)
  830. response.pause()
  831. response.on('data', (chunk) => {
  832. })
  833. response.on('end', () => {
  834. assert(requestIsRedirected, 'The server should receive a request to the forward URL')
  835. assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
  836. done()
  837. })
  838. response.resume()
  839. })
  840. urlRequest.end()
  841. })
  842. it('should to able to create and intercept a request using a custom session object', (done) => {
  843. const requestUrl = '/requestUrl'
  844. const redirectUrl = '/redirectUrl'
  845. const customPartitionName = 'custom-partition'
  846. let requestIsRedirected = false
  847. server.on('request', (request, response) => {
  848. switch (request.url) {
  849. case redirectUrl:
  850. requestIsRedirected = true
  851. response.end()
  852. break
  853. default:
  854. handleUnexpectedURL(request, response)
  855. }
  856. })
  857. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  858. assert.fail('Request should not be intercepted by the default session')
  859. })
  860. let customSession = session.fromPartition(customPartitionName, {cache: false})
  861. let requestIsIntercepted = false
  862. customSession.webRequest.onBeforeRequest((details, callback) => {
  863. if (details.url === `${server.url}${requestUrl}`) {
  864. requestIsIntercepted = true
  865. // Disabled due to false positive in StandardJS
  866. // eslint-disable-next-line standard/no-callback-literal
  867. callback({
  868. redirectURL: `${server.url}${redirectUrl}`
  869. })
  870. } else {
  871. callback({
  872. cancel: false
  873. })
  874. }
  875. })
  876. const urlRequest = net.request({
  877. url: `${server.url}${requestUrl}`,
  878. session: customSession
  879. })
  880. urlRequest.on('response', (response) => {
  881. assert.equal(response.statusCode, 200)
  882. response.pause()
  883. response.on('data', (chunk) => {
  884. })
  885. response.on('end', () => {
  886. assert(requestIsRedirected, 'The server should receive a request to the forward URL')
  887. assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
  888. done()
  889. })
  890. response.resume()
  891. })
  892. urlRequest.end()
  893. })
  894. it('should throw if given an invalid redirect mode', () => {
  895. const requestUrl = '/requestUrl'
  896. const options = {
  897. url: `${server.url}${requestUrl}`,
  898. redirect: 'custom'
  899. }
  900. assert.throws(() => {
  901. net.request(options)
  902. }, 'redirect mode should be one of follow, error or manual')
  903. })
  904. it('should throw when calling getHeader without a name', () => {
  905. assert.throws(() => {
  906. net.request({url: `${server.url}/requestUrl`}).getHeader()
  907. }, /`name` is required for getHeader\(name\)\./)
  908. assert.throws(() => {
  909. net.request({url: `${server.url}/requestUrl`}).getHeader(null)
  910. }, /`name` is required for getHeader\(name\)\./)
  911. })
  912. it('should throw when calling removeHeader without a name', () => {
  913. assert.throws(() => {
  914. net.request({url: `${server.url}/requestUrl`}).removeHeader()
  915. }, /`name` is required for removeHeader\(name\)\./)
  916. assert.throws(() => {
  917. net.request({url: `${server.url}/requestUrl`}).removeHeader(null)
  918. }, /`name` is required for removeHeader\(name\)\./)
  919. })
  920. it('should follow redirect when no redirect mode is provided', (done) => {
  921. const requestUrl = '/301'
  922. server.on('request', (request, response) => {
  923. switch (request.url) {
  924. case '/301':
  925. response.statusCode = '301'
  926. response.setHeader('Location', '/200')
  927. response.end()
  928. break
  929. case '/200':
  930. response.statusCode = '200'
  931. response.end()
  932. break
  933. default:
  934. handleUnexpectedURL(request, response)
  935. }
  936. })
  937. const urlRequest = net.request({
  938. url: `${server.url}${requestUrl}`
  939. })
  940. urlRequest.on('response', (response) => {
  941. assert.equal(response.statusCode, 200)
  942. done()
  943. })
  944. urlRequest.end()
  945. })
  946. it('should follow redirect chain when no redirect mode is provided', (done) => {
  947. const requestUrl = '/redirectChain'
  948. server.on('request', (request, response) => {
  949. switch (request.url) {
  950. case '/redirectChain':
  951. response.statusCode = '301'
  952. response.setHeader('Location', '/301')
  953. response.end()
  954. break
  955. case '/301':
  956. response.statusCode = '301'
  957. response.setHeader('Location', '/200')
  958. response.end()
  959. break
  960. case '/200':
  961. response.statusCode = '200'
  962. response.end()
  963. break
  964. default:
  965. handleUnexpectedURL(request, response)
  966. }
  967. })
  968. const urlRequest = net.request({
  969. url: `${server.url}${requestUrl}`
  970. })
  971. urlRequest.on('response', (response) => {
  972. assert.equal(response.statusCode, 200)
  973. done()
  974. })
  975. urlRequest.end()
  976. })
  977. it('should not follow redirect when mode is error', (done) => {
  978. const requestUrl = '/301'
  979. server.on('request', (request, response) => {
  980. switch (request.url) {
  981. case '/301':
  982. response.statusCode = '301'
  983. response.setHeader('Location', '/200')
  984. response.end()
  985. break
  986. case '/200':
  987. response.statusCode = '200'
  988. response.end()
  989. break
  990. default:
  991. handleUnexpectedURL(request, response)
  992. }
  993. })
  994. const urlRequest = net.request({
  995. url: `${server.url}${requestUrl}`,
  996. redirect: 'error'
  997. })
  998. urlRequest.on('error', (error) => {
  999. assert.equal(error.message, 'Request cannot follow redirect with the current redirect mode')
  1000. })
  1001. urlRequest.on('close', () => {
  1002. done()
  1003. })
  1004. urlRequest.end()
  1005. })
  1006. it('should allow follow redirect when mode is manual', (done) => {
  1007. const requestUrl = '/redirectChain'
  1008. let redirectCount = 0
  1009. server.on('request', (request, response) => {
  1010. switch (request.url) {
  1011. case '/redirectChain':
  1012. response.statusCode = '301'
  1013. response.setHeader('Location', '/301')
  1014. response.end()
  1015. break
  1016. case '/301':
  1017. response.statusCode = '301'
  1018. response.setHeader('Location', '/200')
  1019. response.end()
  1020. break
  1021. case '/200':
  1022. response.statusCode = '200'
  1023. response.end()
  1024. break
  1025. default:
  1026. handleUnexpectedURL(request, response)
  1027. }
  1028. })
  1029. const urlRequest = net.request({
  1030. url: `${server.url}${requestUrl}`,
  1031. redirect: 'manual'
  1032. })
  1033. urlRequest.on('response', (response) => {
  1034. assert.equal(response.statusCode, 200)
  1035. assert.equal(redirectCount, 2)
  1036. done()
  1037. })
  1038. urlRequest.on('redirect', (status, method, url) => {
  1039. if (url === `${server.url}/301` || url === `${server.url}/200`) {
  1040. redirectCount += 1
  1041. urlRequest.followRedirect()
  1042. }
  1043. })
  1044. urlRequest.end()
  1045. })
  1046. it('should allow cancelling redirect when mode is manual', (done) => {
  1047. const requestUrl = '/redirectChain'
  1048. let redirectCount = 0
  1049. server.on('request', (request, response) => {
  1050. switch (request.url) {
  1051. case '/redirectChain':
  1052. response.statusCode = '301'
  1053. response.setHeader('Location', '/redirect/1')
  1054. response.end()
  1055. break
  1056. case '/redirect/1':
  1057. response.statusCode = '200'
  1058. response.setHeader('Location', '/redirect/2')
  1059. response.end()
  1060. break
  1061. case '/redirect/2':
  1062. response.statusCode = '200'
  1063. response.end()
  1064. break
  1065. default:
  1066. handleUnexpectedURL(request, response)
  1067. }
  1068. })
  1069. const urlRequest = net.request({
  1070. url: `${server.url}${requestUrl}`,
  1071. redirect: 'manual'
  1072. })
  1073. urlRequest.on('response', (response) => {
  1074. assert.equal(response.statusCode, 200)
  1075. response.pause()
  1076. response.on('data', (chunk) => {
  1077. })
  1078. response.on('end', () => {
  1079. urlRequest.abort()
  1080. })
  1081. response.resume()
  1082. })
  1083. urlRequest.on('close', () => {
  1084. assert.equal(redirectCount, 1)
  1085. done()
  1086. })
  1087. urlRequest.on('redirect', (status, method, url) => {
  1088. if (url === `${server.url}/redirect/1`) {
  1089. redirectCount += 1
  1090. urlRequest.followRedirect()
  1091. }
  1092. })
  1093. urlRequest.end()
  1094. })
  1095. it('should throw if given an invalid session option', (done) => {
  1096. const requestUrl = '/requestUrl'
  1097. try {
  1098. const urlRequest = net.request({
  1099. url: `${server.url}${requestUrl}`,
  1100. session: 1
  1101. })
  1102. // eslint-disable-next-line
  1103. urlRequest
  1104. } catch (exception) {
  1105. done()
  1106. }
  1107. })
  1108. it('should to able to create and intercept a request using a custom partition name', (done) => {
  1109. const requestUrl = '/requestUrl'
  1110. const redirectUrl = '/redirectUrl'
  1111. const customPartitionName = 'custom-partition'
  1112. let requestIsRedirected = false
  1113. server.on('request', (request, response) => {
  1114. switch (request.url) {
  1115. case redirectUrl:
  1116. requestIsRedirected = true
  1117. response.end()
  1118. break
  1119. default:
  1120. handleUnexpectedURL(request, response)
  1121. }
  1122. })
  1123. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  1124. assert.fail('Request should not be intercepted by the default session')
  1125. })
  1126. let customSession = session.fromPartition(customPartitionName, {
  1127. cache: false
  1128. })
  1129. let requestIsIntercepted = false
  1130. customSession.webRequest.onBeforeRequest((details, callback) => {
  1131. if (details.url === `${server.url}${requestUrl}`) {
  1132. requestIsIntercepted = true
  1133. callback({
  1134. redirectURL: `${server.url}${redirectUrl}`
  1135. })
  1136. } else {
  1137. callback({
  1138. cancel: false
  1139. })
  1140. }
  1141. })
  1142. const urlRequest = net.request({
  1143. url: `${server.url}${requestUrl}`,
  1144. partition: customPartitionName
  1145. })
  1146. urlRequest.on('response', (response) => {
  1147. assert.equal(response.statusCode, 200)
  1148. response.pause()
  1149. response.on('data', (chunk) => {
  1150. })
  1151. response.on('end', () => {
  1152. assert(requestIsRedirected, 'The server should receive a request to the forward URL')
  1153. assert(requestIsIntercepted, 'The request should be intercepted by the webRequest module')
  1154. done()
  1155. })
  1156. response.resume()
  1157. })
  1158. urlRequest.end()
  1159. })
  1160. it('should throw if given an invalid partition option', (done) => {
  1161. const requestUrl = '/requestUrl'
  1162. try {
  1163. const urlRequest = net.request({
  1164. url: `${server.url}${requestUrl}`,
  1165. partition: 1
  1166. })
  1167. // eslint-disable-next-line
  1168. urlRequest
  1169. } catch (exception) {
  1170. done()
  1171. }
  1172. })
  1173. it('should be able to create a request with options', (done) => {
  1174. const requestUrl = '/'
  1175. const customHeaderName = 'Some-Custom-Header-Name'
  1176. const customHeaderValue = 'Some-Customer-Header-Value'
  1177. server.on('request', (request, response) => {
  1178. switch (request.url) {
  1179. case requestUrl:
  1180. assert.equal(request.method, 'GET')
  1181. assert.equal(request.headers[customHeaderName.toLowerCase()],
  1182. customHeaderValue)
  1183. response.statusCode = 200
  1184. response.statusMessage = 'OK'
  1185. response.end()
  1186. break
  1187. default:
  1188. handleUnexpectedURL(request, response)
  1189. }
  1190. })
  1191. const serverUrl = url.parse(server.url)
  1192. let options = {
  1193. port: serverUrl.port,
  1194. hostname: '127.0.0.1',
  1195. headers: {}
  1196. }
  1197. options.headers[customHeaderName] = customHeaderValue
  1198. const urlRequest = net.request(options)
  1199. urlRequest.on('response', (response) => {
  1200. assert.equal(response.statusCode, 200)
  1201. response.pause()
  1202. response.on('data', (chunk) => {
  1203. })
  1204. response.on('end', () => {
  1205. done()
  1206. })
  1207. response.resume()
  1208. })
  1209. urlRequest.end()
  1210. })
  1211. it('should be able to pipe a readable stream into a net request', (done) => {
  1212. const nodeRequestUrl = '/nodeRequestUrl'
  1213. const netRequestUrl = '/netRequestUrl'
  1214. const bodyData = randomString(kOneMegaByte)
  1215. let netRequestReceived = false
  1216. let netRequestEnded = false
  1217. server.on('request', (request, response) => {
  1218. switch (request.url) {
  1219. case nodeRequestUrl:
  1220. response.write(bodyData)
  1221. response.end()
  1222. break
  1223. case netRequestUrl:
  1224. netRequestReceived = true
  1225. let receivedBodyData = ''
  1226. request.on('data', (chunk) => {
  1227. receivedBodyData += chunk.toString()
  1228. })
  1229. request.on('end', (chunk) => {
  1230. netRequestEnded = true
  1231. if (chunk) {
  1232. receivedBodyData += chunk.toString()
  1233. }
  1234. assert.equal(receivedBodyData, bodyData)
  1235. response.end()
  1236. })
  1237. break
  1238. default:
  1239. handleUnexpectedURL(request, response)
  1240. }
  1241. })
  1242. let nodeRequest = http.request(`${server.url}${nodeRequestUrl}`)
  1243. nodeRequest.on('response', (nodeResponse) => {
  1244. const netRequest = net.request(`${server.url}${netRequestUrl}`)
  1245. netRequest.on('response', (netResponse) => {
  1246. assert.equal(netResponse.statusCode, 200)
  1247. netResponse.pause()
  1248. netResponse.on('data', (chunk) => {})
  1249. netResponse.on('end', () => {
  1250. assert(netRequestReceived)
  1251. assert(netRequestEnded)
  1252. done()
  1253. })
  1254. netResponse.resume()
  1255. })
  1256. nodeResponse.pipe(netRequest)
  1257. })
  1258. nodeRequest.end()
  1259. })
  1260. it('should emit error event on server socket close', (done) => {
  1261. const requestUrl = '/requestUrl'
  1262. server.on('request', (request, response) => {
  1263. switch (request.url) {
  1264. case requestUrl:
  1265. request.socket.destroy()
  1266. break
  1267. default:
  1268. handleUnexpectedURL(request, response)
  1269. }
  1270. })
  1271. let requestErrorEventEmitted = false
  1272. const urlRequest = net.request(`${server.url}${requestUrl}`)
  1273. urlRequest.on('error', (error) => {
  1274. assert(error)
  1275. requestErrorEventEmitted = true
  1276. })
  1277. urlRequest.on('close', () => {
  1278. assert(requestErrorEventEmitted)
  1279. done()
  1280. })
  1281. urlRequest.end()
  1282. })
  1283. })
  1284. describe('IncomingMessage API', () => {
  1285. it('response object should implement the IncomingMessage API', (done) => {
  1286. const requestUrl = '/requestUrl'
  1287. const customHeaderName = 'Some-Custom-Header-Name'
  1288. const customHeaderValue = 'Some-Customer-Header-Value'
  1289. server.on('request', (request, response) => {
  1290. switch (request.url) {
  1291. case requestUrl:
  1292. response.statusCode = 200
  1293. response.statusMessage = 'OK'
  1294. response.setHeader(customHeaderName, customHeaderValue)
  1295. response.end()
  1296. break
  1297. default:
  1298. handleUnexpectedURL(request, response)
  1299. }
  1300. })
  1301. const urlRequest = net.request({
  1302. method: 'GET',
  1303. url: `${server.url}${requestUrl}`
  1304. })
  1305. urlRequest.on('response', (response) => {
  1306. const statusCode = response.statusCode
  1307. assert(typeof statusCode === 'number')
  1308. assert.equal(statusCode, 200)
  1309. const statusMessage = response.statusMessage
  1310. assert(typeof statusMessage === 'string')
  1311. assert.equal(statusMessage, 'OK')
  1312. const headers = response.headers
  1313. assert(typeof headers === 'object')
  1314. assert.deepEqual(headers[customHeaderName.toLowerCase()],
  1315. [customHeaderValue])
  1316. const httpVersion = response.httpVersion
  1317. assert(typeof httpVersion === 'string')
  1318. assert(httpVersion.length > 0)
  1319. const httpVersionMajor = response.httpVersionMajor
  1320. assert(typeof httpVersionMajor === 'number')
  1321. assert(httpVersionMajor >= 1)
  1322. const httpVersionMinor = response.httpVersionMinor
  1323. assert(typeof httpVersionMinor === 'number')
  1324. assert(httpVersionMinor >= 0)
  1325. response.pause()
  1326. response.on('data', (chunk) => {
  1327. })
  1328. response.on('end', () => {
  1329. done()
  1330. })
  1331. response.resume()
  1332. })
  1333. urlRequest.end()
  1334. })
  1335. it('should be able to pipe a net response into a writable stream', (done) => {
  1336. const nodeRequestUrl = '/nodeRequestUrl'
  1337. const netRequestUrl = '/netRequestUrl'
  1338. const bodyData = randomString(kOneMegaByte)
  1339. server.on('request', (request, response) => {
  1340. switch (request.url) {
  1341. case netRequestUrl:
  1342. response.statusCode = 200
  1343. response.statusMessage = 'OK'
  1344. response.write(bodyData)
  1345. response.end()
  1346. break
  1347. case nodeRequestUrl:
  1348. let receivedBodyData = ''
  1349. request.on('data', (chunk) => {
  1350. receivedBodyData += chunk.toString()
  1351. })
  1352. request.on('end', (chunk) => {
  1353. if (chunk) {
  1354. receivedBodyData += chunk.toString()
  1355. }
  1356. assert.equal(receivedBodyData, bodyData)
  1357. response.end()
  1358. })
  1359. break
  1360. default:
  1361. handleUnexpectedURL(request, response)
  1362. }
  1363. })
  1364. ipcRenderer.once('api-net-spec-done', () => {
  1365. done()
  1366. })
  1367. // Execute below code directly within the browser context without
  1368. // using the remote module.
  1369. ipcRenderer.send('eval', `
  1370. const {net} = require('electron')
  1371. const http = require('http')
  1372. const netRequest = net.request('${server.url}${netRequestUrl}')
  1373. netRequest.on('response', function (netResponse) {
  1374. const serverUrl = url.parse('${server.url}')
  1375. const nodeOptions = {
  1376. method: 'POST',
  1377. path: '${nodeRequestUrl}',
  1378. port: serverUrl.port
  1379. }
  1380. let nodeRequest = http.request(nodeOptions)
  1381. nodeRequest.on('response', function (nodeResponse) {
  1382. nodeResponse.on('data', (chunk) => {
  1383. })
  1384. nodeResponse.on('end', (chunk) => {
  1385. event.sender.send('api-net-spec-done')
  1386. })
  1387. })
  1388. netResponse.pipe(nodeRequest)
  1389. })
  1390. netRequest.end()
  1391. `)
  1392. })
  1393. it('should not emit any event after close', (done) => {
  1394. const requestUrl = '/requestUrl'
  1395. let bodyData = randomString(kOneKiloByte)
  1396. server.on('request', (request, response) => {
  1397. switch (request.url) {
  1398. case requestUrl:
  1399. response.statusCode = 200
  1400. response.statusMessage = 'OK'
  1401. response.write(bodyData)
  1402. response.end()
  1403. break
  1404. default:
  1405. handleUnexpectedURL(request, response)
  1406. }
  1407. })
  1408. let requestCloseEventEmitted = false
  1409. const urlRequest = net.request({
  1410. method: 'GET',
  1411. url: `${server.url}${requestUrl}`
  1412. })
  1413. urlRequest.on('response', (response) => {
  1414. assert(!requestCloseEventEmitted)
  1415. const statusCode = response.statusCode
  1416. assert.equal(statusCode, 200)
  1417. response.pause()
  1418. response.on('data', () => {
  1419. })
  1420. response.on('end', () => {
  1421. })
  1422. response.resume()
  1423. response.on('error', () => {
  1424. assert(!requestCloseEventEmitted)
  1425. })
  1426. response.on('aborted', () => {
  1427. assert(!requestCloseEventEmitted)
  1428. })
  1429. })
  1430. urlRequest.on('finish', () => {
  1431. assert(!requestCloseEventEmitted)
  1432. })
  1433. urlRequest.on('error', () => {
  1434. assert(!requestCloseEventEmitted)
  1435. })
  1436. urlRequest.on('abort', () => {
  1437. assert(!requestCloseEventEmitted)
  1438. })
  1439. urlRequest.on('close', () => {
  1440. requestCloseEventEmitted = true
  1441. // Wait so that all async events get scheduled.
  1442. setTimeout(() => {
  1443. done()
  1444. }, 100)
  1445. })
  1446. urlRequest.end()
  1447. })
  1448. })
  1449. describe('Stability and performance', (done) => {
  1450. it('should free unreferenced, never-started request objects without crash', (done) => {
  1451. const requestUrl = '/requestUrl'
  1452. ipcRenderer.once('api-net-spec-done', () => {
  1453. done()
  1454. })
  1455. ipcRenderer.send('eval', `
  1456. const {net} = require('electron')
  1457. const urlRequest = net.request('${server.url}${requestUrl}')
  1458. process.nextTick(() => {
  1459. const v8Util = process.atomBinding('v8_util')
  1460. v8Util.requestGarbageCollectionForTesting()
  1461. event.sender.send('api-net-spec-done')
  1462. })
  1463. `)
  1464. })
  1465. it('should not collect on-going requests without crash', (done) => {
  1466. const requestUrl = '/requestUrl'
  1467. server.on('request', (request, response) => {
  1468. switch (request.url) {
  1469. case requestUrl:
  1470. response.statusCode = 200
  1471. response.statusMessage = 'OK'
  1472. response.write(randomString(kOneKiloByte))
  1473. ipcRenderer.once('api-net-spec-resume', () => {
  1474. response.write(randomString(kOneKiloByte))
  1475. response.end()
  1476. })
  1477. break
  1478. default:
  1479. handleUnexpectedURL(request, response)
  1480. }
  1481. })
  1482. ipcRenderer.once('api-net-spec-done', () => {
  1483. done()
  1484. })
  1485. // Execute below code directly within the browser context without
  1486. // using the remote module.
  1487. ipcRenderer.send('eval', `
  1488. const {net} = require('electron')
  1489. const urlRequest = net.request('${server.url}${requestUrl}')
  1490. urlRequest.on('response', (response) => {
  1491. response.on('data', () => {
  1492. })
  1493. response.on('end', () => {
  1494. event.sender.send('api-net-spec-done')
  1495. })
  1496. process.nextTick(() => {
  1497. // Trigger a garbage collection.
  1498. const v8Util = process.atomBinding('v8_util')
  1499. v8Util.requestGarbageCollectionForTesting()
  1500. event.sender.send('api-net-spec-resume')
  1501. })
  1502. })
  1503. urlRequest.end()
  1504. `)
  1505. })
  1506. it('should collect unreferenced, ended requests without crash', (done) => {
  1507. const requestUrl = '/requestUrl'
  1508. server.on('request', (request, response) => {
  1509. switch (request.url) {
  1510. case requestUrl:
  1511. response.statusCode = 200
  1512. response.statusMessage = 'OK'
  1513. response.end()
  1514. break
  1515. default:
  1516. handleUnexpectedURL(request, response)
  1517. }
  1518. })
  1519. ipcRenderer.once('api-net-spec-done', () => {
  1520. done()
  1521. })
  1522. ipcRenderer.send('eval', `
  1523. const {net} = require('electron')
  1524. const urlRequest = net.request('${server.url}${requestUrl}')
  1525. urlRequest.on('response', (response) => {
  1526. response.on('data', () => {
  1527. })
  1528. response.on('end', () => {
  1529. })
  1530. })
  1531. urlRequest.on('close', () => {
  1532. process.nextTick(() => {
  1533. const v8Util = process.atomBinding('v8_util')
  1534. v8Util.requestGarbageCollectionForTesting()
  1535. event.sender.send('api-net-spec-done')
  1536. })
  1537. })
  1538. urlRequest.end()
  1539. `)
  1540. })
  1541. })
  1542. })
  1543. function handleUnexpectedURL (request, response) {
  1544. response.statusCode = '500'
  1545. response.end()
  1546. assert.fail(`Unexpected URL: ${request.url}`)
  1547. }