api-protocol-spec.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. const assert = require('assert')
  2. const http = require('http')
  3. const path = require('path')
  4. const qs = require('querystring')
  5. const {closeWindow} = require('./window-helpers')
  6. const remote = require('electron').remote
  7. const {BrowserWindow, ipcMain, protocol, session, webContents} = remote
  8. describe('protocol module', function () {
  9. var protocolName = 'sp'
  10. var text = 'valar morghulis'
  11. var postData = {
  12. name: 'post test',
  13. type: 'string'
  14. }
  15. afterEach(function (done) {
  16. protocol.unregisterProtocol(protocolName, function () {
  17. protocol.uninterceptProtocol('http', function () {
  18. done()
  19. })
  20. })
  21. })
  22. describe('protocol.register(Any)Protocol', function () {
  23. var emptyHandler = function (request, callback) {
  24. callback()
  25. }
  26. it('throws error when scheme is already registered', function (done) {
  27. protocol.registerStringProtocol(protocolName, emptyHandler, function (error) {
  28. assert.equal(error, null)
  29. protocol.registerBufferProtocol(protocolName, emptyHandler, function (error) {
  30. assert.notEqual(error, null)
  31. done()
  32. })
  33. })
  34. })
  35. it('does not crash when handler is called twice', function (done) {
  36. var doubleHandler = function (request, callback) {
  37. try {
  38. callback(text)
  39. callback()
  40. } catch (error) {
  41. // Ignore error
  42. }
  43. }
  44. protocol.registerStringProtocol(protocolName, doubleHandler, function (error) {
  45. if (error) {
  46. return done(error)
  47. }
  48. $.ajax({
  49. url: protocolName + '://fake-host',
  50. cache: false,
  51. success: function (data) {
  52. assert.equal(data, text)
  53. done()
  54. },
  55. error: function (xhr, errorType, error) {
  56. done(error)
  57. }
  58. })
  59. })
  60. })
  61. it('sends error when callback is called with nothing', function (done) {
  62. protocol.registerBufferProtocol(protocolName, emptyHandler, function (error) {
  63. if (error) {
  64. return done(error)
  65. }
  66. $.ajax({
  67. url: protocolName + '://fake-host',
  68. cache: false,
  69. success: function () {
  70. return done('request succeeded but it should not')
  71. },
  72. error: function (xhr, errorType) {
  73. assert.equal(errorType, 'error')
  74. return done()
  75. }
  76. })
  77. })
  78. })
  79. it('does not crash when callback is called in next tick', function (done) {
  80. var handler = function (request, callback) {
  81. setImmediate(function () {
  82. callback(text)
  83. })
  84. }
  85. protocol.registerStringProtocol(protocolName, handler, function (error) {
  86. if (error) {
  87. return done(error)
  88. }
  89. $.ajax({
  90. url: protocolName + '://fake-host',
  91. cache: false,
  92. success: function (data) {
  93. assert.equal(data, text)
  94. done()
  95. },
  96. error: function (xhr, errorType, error) {
  97. done(error)
  98. }
  99. })
  100. })
  101. })
  102. })
  103. describe('protocol.unregisterProtocol', function () {
  104. it('returns error when scheme does not exist', function (done) {
  105. protocol.unregisterProtocol('not-exist', function (error) {
  106. assert.notEqual(error, null)
  107. done()
  108. })
  109. })
  110. })
  111. describe('protocol.registerStringProtocol', function () {
  112. it('sends string as response', function (done) {
  113. var handler = function (request, callback) {
  114. callback(text)
  115. }
  116. protocol.registerStringProtocol(protocolName, handler, function (error) {
  117. if (error) {
  118. return done(error)
  119. }
  120. $.ajax({
  121. url: protocolName + '://fake-host',
  122. cache: false,
  123. success: function (data) {
  124. assert.equal(data, text)
  125. done()
  126. },
  127. error: function (xhr, errorType, error) {
  128. done(error)
  129. }
  130. })
  131. })
  132. })
  133. it('sets Access-Control-Allow-Origin', function (done) {
  134. var handler = function (request, callback) {
  135. callback(text)
  136. }
  137. protocol.registerStringProtocol(protocolName, handler, function (error) {
  138. if (error) {
  139. return done(error)
  140. }
  141. $.ajax({
  142. url: protocolName + '://fake-host',
  143. cache: false,
  144. success: function (data, status, request) {
  145. assert.equal(data, text)
  146. assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
  147. done()
  148. },
  149. error: function (xhr, errorType, error) {
  150. done(error)
  151. }
  152. })
  153. })
  154. })
  155. it('sends object as response', function (done) {
  156. var handler = function (request, callback) {
  157. callback({
  158. data: text,
  159. mimeType: 'text/html'
  160. })
  161. }
  162. protocol.registerStringProtocol(protocolName, handler, function (error) {
  163. if (error) {
  164. return done(error)
  165. }
  166. $.ajax({
  167. url: protocolName + '://fake-host',
  168. cache: false,
  169. success: function (data) {
  170. assert.equal(data, text)
  171. done()
  172. },
  173. error: function (xhr, errorType, error) {
  174. done(error)
  175. }
  176. })
  177. })
  178. })
  179. it('fails when sending object other than string', function (done) {
  180. var handler = function (request, callback) {
  181. callback(new Date())
  182. }
  183. protocol.registerBufferProtocol(protocolName, handler, function (error) {
  184. if (error) {
  185. return done(error)
  186. }
  187. $.ajax({
  188. url: protocolName + '://fake-host',
  189. cache: false,
  190. success: function () {
  191. done('request succeeded but it should not')
  192. },
  193. error: function (xhr, errorType) {
  194. assert.equal(errorType, 'error')
  195. done()
  196. }
  197. })
  198. })
  199. })
  200. })
  201. describe('protocol.registerBufferProtocol', function () {
  202. var buffer = new Buffer(text)
  203. it('sends Buffer as response', function (done) {
  204. var handler = function (request, callback) {
  205. callback(buffer)
  206. }
  207. protocol.registerBufferProtocol(protocolName, handler, function (error) {
  208. if (error) {
  209. return done(error)
  210. }
  211. $.ajax({
  212. url: protocolName + '://fake-host',
  213. cache: false,
  214. success: function (data) {
  215. assert.equal(data, text)
  216. done()
  217. },
  218. error: function (xhr, errorType, error) {
  219. done(error)
  220. }
  221. })
  222. })
  223. })
  224. it('sets Access-Control-Allow-Origin', function (done) {
  225. var handler = function (request, callback) {
  226. callback(buffer)
  227. }
  228. protocol.registerBufferProtocol(protocolName, handler, function (error) {
  229. if (error) {
  230. return done(error)
  231. }
  232. $.ajax({
  233. url: protocolName + '://fake-host',
  234. cache: false,
  235. success: function (data, status, request) {
  236. assert.equal(data, text)
  237. assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
  238. done()
  239. },
  240. error: function (xhr, errorType, error) {
  241. done(error)
  242. }
  243. })
  244. })
  245. })
  246. it('sends object as response', function (done) {
  247. var handler = function (request, callback) {
  248. callback({
  249. data: buffer,
  250. mimeType: 'text/html'
  251. })
  252. }
  253. protocol.registerBufferProtocol(protocolName, handler, function (error) {
  254. if (error) {
  255. return done(error)
  256. }
  257. $.ajax({
  258. url: protocolName + '://fake-host',
  259. cache: false,
  260. success: function (data) {
  261. assert.equal(data, text)
  262. done()
  263. },
  264. error: function (xhr, errorType, error) {
  265. done(error)
  266. }
  267. })
  268. })
  269. })
  270. it('fails when sending string', function (done) {
  271. var handler = function (request, callback) {
  272. callback(text)
  273. }
  274. protocol.registerBufferProtocol(protocolName, handler, function (error) {
  275. if (error) {
  276. return done(error)
  277. }
  278. $.ajax({
  279. url: protocolName + '://fake-host',
  280. cache: false,
  281. success: function () {
  282. done('request succeeded but it should not')
  283. },
  284. error: function (xhr, errorType) {
  285. assert.equal(errorType, 'error')
  286. done()
  287. }
  288. })
  289. })
  290. })
  291. })
  292. describe('protocol.registerFileProtocol', function () {
  293. var filePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'file1')
  294. var fileContent = require('fs').readFileSync(filePath)
  295. var normalPath = path.join(__dirname, 'fixtures', 'pages', 'a.html')
  296. var normalContent = require('fs').readFileSync(normalPath)
  297. it('sends file path as response', function (done) {
  298. var handler = function (request, callback) {
  299. callback(filePath)
  300. }
  301. protocol.registerFileProtocol(protocolName, handler, function (error) {
  302. if (error) {
  303. return done(error)
  304. }
  305. $.ajax({
  306. url: protocolName + '://fake-host',
  307. cache: false,
  308. success: function (data) {
  309. assert.equal(data, String(fileContent))
  310. return done()
  311. },
  312. error: function (xhr, errorType, error) {
  313. return done(error)
  314. }
  315. })
  316. })
  317. })
  318. it('sets Access-Control-Allow-Origin', function (done) {
  319. var handler = function (request, callback) {
  320. callback(filePath)
  321. }
  322. protocol.registerFileProtocol(protocolName, handler, function (error) {
  323. if (error) {
  324. return done(error)
  325. }
  326. $.ajax({
  327. url: protocolName + '://fake-host',
  328. cache: false,
  329. success: function (data, status, request) {
  330. assert.equal(data, String(fileContent))
  331. assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*')
  332. done()
  333. },
  334. error: function (xhr, errorType, error) {
  335. done(error)
  336. }
  337. })
  338. })
  339. })
  340. it('sends object as response', function (done) {
  341. var handler = function (request, callback) {
  342. callback({
  343. path: filePath
  344. })
  345. }
  346. protocol.registerFileProtocol(protocolName, handler, function (error) {
  347. if (error) {
  348. return done(error)
  349. }
  350. $.ajax({
  351. url: protocolName + '://fake-host',
  352. cache: false,
  353. success: function (data) {
  354. assert.equal(data, String(fileContent))
  355. done()
  356. },
  357. error: function (xhr, errorType, error) {
  358. done(error)
  359. }
  360. })
  361. })
  362. })
  363. it('can send normal file', function (done) {
  364. var handler = function (request, callback) {
  365. callback(normalPath)
  366. }
  367. protocol.registerFileProtocol(protocolName, handler, function (error) {
  368. if (error) {
  369. return done(error)
  370. }
  371. $.ajax({
  372. url: protocolName + '://fake-host',
  373. cache: false,
  374. success: function (data) {
  375. assert.equal(data, String(normalContent))
  376. done()
  377. },
  378. error: function (xhr, errorType, error) {
  379. done(error)
  380. }
  381. })
  382. })
  383. })
  384. it('fails when sending unexist-file', function (done) {
  385. var fakeFilePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'not-exist')
  386. var handler = function (request, callback) {
  387. callback(fakeFilePath)
  388. }
  389. protocol.registerFileProtocol(protocolName, handler, function (error) {
  390. if (error) {
  391. return done(error)
  392. }
  393. $.ajax({
  394. url: protocolName + '://fake-host',
  395. cache: false,
  396. success: function () {
  397. done('request succeeded but it should not')
  398. },
  399. error: function (xhr, errorType) {
  400. assert.equal(errorType, 'error')
  401. done()
  402. }
  403. })
  404. })
  405. })
  406. it('fails when sending unsupported content', function (done) {
  407. var handler = function (request, callback) {
  408. callback(new Date())
  409. }
  410. protocol.registerFileProtocol(protocolName, handler, function (error) {
  411. if (error) {
  412. return done(error)
  413. }
  414. $.ajax({
  415. url: protocolName + '://fake-host',
  416. cache: false,
  417. success: function () {
  418. done('request succeeded but it should not')
  419. },
  420. error: function (xhr, errorType) {
  421. assert.equal(errorType, 'error')
  422. done()
  423. }
  424. })
  425. })
  426. })
  427. })
  428. describe('protocol.registerHttpProtocol', function () {
  429. it('sends url as response', function (done) {
  430. var server = http.createServer(function (req, res) {
  431. assert.notEqual(req.headers.accept, '')
  432. res.end(text)
  433. server.close()
  434. })
  435. server.listen(0, '127.0.0.1', function () {
  436. var port = server.address().port
  437. var url = 'http://127.0.0.1:' + port
  438. var handler = function (request, callback) {
  439. callback({
  440. url: url
  441. })
  442. }
  443. protocol.registerHttpProtocol(protocolName, handler, function (error) {
  444. if (error) {
  445. return done(error)
  446. }
  447. $.ajax({
  448. url: protocolName + '://fake-host',
  449. cache: false,
  450. success: function (data) {
  451. assert.equal(data, text)
  452. done()
  453. },
  454. error: function (xhr, errorType, error) {
  455. done(error)
  456. }
  457. })
  458. })
  459. })
  460. })
  461. it('fails when sending invalid url', function (done) {
  462. var handler = function (request, callback) {
  463. callback({
  464. url: 'url'
  465. })
  466. }
  467. protocol.registerHttpProtocol(protocolName, handler, function (error) {
  468. if (error) {
  469. return done(error)
  470. }
  471. $.ajax({
  472. url: protocolName + '://fake-host',
  473. cache: false,
  474. success: function () {
  475. done('request succeeded but it should not')
  476. },
  477. error: function (xhr, errorType) {
  478. assert.equal(errorType, 'error')
  479. done()
  480. }
  481. })
  482. })
  483. })
  484. it('fails when sending unsupported content', function (done) {
  485. var handler = function (request, callback) {
  486. callback(new Date())
  487. }
  488. protocol.registerHttpProtocol(protocolName, handler, function (error) {
  489. if (error) {
  490. return done(error)
  491. }
  492. $.ajax({
  493. url: protocolName + '://fake-host',
  494. cache: false,
  495. success: function () {
  496. done('request succeeded but it should not')
  497. },
  498. error: function (xhr, errorType) {
  499. assert.equal(errorType, 'error')
  500. done()
  501. }
  502. })
  503. })
  504. })
  505. it('works when target URL redirects', function (done) {
  506. var contents = null
  507. var server = http.createServer(function (req, res) {
  508. if (req.url === '/serverRedirect') {
  509. res.statusCode = 301
  510. res.setHeader('Location', 'http://' + req.rawHeaders[1])
  511. res.end()
  512. } else {
  513. res.end(text)
  514. }
  515. })
  516. server.listen(0, '127.0.0.1', function () {
  517. var port = server.address().port
  518. var url = `${protocolName}://fake-host`
  519. var redirectURL = `http://127.0.0.1:${port}/serverRedirect`
  520. var handler = function (request, callback) {
  521. callback({
  522. url: redirectURL
  523. })
  524. }
  525. protocol.registerHttpProtocol(protocolName, handler, function (error) {
  526. if (error) {
  527. return done(error)
  528. }
  529. contents = webContents.create({})
  530. contents.on('did-finish-load', function () {
  531. assert.equal(contents.getURL(), url)
  532. server.close()
  533. contents.destroy()
  534. done()
  535. })
  536. contents.loadURL(url)
  537. })
  538. })
  539. })
  540. })
  541. describe('protocol.isProtocolHandled', function () {
  542. it('returns true for about:', function (done) {
  543. protocol.isProtocolHandled('about', function (result) {
  544. assert.equal(result, true)
  545. done()
  546. })
  547. })
  548. it('returns true for file:', function (done) {
  549. protocol.isProtocolHandled('file', function (result) {
  550. assert.equal(result, true)
  551. done()
  552. })
  553. })
  554. it('returns true for http:', function (done) {
  555. protocol.isProtocolHandled('http', function (result) {
  556. assert.equal(result, true)
  557. done()
  558. })
  559. })
  560. it('returns true for https:', function (done) {
  561. protocol.isProtocolHandled('https', function (result) {
  562. assert.equal(result, true)
  563. done()
  564. })
  565. })
  566. it('returns false when scheme is not registered', function (done) {
  567. protocol.isProtocolHandled('no-exist', function (result) {
  568. assert.equal(result, false)
  569. done()
  570. })
  571. })
  572. it('returns true for custom protocol', function (done) {
  573. var emptyHandler = function (request, callback) {
  574. callback()
  575. }
  576. protocol.registerStringProtocol(protocolName, emptyHandler, function (error) {
  577. assert.equal(error, null)
  578. protocol.isProtocolHandled(protocolName, function (result) {
  579. assert.equal(result, true)
  580. done()
  581. })
  582. })
  583. })
  584. it('returns true for intercepted protocol', function (done) {
  585. var emptyHandler = function (request, callback) {
  586. callback()
  587. }
  588. protocol.interceptStringProtocol('http', emptyHandler, function (error) {
  589. assert.equal(error, null)
  590. protocol.isProtocolHandled('http', function (result) {
  591. assert.equal(result, true)
  592. done()
  593. })
  594. })
  595. })
  596. })
  597. describe('protocol.intercept(Any)Protocol', function () {
  598. var emptyHandler = function (request, callback) {
  599. callback()
  600. }
  601. it('throws error when scheme is already intercepted', function (done) {
  602. protocol.interceptStringProtocol('http', emptyHandler, function (error) {
  603. assert.equal(error, null)
  604. protocol.interceptBufferProtocol('http', emptyHandler, function (error) {
  605. assert.notEqual(error, null)
  606. done()
  607. })
  608. })
  609. })
  610. it('does not crash when handler is called twice', function (done) {
  611. var doubleHandler = function (request, callback) {
  612. try {
  613. callback(text)
  614. callback()
  615. } catch (error) {
  616. // Ignore error
  617. }
  618. }
  619. protocol.interceptStringProtocol('http', doubleHandler, function (error) {
  620. if (error) {
  621. return done(error)
  622. }
  623. $.ajax({
  624. url: 'http://fake-host',
  625. cache: false,
  626. success: function (data) {
  627. assert.equal(data, text)
  628. done()
  629. },
  630. error: function (xhr, errorType, error) {
  631. done(error)
  632. }
  633. })
  634. })
  635. })
  636. it('sends error when callback is called with nothing', function (done) {
  637. if (process.env.TRAVIS === 'true') {
  638. return done()
  639. }
  640. protocol.interceptBufferProtocol('http', emptyHandler, function (error) {
  641. if (error) {
  642. return done(error)
  643. }
  644. $.ajax({
  645. url: 'http://fake-host',
  646. cache: false,
  647. success: function () {
  648. done('request succeeded but it should not')
  649. },
  650. error: function (xhr, errorType) {
  651. assert.equal(errorType, 'error')
  652. done()
  653. }
  654. })
  655. })
  656. })
  657. })
  658. describe('protocol.interceptStringProtocol', function () {
  659. it('can intercept http protocol', function (done) {
  660. var handler = function (request, callback) {
  661. callback(text)
  662. }
  663. protocol.interceptStringProtocol('http', handler, function (error) {
  664. if (error) {
  665. return done(error)
  666. }
  667. $.ajax({
  668. url: 'http://fake-host',
  669. cache: false,
  670. success: function (data) {
  671. assert.equal(data, text)
  672. done()
  673. },
  674. error: function (xhr, errorType, error) {
  675. done(error)
  676. }
  677. })
  678. })
  679. })
  680. it('can set content-type', function (done) {
  681. var handler = function (request, callback) {
  682. callback({
  683. mimeType: 'application/json',
  684. data: '{"value": 1}'
  685. })
  686. }
  687. protocol.interceptStringProtocol('http', handler, function (error) {
  688. if (error) {
  689. return done(error)
  690. }
  691. $.ajax({
  692. url: 'http://fake-host',
  693. cache: false,
  694. success: function (data) {
  695. assert.equal(typeof data, 'object')
  696. assert.equal(data.value, 1)
  697. done()
  698. },
  699. error: function (xhr, errorType, error) {
  700. done(error)
  701. }
  702. })
  703. })
  704. })
  705. it('can receive post data', function (done) {
  706. var handler = function (request, callback) {
  707. var uploadData = request.uploadData[0].bytes.toString()
  708. callback({
  709. data: uploadData
  710. })
  711. }
  712. protocol.interceptStringProtocol('http', handler, function (error) {
  713. if (error) {
  714. return done(error)
  715. }
  716. $.ajax({
  717. url: 'http://fake-host',
  718. cache: false,
  719. type: 'POST',
  720. data: postData,
  721. success: function (data) {
  722. assert.deepEqual(qs.parse(data), postData)
  723. done()
  724. },
  725. error: function (xhr, errorType, error) {
  726. done(error)
  727. }
  728. })
  729. })
  730. })
  731. })
  732. describe('protocol.interceptBufferProtocol', function () {
  733. it('can intercept http protocol', function (done) {
  734. var handler = function (request, callback) {
  735. callback(new Buffer(text))
  736. }
  737. protocol.interceptBufferProtocol('http', handler, function (error) {
  738. if (error) {
  739. return done(error)
  740. }
  741. $.ajax({
  742. url: 'http://fake-host',
  743. cache: false,
  744. success: function (data) {
  745. assert.equal(data, text)
  746. done()
  747. },
  748. error: function (xhr, errorType, error) {
  749. done(error)
  750. }
  751. })
  752. })
  753. })
  754. it('can receive post data', function (done) {
  755. var handler = function (request, callback) {
  756. var uploadData = request.uploadData[0].bytes
  757. callback(uploadData)
  758. }
  759. protocol.interceptBufferProtocol('http', handler, function (error) {
  760. if (error) {
  761. return done(error)
  762. }
  763. $.ajax({
  764. url: 'http://fake-host',
  765. cache: false,
  766. type: 'POST',
  767. data: postData,
  768. success: function (data) {
  769. assert.equal(data, $.param(postData))
  770. done()
  771. },
  772. error: function (xhr, errorType, error) {
  773. done(error)
  774. }
  775. })
  776. })
  777. })
  778. })
  779. describe('protocol.interceptHttpProtocol', function () {
  780. it('can send POST request', function (done) {
  781. var server = http.createServer(function (req, res) {
  782. var body = ''
  783. req.on('data', function (chunk) {
  784. body += chunk
  785. })
  786. req.on('end', function () {
  787. res.end(body)
  788. })
  789. server.close()
  790. })
  791. server.listen(0, '127.0.0.1', function () {
  792. var port = server.address().port
  793. var url = 'http://127.0.0.1:' + port
  794. var handler = function (request, callback) {
  795. var data = {
  796. url: url,
  797. method: 'POST',
  798. uploadData: {
  799. contentType: 'application/x-www-form-urlencoded',
  800. data: request.uploadData[0].bytes.toString()
  801. },
  802. session: null
  803. }
  804. callback(data)
  805. }
  806. protocol.interceptHttpProtocol('http', handler, function (error) {
  807. if (error) {
  808. return done(error)
  809. }
  810. $.ajax({
  811. url: 'http://fake-host',
  812. cache: false,
  813. type: 'POST',
  814. data: postData,
  815. success: function (data) {
  816. assert.deepEqual(qs.parse(data), postData)
  817. done()
  818. },
  819. error: function (xhr, errorType, error) {
  820. done(error)
  821. }
  822. })
  823. })
  824. })
  825. })
  826. it('can use custom session', function (done) {
  827. const customSession = session.fromPartition('custom-ses', {
  828. cache: false
  829. })
  830. customSession.webRequest.onBeforeRequest(function (details, callback) {
  831. assert.equal(details.url, 'http://fake-host/')
  832. callback({cancel: true})
  833. })
  834. const handler = function (request, callback) {
  835. callback({
  836. url: request.url,
  837. session: customSession
  838. })
  839. }
  840. protocol.interceptHttpProtocol('http', handler, function (error) {
  841. if (error) {
  842. return done(error)
  843. }
  844. fetch('http://fake-host').then(function () {
  845. done('request succeeded but it should not')
  846. }).catch(function () {
  847. customSession.webRequest.onBeforeRequest(null)
  848. done()
  849. })
  850. })
  851. })
  852. })
  853. describe('protocol.uninterceptProtocol', function () {
  854. it('returns error when scheme does not exist', function (done) {
  855. protocol.uninterceptProtocol('not-exist', function (error) {
  856. assert.notEqual(error, null)
  857. done()
  858. })
  859. })
  860. it('returns error when scheme is not intercepted', function (done) {
  861. protocol.uninterceptProtocol('http', function (error) {
  862. assert.notEqual(error, null)
  863. done()
  864. })
  865. })
  866. })
  867. describe('protocol.registerStandardSchemes', function () {
  868. const standardScheme = remote.getGlobal('standardScheme')
  869. const origin = standardScheme + '://fake-host'
  870. const imageURL = origin + '/test.png'
  871. const filePath = path.join(__dirname, 'fixtures', 'pages', 'b.html')
  872. const fileContent = '<img src="/test.png" />'
  873. var w = null
  874. var success = null
  875. beforeEach(function () {
  876. w = new BrowserWindow({show: false})
  877. success = false
  878. })
  879. afterEach(function (done) {
  880. protocol.unregisterProtocol(standardScheme, function () {
  881. closeWindow(w).then(function () {
  882. w = null
  883. done()
  884. })
  885. })
  886. })
  887. it('resolves relative resources', function (done) {
  888. var handler = function (request, callback) {
  889. if (request.url === imageURL) {
  890. success = true
  891. callback()
  892. } else {
  893. callback(filePath)
  894. }
  895. }
  896. protocol.registerFileProtocol(standardScheme, handler, function (error) {
  897. if (error) {
  898. return done(error)
  899. }
  900. w.webContents.on('did-finish-load', function () {
  901. assert(success)
  902. done()
  903. })
  904. w.loadURL(origin)
  905. })
  906. })
  907. it('resolves absolute resources', function (done) {
  908. var handler = function (request, callback) {
  909. if (request.url === imageURL) {
  910. success = true
  911. callback()
  912. } else {
  913. callback({
  914. data: fileContent,
  915. mimeType: 'text/html'
  916. })
  917. }
  918. }
  919. protocol.registerStringProtocol(standardScheme, handler, function (error) {
  920. if (error) {
  921. return done(error)
  922. }
  923. w.webContents.on('did-finish-load', function () {
  924. assert(success)
  925. done()
  926. })
  927. w.loadURL(origin)
  928. })
  929. })
  930. it('can have fetch working in it', function (done) {
  931. const content = '<html><script>fetch("http://github.com")</script></html>'
  932. const handler = function (request, callback) {
  933. callback({data: content, mimeType: 'text/html'})
  934. }
  935. protocol.registerStringProtocol(standardScheme, handler, function (error) {
  936. if (error) return done(error)
  937. w.webContents.on('crashed', function () {
  938. done('WebContents crashed')
  939. })
  940. w.webContents.on('did-finish-load', function () {
  941. done()
  942. })
  943. w.loadURL(origin)
  944. })
  945. })
  946. it('can access files through the FileSystem API', function (done) {
  947. let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
  948. const handler = function (request, callback) {
  949. callback({path: filePath})
  950. }
  951. protocol.registerFileProtocol(standardScheme, handler, function (error) {
  952. if (error) return done(error)
  953. w.loadURL(origin)
  954. })
  955. ipcMain.once('file-system-error', (event, err) => done(err))
  956. ipcMain.once('file-system-write-end', () => done())
  957. })
  958. it('registers secure, when {secure: true}', function (done) {
  959. // the CacheStorage API will only work if secure == true
  960. let filePath = path.join(__dirname, 'fixtures', 'pages', 'cache-storage.html')
  961. const handler = function (request, callback) {
  962. callback({path: filePath})
  963. }
  964. ipcMain.once('success', () => done())
  965. ipcMain.once('failure', (event, err) => done(err))
  966. protocol.registerFileProtocol(standardScheme, handler, function (error) {
  967. if (error) return done(error)
  968. w.loadURL(origin)
  969. })
  970. })
  971. })
  972. })