|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const assert = require("assert"); |
| 4 | +const loaderUtils = require("../"); |
| 5 | + |
| 6 | +function ExpectedError(regex) { |
| 7 | + this.regex = regex; |
| 8 | +} |
| 9 | +ExpectedError.prototype.matches = function(err) { |
| 10 | + return this.regex.test(err.message); |
| 11 | +}; |
| 12 | + |
| 13 | +describe("isUrlRequest()", () => { |
| 14 | + [ |
| 15 | + // without root |
| 16 | + [["//google.com"], false, "should be negative for scheme-agnostic urls"], |
| 17 | + [["http://google.com"], false, "should be negative for http urls"], |
| 18 | + [["https://google.com"], false, "should be negative for https urls"], |
| 19 | + [["chrome-extension://"], false, "should be negative for https urls"], |
| 20 | + [["moz-extension://"], false, "should be negative for https urls"], |
| 21 | + [["ms-browser-extension://"], false, "should be negative for https urls"], |
| 22 | + [["path/to/thing"], true, "should be positive for implicit relative urls"], |
| 23 | + [["./path/to/thing"], true, "should be positive for explicit relative urls"], |
| 24 | + [["~path/to/thing"], true, "should be positive for module urls (with ~)"], |
| 25 | + [["some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with path prefix"], |
| 26 | + [["./some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with relative path prefix"], |
| 27 | + // with root (normal path) |
| 28 | + [["path/to/thing", "root/dir"], true, "should be positive with root if implicit relative url"], |
| 29 | + [["./path/to/thing", "root/dir"], true, "should be positive with root if explicit relative url"], |
| 30 | + [["/path/to/thing", "root/dir"], true, "should be positive with root if root-relative url"], |
| 31 | + // with root (boolean) |
| 32 | + [["/path/to/thing", true], true, "should be positive for root-relative if root = `true`"], |
| 33 | + // with root (boolean) on Windows |
| 34 | + [["C:\\path\\to\\thing"], true, "should be positive for Windows absolute paths with drive letter"], |
| 35 | + [["\\\\?\\UNC\\ComputerName\\path\\to\\thing"], true, "should be positive for Windows absolute UNC paths"], |
| 36 | + // with root (module) |
| 37 | + [["/path/to/thing", "~"], true, "should be positive for module url if root = ~"], |
| 38 | + // with root (module path) |
| 39 | + [["/path/to/thing", "~module"], true, "should be positive for module prefixes when root starts with ~"], |
| 40 | + [["/path/to/thing", "~module/"], true, "should be positive for module prefixes (with trailing slash) when root starts with ~"], |
| 41 | + // error cases |
| 42 | + [["/path/to/thing", 1], new ExpectedError(/unexpected parameters/i), "should throw an error on invalid root"], |
| 43 | + |
| 44 | + // empty url |
| 45 | + [[""], true, "should be positive if url is empty"] |
| 46 | + ].forEach((test) => { |
| 47 | + it(test[2], () => { |
| 48 | + const expected = test[1]; |
| 49 | + try { |
| 50 | + const request = loaderUtils.isUrlRequest.apply(loaderUtils, test[0]); |
| 51 | + assert.equal(request, expected); |
| 52 | + } catch(e) { |
| 53 | + if(expected instanceof ExpectedError) { |
| 54 | + assert.ok(expected.matches(e)); |
| 55 | + } else { |
| 56 | + assert.ok(false, "should not have thrown an error: " + e.message); |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments