api-web-frame-spec.js 4.8 KB

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