Browse Source

Add failing spec for targetOrigin handling

Kevin Sawicki 8 years ago
parent
commit
3c58d50f87
2 changed files with 52 additions and 0 deletions
  1. 34 0
      spec/chromium-spec.js
  2. 18 0
      spec/fixtures/pages/window-opener-targetOrigin.html

+ 34 - 0
spec/chromium-spec.js

@@ -1,4 +1,5 @@
 const assert = require('assert')
+const fs = require('fs')
 const http = require('http')
 const path = require('path')
 const ws = require('ws')
@@ -618,6 +619,39 @@ describe('chromium feature', function () {
       })
       document.body.appendChild(webview)
     })
+
+    describe('targetOrigin argument', function () {
+      let serverURL
+      let server
+
+      beforeEach(function (done) {
+        server = http.createServer(function (req, res) {
+          res.writeHead(200)
+          const filePath = path.join(fixtures, 'pages', 'window-opener-targetOrigin.html')
+          res.end(fs.readFileSync(filePath, 'utf8'))
+        })
+        server.listen(0, '127.0.0.1', function () {
+          serverURL = `http://127.0.0.1:${server.address().port}`
+          done()
+        })
+      })
+
+      afterEach(function () {
+        server.close()
+      })
+
+      it('delivers messages that match the origin', function (done) {
+        let b
+        listener = function (event) {
+          window.removeEventListener('message', listener)
+          b.close()
+          assert.equal(event.data, 'second message')
+          done()
+        }
+        window.addEventListener('message', listener)
+        b = window.open(serverURL, '', 'show=no')
+      })
+    })
   })
 
   describe('creating a Uint8Array under browser side', function () {

+ 18 - 0
spec/fixtures/pages/window-opener-targetOrigin.html

@@ -0,0 +1,18 @@
+<html>
+<body>
+<script type="text/javascript" charset="utf-8">
+  const url = require('url')
+  if (url.parse(window.location.href, true).query.opened != null) {
+    // Ensure origin are properly checked by removing a single character from the end
+    window.opener.postMessage('first message', window.location.origin.substring(0, window.location.origin.length - 1))
+    window.opener.postMessage('second message', window.location.origin)
+  } else {
+    const opened = window.open(`${window.location.href}?opened=true`)
+    window.addEventListener('message', function (event) {
+      window.opener.postMessage(event.data, '*')
+      opened.close()
+    })
+  }
+</script>
+</body>
+</html>