|
| 1 | +var DEFAULT_TIMEOUT = 20000; |
| 2 | + |
| 3 | +// creates a new document in an <iframe> and runs |
| 4 | +// a WombatJS test case in it. |
| 5 | +// |
| 6 | +// A new <iframe> is used for each test so that each |
| 7 | +// case is run with fresh Document and Window objects, |
| 8 | +// since Wombat monkey-patches many Document and Window |
| 9 | +// functions |
| 10 | +// |
| 11 | +function runWombatTest(testCase, done) { |
| 12 | + // create an <iframe> |
| 13 | + var testFrame = document.createElement('iframe'); |
| 14 | + testFrame.src = '/base/karma-tests/dummy.html'; |
| 15 | + document.body.appendChild(testFrame); |
| 16 | + |
| 17 | + testFrame.contentWindow.addEventListener('load', function () { |
| 18 | + var testDocument = testFrame.contentDocument; |
| 19 | + |
| 20 | + function runFunctionInIFrame(func) { |
| 21 | + testFrame.contentWindow.eval('(' + func.toString() + ')()'); |
| 22 | + } |
| 23 | + |
| 24 | + // expose an error reporting function to the <iframe> |
| 25 | + window.reportError = function(ex) { |
| 26 | + done(new Error(ex)); |
| 27 | + }; |
| 28 | + |
| 29 | + // expose utility methods for assertion testing in tests. |
| 30 | + // (We used to expose chai asserts here but Karma's default |
| 31 | + // error reporter replaces URLs in exception messages with |
| 32 | + // the corresponding file paths, which is unhelpful for us |
| 33 | + // since assert.equal() will often be called with URLs in our tests) |
| 34 | + window.assert = { |
| 35 | + equal: function (a, b) { |
| 36 | + if (a !== b) { |
| 37 | + console.error('Mismatch between', a, 'and', b); |
| 38 | + throw new Error('AssertionError'); |
| 39 | + } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + runFunctionInIFrame(function () { |
| 44 | + // re-assign the iframe's console object to the parent window's |
| 45 | + // console so that messages are intercepted by Karma |
| 46 | + // and output to wherever it is configured to send |
| 47 | + // console logs (typically stdout) |
| 48 | + console = window.parent.console; |
| 49 | + window.onerror = function (message, url, line, col, error) { |
| 50 | + if (error) { |
| 51 | + console.log(error.stack); |
| 52 | + } |
| 53 | + reportError(new Error(message)); |
| 54 | + }; |
| 55 | + |
| 56 | + // expose chai's assertion testing API to the test script |
| 57 | + window.assert = window.parent.assert; |
| 58 | + window.reportError = window.parent.reportError; |
| 59 | + |
| 60 | + // helpers which check whether DOM property overrides are supported |
| 61 | + // in the current browser |
| 62 | + window.domTests = { |
| 63 | + areDOMPropertiesConfigurable: function () { |
| 64 | + var descriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'baseURI'); |
| 65 | + if (descriptor && !descriptor.configurable) { |
| 66 | + return false; |
| 67 | + } else { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + }); |
| 73 | + |
| 74 | + try { |
| 75 | + runFunctionInIFrame(testCase.initScript); |
| 76 | + } catch (e) { |
| 77 | + throw new Error('Configuring Wombat failed: ' + e.toString()); |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + testFrame.contentWindow.eval(testCase.wombatScript); |
| 82 | + runFunctionInIFrame(function () { |
| 83 | + new window._WBWombat(wbinfo); |
| 84 | + }); |
| 85 | + } catch (e) { |
| 86 | + throw new Error('Initializing WombatJS failed: ' + e.toString()); |
| 87 | + } |
| 88 | + |
| 89 | + if (testCase.html) { |
| 90 | + testDocument.body.innerHTML = testCase.html; |
| 91 | + } |
| 92 | + |
| 93 | + if (testCase.testScript) { |
| 94 | + try { |
| 95 | + runFunctionInIFrame(testCase.testScript); |
| 96 | + } catch (e) { |
| 97 | + throw new Error('Test script failed: ' + e.toString()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + testFrame.remove(); |
| 102 | + done(); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +describe('WombatJS', function () { |
| 107 | + this.timeout(DEFAULT_TIMEOUT); |
| 108 | + |
| 109 | + var wombatScript; |
| 110 | + |
| 111 | + before(function (done) { |
| 112 | + // load the source of the WombatJS content |
| 113 | + // rewriting script |
| 114 | + var req = new XMLHttpRequest(); |
| 115 | + req.open('GET', '/base/pywb/static/wombat.js'); |
| 116 | + req.onload = function () { |
| 117 | + wombatScript = req.responseText; |
| 118 | + done(); |
| 119 | + }; |
| 120 | + req.send(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should load', function (done) { |
| 124 | + runWombatTest({ |
| 125 | + initScript: function () { |
| 126 | + wbinfo = { |
| 127 | + wombat_opts: {}, |
| 128 | + }; |
| 129 | + }, |
| 130 | + wombatScript: wombatScript, |
| 131 | + }, done); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('anchor rewriting', function () { |
| 135 | + var config; |
| 136 | + beforeEach(function () { |
| 137 | + config = { |
| 138 | + initScript: function () { |
| 139 | + wbinfo = { |
| 140 | + wombat_opts: {}, |
| 141 | + prefix: window.location.origin, |
| 142 | + wombat_ts: '', |
| 143 | + }; |
| 144 | + }, |
| 145 | + wombatScript: wombatScript, |
| 146 | + html: '<a href="foobar.html" id="link">A link</a>', |
| 147 | + }; |
| 148 | + }); |
| 149 | + |
| 150 | + it('should rewrite links in dynamically injected <a> tags', function (done) { |
| 151 | + config.testScript = function () { |
| 152 | + if (domTests.areDOMPropertiesConfigurable()) { |
| 153 | + var link = document.getElementById('link'); |
| 154 | + assert.equal(link.href, 'http:///base/karma-tests/foobar.html'); |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + runWombatTest(config, done); |
| 159 | + }); |
| 160 | + |
| 161 | + it('toString() should return the rewritten URL', function (done) { |
| 162 | + config.testScript = function () { |
| 163 | + if (domTests.areDOMPropertiesConfigurable()) { |
| 164 | + var link = document.getElementById('link'); |
| 165 | + assert.equal(link.href, link.toString()); |
| 166 | + } |
| 167 | + }; |
| 168 | + runWombatTest(config, done); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('base URL overrides', function () { |
| 173 | + it('document.baseURI should return the original URL', function (done) { |
| 174 | + runWombatTest({ |
| 175 | + initScript: function () { |
| 176 | + wbinfo = { |
| 177 | + wombat_opts: {}, |
| 178 | + prefix: window.location.origin, |
| 179 | + wombat_ts: '', |
| 180 | + }; |
| 181 | + }, |
| 182 | + wombatScript: wombatScript, |
| 183 | + testScript: function () { |
| 184 | + var baseURI = document.baseURI; |
| 185 | + if (typeof baseURI !== 'string') { |
| 186 | + throw new Error('baseURI is not a string'); |
| 187 | + } |
| 188 | + if (domTests.areDOMPropertiesConfigurable()) { |
| 189 | + assert.equal(baseURI, 'http:///base/karma-tests/dummy.html'); |
| 190 | + } |
| 191 | + }, |
| 192 | + }, done); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should allow base.href to be assigned', function (done) { |
| 196 | + runWombatTest({ |
| 197 | + initScript: function () { |
| 198 | + wbinfo = { |
| 199 | + wombat_opts: {}, |
| 200 | + }; |
| 201 | + }, |
| 202 | + wombatScript: wombatScript, |
| 203 | + testScript: function () { |
| 204 | + 'use strict'; |
| 205 | + var baseElement = document.createElement('base'); |
| 206 | + baseElement.href = 'http://foobar.com/base'; |
| 207 | + assert.equal(baseElement.href, 'http://foobar.com/base'); |
| 208 | + }, |
| 209 | + }, done); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments