|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var utils = require("../../../lib/server/proxy-utils"); |
| 4 | +var assert = require("chai").assert; |
| 5 | +var fs = require("fs"); |
| 6 | + |
| 7 | +describe("Rewriting Domains", function() { |
| 8 | + |
| 9 | + var proxyUrl = "192.168.0.4:3002"; |
| 10 | + |
| 11 | + describe("when rewriting 'example.com' links", function() { |
| 12 | + |
| 13 | + var regex, fn; |
| 14 | + var testRegex; |
| 15 | + before(function() { |
| 16 | + var host = "192.168.0.4:3002"; |
| 17 | + var rewrite = utils.rewriteLinks({hostname: "example.com", port: 80}, proxyUrl); |
| 18 | + var bound = rewrite.fn.bind(null, {headers:{host:host}}, {}); |
| 19 | + regex = rewrite.match; |
| 20 | + fn = rewrite.fn; |
| 21 | + testRegex = function (string) { |
| 22 | + return string.replace(rewrite.match, bound); |
| 23 | + }; |
| 24 | + }); |
| 25 | + |
| 26 | + it("should not replace domains that are not inside attribute", function() { |
| 27 | + var actual = testRegex("<h1>example.com</h1>"); |
| 28 | + var expected = "<h1>example.com</h1>"; |
| 29 | + assert.equal(expected, actual); |
| 30 | + }); |
| 31 | + it("should replace CSS LINK", function() { |
| 32 | + var actual = testRegex("<link href='http://example.com/css/styles'>example.com</link>"); |
| 33 | + var expected = "<link href='//192.168.0.4:3002/css/styles'>example.com</link>"; |
| 34 | + assert.equal(actual, expected); |
| 35 | + }); |
| 36 | + it("should use the regex to replace links (2)", function() { |
| 37 | + var actual = testRegex("<a href='http://example.com'></a>"); |
| 38 | + var expected = "<a href='//192.168.0.4:3002'></a>"; |
| 39 | + assert.equal(actual, expected); |
| 40 | + }); |
| 41 | + it("should use the regex to replace links (23)", function() { |
| 42 | + var actual = testRegex("<a href='http://example.com/sub/dir'></a>"); |
| 43 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 44 | + assert.equal(actual, expected); |
| 45 | + }); |
| 46 | + it("should use the regex to replace links (3)", function() { |
| 47 | + var actual = testRegex("<a href='http://example.com/sub/dir'></a>"); |
| 48 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 49 | + assert.equal(actual, expected); |
| 50 | + }); |
| 51 | + it("should use the regex to replace links (4)", function() { |
| 52 | + var actual = testRegex("<a href='https://example.com/sub/dir'></a>"); |
| 53 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 54 | + assert.equal(actual, expected); |
| 55 | + }); |
| 56 | + it("should use the regex to replace links (5)", function() { |
| 57 | + var actual = testRegex("<a href='/example.com/sub/dir'></a>"); |
| 58 | + var expected = "<a href='/192.168.0.4:3002/sub/dir'></a>"; |
| 59 | + assert.equal(actual, expected); |
| 60 | + }); |
| 61 | + it("should use the regex to replace links (6)", function() { |
| 62 | + var actual = testRegex("<a href='example.com/sub/dir'></a>"); |
| 63 | + var expected = "<a href='192.168.0.4:3002/sub/dir'></a>"; |
| 64 | + assert.equal(actual, expected); |
| 65 | + }); |
| 66 | + it("should use the regex to replace links (6)", function() { |
| 67 | + /*jshint ignore:start*/ |
| 68 | + var actual = testRegex('<a href="http://example.com" class="active" title="Home">Home</a><a href="http://example.com/information" class="" title="Info">Info</a>'); |
| 69 | + var expected = '<a href="//192.168.0.4:3002" class="active" title="Home">Home</a><a href="//192.168.0.4:3002/information" class="" title="Info">Info</a>'; |
| 70 | + assert.equal(actual, expected); |
| 71 | + /*jshint ignore:end*/ |
| 72 | + }); |
| 73 | + it("should use the regex to replace links (7)", function() { |
| 74 | + var actual = testRegex("<a href='http://example.com/sub/dir/example.com/css/styles.css'></a><a href='http://example.com/sub/dir/example.com/css/styles.css'></a>"); |
| 75 | + var expected = "<a href='//192.168.0.4:3002/sub/dir/example.com/css/styles.css'></a><a href='//192.168.0.4:3002/sub/dir/example.com/css/styles.css'></a>"; |
| 76 | + assert.equal(actual, expected); |
| 77 | + }); |
| 78 | + }); |
| 79 | + describe("when rewriting 'localhost:8000' links", function() { |
| 80 | + |
| 81 | + var regex, fn; |
| 82 | + var testRegex; |
| 83 | + before(function() { |
| 84 | + var host = "192.168.0.4:3002"; |
| 85 | + var rewrite = utils.rewriteLinks({hostname: "localhost:8000", port: 80}, proxyUrl); |
| 86 | + var bound = rewrite.fn.bind(null, {headers:{host:host}}, {}); |
| 87 | + regex = rewrite.match; |
| 88 | + fn = rewrite.fn; |
| 89 | + testRegex = function (string) { |
| 90 | + return string.replace(rewrite.match, bound); |
| 91 | + }; |
| 92 | + }); |
| 93 | + it("should use the regex to replace links (1)", function() { |
| 94 | + var actual = testRegex("<a href='http://localhost:8000'></a>"); |
| 95 | + var expected = "<a href='//192.168.0.4:3002'></a>"; |
| 96 | + assert.equal(actual, expected); |
| 97 | + }); |
| 98 | + it("should use the regex to replace links (1)", function() { |
| 99 | + var actual = testRegex("<a href='http://localhost:8000'></a>"); |
| 100 | + var expected = "<a href='//192.168.0.4:3002'></a>"; |
| 101 | + assert.equal(actual, expected); |
| 102 | + }); |
| 103 | + it("should use the regex to replace links (2)", function() { |
| 104 | + var actual = testRegex("<a href='http://localhost:8000/sub/dir'></a>"); |
| 105 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 106 | + assert.equal(actual, expected); |
| 107 | + }); |
| 108 | + it("should use the regex to replace links (3)", function() { |
| 109 | + var actual = testRegex("<a href='http://localhost:8000/sub/dir'></a>"); |
| 110 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 111 | + assert.equal(actual, expected); |
| 112 | + }); |
| 113 | + it("should use the regex to replace links (4)", function() { |
| 114 | + var actual = testRegex("<a href='https://localhost:8000/sub/dir'></a>"); |
| 115 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 116 | + assert.equal(actual, expected); |
| 117 | + }); |
| 118 | + it("should use the regex to replace links (5)", function() { |
| 119 | + var actual = testRegex("<a href='/localhost:8000/sub/dir'></a>"); |
| 120 | + var expected = "<a href='/192.168.0.4:3002/sub/dir'></a>"; |
| 121 | + assert.equal(actual, expected); |
| 122 | + }); |
| 123 | + it("should use the regex to replace links (6)", function() { |
| 124 | + var actual = testRegex("<a href='localhost:8000/sub/dir'></a>"); |
| 125 | + var expected = "<a href='//192.168.0.4:3002/sub/dir'></a>"; |
| 126 | + assert.equal(actual, expected); |
| 127 | + }); |
| 128 | + it("should use the regex to replace links that contain hashes (1)", function() { |
| 129 | + var actual = testRegex("<a href='http://localhost:8000/sub/dir/?search=some#shane'></a>"); |
| 130 | + var expected = "<a href='//192.168.0.4:3002/sub/dir/?search=some#shane'></a>"; |
| 131 | + assert.equal(actual, expected); |
| 132 | + }); |
| 133 | + it("should use the regex to replace links that contain hashes (2)", function() { |
| 134 | + var actual = testRegex("<a href='http://localhost:8000/sub/dir/#search'></a>"); |
| 135 | + var expected = "<a href='//192.168.0.4:3002/sub/dir/#search'></a>"; |
| 136 | + assert.equal(actual, expected); |
| 137 | + }); |
| 138 | + it("should use the regex to replace links that contain hashes (2)", function() { |
| 139 | + var getBase = function (host) { |
| 140 | + return ` |
| 141 | +<ul class="navigation__items"> |
| 142 | + <li class="navigation__items__page"> |
| 143 | + <a href="${host}/careers/?number=4">« Previous</a> |
| 144 | + </li> |
| 145 | + <li class="navigation__items__page"> |
| 146 | + <a href="${host}/careers/#search">1</a> |
| 147 | + </li> |
| 148 | + <li class="navigation__items__page"> |
| 149 | + <a href="${host}/careers/?number=2#search">2</a> |
| 150 | + </li> |
| 151 | + <li class="navigation__items__page"> |
| 152 | + <a href="${host}/careers/?number=3#search">3</a> |
| 153 | + </li> |
| 154 | + <li class="navigation__items__page"> |
| 155 | + <a href="${host}/careers/?number=4#search">4</a> |
| 156 | + </li> |
| 157 | + <li class="navigation__items__page navigation__items__page--active"> |
| 158 | + <a href="${host}/careers/?number=5#search">5</a> |
| 159 | + </li> |
| 160 | +</ul> |
| 161 | + `; |
| 162 | + }; |
| 163 | + var original = getBase("http://www.example.local.colinr.com"); |
| 164 | + var expected = getBase("//" + proxyUrl); |
| 165 | + var rewrite = utils.rewriteLinks({hostname: "www.example.local.colinr.com", port: 80}, proxyUrl); |
| 166 | + var bound = rewrite.fn.bind(null, {headers:{host:proxyUrl}}, {}); |
| 167 | + var actual = original.replace(rewrite.match, bound); |
| 168 | + assert.equal(actual, expected); |
| 169 | + }); |
| 170 | + it("should use the regex to replace links that contain port + ", function() { |
| 171 | + var input = ` |
| 172 | +<!--//<a href="http://example.com:1234/foo">Link 1</a>--> |
| 173 | +<a href="http://example.com.gov/foo">Link 1</a> |
| 174 | +`; |
| 175 | + var expected = ` |
| 176 | +<!--//<a href="//${proxyUrl}/foo">Link 1</a>--> |
| 177 | +<a href="http://example.com.gov/foo">Link 1</a> |
| 178 | +`; |
| 179 | + |
| 180 | + var rewrite = utils.rewriteLinks({hostname: "example.com", port: 1234}, proxyUrl); |
| 181 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 182 | + var actual = input.replace(rewrite.match, bound); |
| 183 | + |
| 184 | + assert.equal(actual, expected); |
| 185 | + }); |
| 186 | + it("should use the regex to replace links that contain port + ", function() { |
| 187 | + var input = ` |
| 188 | +<!doctype html> |
| 189 | +<html><body> |
| 190 | +<p>This is an experiment.dev</p> |
| 191 | +<p><img src="/images/experiment.dev/example.jpg"></p> |
| 192 | +<!-- This experiment.dev should prove interesting --> |
| 193 | +<p><a href="http://experiment.dev/example.html">Link</a></p> |
| 194 | +</body></html> |
| 195 | +`; |
| 196 | + var expected = ` |
| 197 | +<!doctype html> |
| 198 | +<html><body> |
| 199 | +<p>This is an experiment.dev</p> |
| 200 | +<p><img src="/images/experiment.dev/example.jpg"></p> |
| 201 | +<!-- This experiment.dev should prove interesting --> |
| 202 | +<p><a href="//${proxyUrl}/example.html">Link</a></p> |
| 203 | +</body></html> |
| 204 | +`; |
| 205 | + |
| 206 | + var rewrite = utils.rewriteLinks({hostname: "experiment.dev"}, proxyUrl); |
| 207 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 208 | + var actual = input.replace(rewrite.match, bound); |
| 209 | + assert.equal(actual, expected); |
| 210 | + }); |
| 211 | + it("should use the regex to replace links that contain port + ", function() { |
| 212 | + var input = ` |
| 213 | +<link href="/sites/default/themes/mclinic/css/styles.css" media="screen" rel="stylesheet"> |
| 214 | +`; |
| 215 | + |
| 216 | + var rewrite = utils.rewriteLinks({hostname: "mclinic"}, proxyUrl); |
| 217 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 218 | + var actual = input.replace(rewrite.match, bound); |
| 219 | + assert.equal(actual, input); |
| 220 | + }); |
| 221 | + it("should not replace when host + subdomain ", function() { |
| 222 | + var input = `<a href="http://assets.cdn.example.com:1234/foo">Link 1</a>`; |
| 223 | + var rewrite = utils.rewriteLinks({hostname: "example.com", port: 1234}, proxyUrl); |
| 224 | + var actual = input.replace(rewrite.match, rewrite.fn); |
| 225 | + assert.equal(actual, input); |
| 226 | + }); |
| 227 | + it("should not remove trailing slash", function() { |
| 228 | + var input = `<a href="http://example.com:1234/foo/">Link 1</a>`; |
| 229 | + var expected = `<a href="//${proxyUrl}/foo/">Link 1</a>`; |
| 230 | + var rewrite = utils.rewriteLinks({hostname: "example.com", port: 1234}, proxyUrl); |
| 231 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 232 | + var actual = input.replace(rewrite.match, bound); |
| 233 | + assert.equal(actual, expected); |
| 234 | + }); |
| 235 | + it("should not greedy match when not inside attr - with paths", function() { |
| 236 | + var input = `<a href="https://example.com/calendar/2015/06/24/20471/">https://example.com/calendar/2015/06/24/20471/</a></p></div></li></ol></td><td class="cal-current-month">`; |
| 237 | + var expected = `<a href="//${proxyUrl}/calendar/2015/06/24/20471/">//${proxyUrl}/calendar/2015/06/24/20471/</a></p></div></li></ol></td><td class="cal-current-month">`; |
| 238 | + var rewrite = utils.rewriteLinks({hostname: "example.com"}, proxyUrl); |
| 239 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 240 | + var actual = input.replace(rewrite.match, bound); |
| 241 | + assert.equal(actual, expected); |
| 242 | + }); |
| 243 | + it("should not greedy match when not inside attr", function() { |
| 244 | + var input = `<a href="https://example.com/">https://example.com/</a></p></div></li></ol></td><td class="cal-current-month">`; |
| 245 | + var expected = `<a href="//${proxyUrl}/">//${proxyUrl}/</a></p></div></li></ol></td><td class="cal-current-month">`; |
| 246 | + var rewrite = utils.rewriteLinks({hostname: "example.com"}, proxyUrl); |
| 247 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 248 | + var actual = input.replace(rewrite.match, bound); |
| 249 | + assert.equal(actual, expected); |
| 250 | + }); |
| 251 | + it("handles escaped urls correctly", function() { |
| 252 | + const input = fs.readFileSync("test/fixtures/escaped.1.html", "utf8"); |
| 253 | + const expected = fs.readFileSync("test/fixtures/escaped.1.expected.html", "utf8"); |
| 254 | + var rewrite = utils.rewriteLinks({hostname: "demo.l5"}, proxyUrl); |
| 255 | + var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {}); |
| 256 | + var actual = input.replace(rewrite.match, bound); |
| 257 | + assert.equal(actual, expected); |
| 258 | + }); |
| 259 | + }); |
| 260 | +}); |
0 commit comments