-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(URL): allow undefined 2nd args and fix pathname return value (#263)
- Loading branch information
Showing
2 changed files
with
83 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,61 @@ | ||
describe("Test URL ", function () { | ||
|
||
it("Test invalid URL parsing", function(){ | ||
var exceptionCaught = false; | ||
try { | ||
const url = new URL(''); | ||
}catch(e){ | ||
exceptionCaught = true; | ||
} | ||
expect(exceptionCaught).toBe(true); | ||
}); | ||
|
||
it("Test valid URL parsing", function(){ | ||
var exceptionCaught = false; | ||
try { | ||
const url = new URL('https://google.com'); | ||
}catch(e){ | ||
exceptionCaught = true; | ||
} | ||
expect(exceptionCaught).toBe(false); | ||
}); | ||
|
||
|
||
it("Test URL fields", function(){ | ||
var exceptionCaught = false; | ||
const url = new URL('https://google.com'); | ||
expect(url.protocol).toBe('https:'); | ||
expect(url.hostname).toBe('google.com'); | ||
}); | ||
|
||
describe("URL", function () { | ||
it("throws on invalid URL", function () { | ||
var exceptionCaught = false; | ||
try { | ||
const url = new URL(""); | ||
} catch (e) { | ||
exceptionCaught = true; | ||
} | ||
expect(exceptionCaught).toBe(true); | ||
}); | ||
|
||
it("does not throw on valid URL", function () { | ||
var exceptionCaught = false; | ||
try { | ||
const url = new URL("https://google.com"); | ||
} catch (e) { | ||
exceptionCaught = true; | ||
} | ||
expect(exceptionCaught).toBe(false); | ||
}); | ||
|
||
it("parses simple urls", function () { | ||
const url = new URL("https://google.com"); | ||
expect(url.protocol).toBe("https:"); | ||
expect(url.hostname).toBe("google.com"); | ||
expect(url.pathname).toBe("/"); | ||
expect(url.port).toBe(""); | ||
expect(url.search).toBe(""); | ||
expect(url.hash).toBe(""); | ||
expect(url.username).toBe(""); | ||
expect(url.password).toBe(""); | ||
expect(url.origin).toBe("https://google.com"); | ||
expect(url.searchParams.size).toBe(0); | ||
}); | ||
|
||
it("parses with undefined base", function () { | ||
const url = new URL("https://google.com", undefined); | ||
expect(url.protocol).toBe("https:"); | ||
expect(url.hostname).toBe("google.com"); | ||
}); | ||
|
||
it("parses with null base", function () { | ||
const url = new URL("https://google.com", null); | ||
expect(url.protocol).toBe("https:"); | ||
expect(url.hostname).toBe("google.com"); | ||
}); | ||
|
||
it("parses query strings", function () { | ||
const url = new URL("https://google.com?q=hello"); | ||
expect(url.search).toBe("?q=hello"); | ||
expect(url.searchParams.get("q")).toBe("hello"); | ||
expect(url.pathname).toBe("/"); | ||
}); | ||
|
||
it("parses query strings with pathname", function () { | ||
const url = new URL("https://google.com/some/path?q=hello"); | ||
expect(url.search).toBe("?q=hello"); | ||
expect(url.searchParams.get("q")).toBe("hello"); | ||
expect(url.pathname).toBe("/some/path"); | ||
}); | ||
}); |