|
| 1 | +// imported from https://github.com/galkn/parseuri |
| 2 | +const expect = require("expect.js"); |
| 3 | +const parseuri = require("..").parse; |
| 4 | + |
| 5 | +describe("parseuri", function () { |
| 6 | + it("should parse an uri", function () { |
| 7 | + const http = parseuri("http://google.com"), |
| 8 | + https = parseuri("https://www.google.com:80"), |
| 9 | + query = parseuri("google.com:8080/foo/bar?foo=bar"), |
| 10 | + localhost = parseuri("localhost:8080"), |
| 11 | + ipv6 = parseuri("2001:0db8:85a3:0042:1000:8a2e:0370:7334"), |
| 12 | + ipv6short = parseuri("2001:db8:85a3:42:1000:8a2e:370:7334"), |
| 13 | + ipv6port = parseuri("2001:db8:85a3:42:1000:8a2e:370:7334:80"), |
| 14 | + ipv6abbrev = parseuri("2001::7334:a:80"), |
| 15 | + ipv6http = parseuri("http://[2001::7334:a]:80"), |
| 16 | + ipv6query = parseuri("http://[2001::7334:a]:80/foo/bar?foo=bar"); |
| 17 | + |
| 18 | + expect(http.protocol).to.be("http"); |
| 19 | + expect(http.port).to.be(""); |
| 20 | + expect(http.host).to.be("google.com"); |
| 21 | + expect(https.protocol).to.be("https"); |
| 22 | + expect(https.port).to.be("80"); |
| 23 | + expect(https.host).to.be("www.google.com"); |
| 24 | + expect(query.port).to.be("8080"); |
| 25 | + expect(query.query).to.be("foo=bar"); |
| 26 | + expect(query.path).to.be("/foo/bar"); |
| 27 | + expect(query.relative).to.be("/foo/bar?foo=bar"); |
| 28 | + expect(query.queryKey.foo).to.be("bar"); |
| 29 | + expect(query.pathNames[0]).to.be("foo"); |
| 30 | + expect(query.pathNames[1]).to.be("bar"); |
| 31 | + expect(localhost.protocol).to.be(""); |
| 32 | + expect(localhost.host).to.be("localhost"); |
| 33 | + expect(localhost.port).to.be("8080"); |
| 34 | + expect(ipv6.protocol).to.be(""); |
| 35 | + expect(ipv6.host).to.be("2001:0db8:85a3:0042:1000:8a2e:0370:7334"); |
| 36 | + expect(ipv6.port).to.be(""); |
| 37 | + expect(ipv6short.protocol).to.be(""); |
| 38 | + expect(ipv6short.host).to.be("2001:db8:85a3:42:1000:8a2e:370:7334"); |
| 39 | + expect(ipv6short.port).to.be(""); |
| 40 | + expect(ipv6port.protocol).to.be(""); |
| 41 | + expect(ipv6port.host).to.be("2001:db8:85a3:42:1000:8a2e:370:7334"); |
| 42 | + expect(ipv6port.port).to.be("80"); |
| 43 | + expect(ipv6abbrev.protocol).to.be(""); |
| 44 | + expect(ipv6abbrev.host).to.be("2001::7334:a:80"); |
| 45 | + expect(ipv6abbrev.port).to.be(""); |
| 46 | + expect(ipv6http.protocol).to.be("http"); |
| 47 | + expect(ipv6http.port).to.be("80"); |
| 48 | + expect(ipv6http.host).to.be("2001::7334:a"); |
| 49 | + expect(ipv6query.protocol).to.be("http"); |
| 50 | + expect(ipv6query.port).to.be("80"); |
| 51 | + expect(ipv6query.host).to.be("2001::7334:a"); |
| 52 | + expect(ipv6query.relative).to.be("/foo/bar?foo=bar"); |
| 53 | + |
| 54 | + const withUserInfo = parseuri("ws://foo:[email protected]"); |
| 55 | + |
| 56 | + expect(withUserInfo.protocol).to.eql("ws"); |
| 57 | + expect(withUserInfo.userInfo).to.eql("foo:bar"); |
| 58 | + expect(withUserInfo.user).to.eql("foo"); |
| 59 | + expect(withUserInfo.password).to.eql("bar"); |
| 60 | + expect(withUserInfo.host).to.eql("google.com"); |
| 61 | + |
| 62 | + const relativeWithQuery = parseuri("/[email protected]"); |
| 63 | + |
| 64 | + expect(relativeWithQuery.host).to.be(""); |
| 65 | + expect(relativeWithQuery.path).to.be("/foo"); |
| 66 | + expect(relativeWithQuery.query).to.be("[email protected]"); |
| 67 | + }); |
| 68 | +}); |
0 commit comments