api-web-frame-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const assert = require('assert')
  2. const path = require('path')
  3. const {closeWindow} = require('./window-helpers')
  4. const {remote, webFrame} = require('electron')
  5. const {BrowserWindow, protocol, ipcMain} = remote
  6. /* Most of the APIs here don't use standard callbacks */
  7. /* eslint-disable standard/no-callback-literal */
  8. describe('webFrame module', function () {
  9. var fixtures = path.resolve(__dirname, 'fixtures')
  10. var w = null
  11. afterEach(function () {
  12. return closeWindow(w).then(function () { w = null })
  13. })
  14. describe('webFrame.registerURLSchemeAsPrivileged', function () {
  15. it('supports fetch api by default', function (done) {
  16. var url = 'file://' + fixtures + '/assets/logo.png'
  17. window.fetch(url).then(function (response) {
  18. assert(response.ok)
  19. done()
  20. }).catch(function (err) {
  21. done('unexpected error : ' + err)
  22. })
  23. })
  24. it('allows CORS requests by default', function (done) {
  25. allowsCORSRequests(200, `<html>
  26. <script>
  27. const {ipcRenderer, webFrame} = require('electron')
  28. webFrame.registerURLSchemeAsPrivileged('cors1')
  29. fetch('cors1://myhost').then(function (response) {
  30. ipcRenderer.send('response', response.status)
  31. }).catch(function (response) {
  32. ipcRenderer.send('response', 'failed')
  33. })
  34. </script>
  35. </html>`, done)
  36. })
  37. it('allows CORS and fetch requests when specified', function (done) {
  38. allowsCORSRequests(200, `<html>
  39. <script>
  40. const {ipcRenderer, webFrame} = require('electron')
  41. webFrame.registerURLSchemeAsPrivileged('cors2', { supportFetchAPI: true, corsEnabled: true })
  42. fetch('cors2://myhost').then(function (response) {
  43. ipcRenderer.send('response', response.status)
  44. }).catch(function (response) {
  45. ipcRenderer.send('response', 'failed')
  46. })
  47. </script>
  48. </html>`, done)
  49. })
  50. it('allows CORS and fetch requests when half-specified', function (done) {
  51. allowsCORSRequests(200, `<html>
  52. <script>
  53. const {ipcRenderer, webFrame} = require('electron')
  54. webFrame.registerURLSchemeAsPrivileged('cors3', { supportFetchAPI: true })
  55. fetch('cors3://myhost').then(function (response) {
  56. ipcRenderer.send('response', response.status)
  57. }).catch(function (response) {
  58. ipcRenderer.send('response', 'failed')
  59. })
  60. </script>
  61. </html>`, done)
  62. })
  63. it('disallows CORS, but allows fetch requests, when specified', function (done) {
  64. allowsCORSRequests('failed', `<html>
  65. <script>
  66. const {ipcRenderer, webFrame} = require('electron')
  67. webFrame.registerURLSchemeAsPrivileged('cors4', { supportFetchAPI: true, corsEnabled: false })
  68. fetch('cors4://myhost').then(function (response) {
  69. ipcRenderer.send('response', response.status)
  70. }).catch(function (response) {
  71. ipcRenderer.send('response', 'failed')
  72. })
  73. </script>
  74. </html>`, done)
  75. })
  76. it('allows CORS, but disallows fetch requests, when specified', function (done) {
  77. allowsCORSRequests('failed', `<html>
  78. <script>
  79. const {ipcRenderer, webFrame} = require('electron')
  80. webFrame.registerURLSchemeAsPrivileged('cors5', { supportFetchAPI: false, corsEnabled: true })
  81. fetch('cors5://myhost').then(function (response) {
  82. ipcRenderer.send('response', response.status)
  83. }).catch(function (response) {
  84. ipcRenderer.send('response', 'failed')
  85. })
  86. </script>
  87. </html>`, done)
  88. })
  89. var runNumber = 1
  90. function allowsCORSRequests (expected, content, done) {
  91. const standardScheme = remote.getGlobal('standardScheme') + runNumber
  92. const corsScheme = 'cors' + runNumber
  93. runNumber++
  94. const url = standardScheme + '://fake-host'
  95. w = new BrowserWindow({show: false})
  96. after(function (done) {
  97. protocol.unregisterProtocol(corsScheme, function () {
  98. protocol.unregisterProtocol(standardScheme, function () {
  99. done()
  100. })
  101. })
  102. })
  103. const handler = function (request, callback) {
  104. callback({data: content, mimeType: 'text/html'})
  105. }
  106. protocol.registerStringProtocol(standardScheme, handler, function (error) {
  107. if (error) return done(error)
  108. })
  109. protocol.registerStringProtocol(corsScheme, function (request, callback) {
  110. callback('')
  111. }, function (error) {
  112. if (error) return done(error)
  113. ipcMain.once('response', function (event, status) {
  114. assert.equal(status, expected)
  115. done()
  116. })
  117. w.loadURL(url)
  118. })
  119. }
  120. })
  121. it('supports setting the visual and layout zoom level limits', function () {
  122. assert.doesNotThrow(function () {
  123. webFrame.setVisualZoomLevelLimits(1, 50)
  124. webFrame.setLayoutZoomLevelLimits(0, 25)
  125. })
  126. })
  127. })