119 changes: 75 additions & 44 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Dependencies
const parseUrl = require("../lib")
, tester = require("tester")
, qs = require("querystring")
;

const INPUTS = [
Expand All @@ -10,189 +9,221 @@ const INPUTS = [
, {
protocols: [ "http" ]
, protocol: "http"
, port: null
, port: ""
, resource: "ionicabizau.net"
, user: ""
, pathname: "/blog"
, hash: ""
, search: ""
, href: "http://ionicabizau.net/blog"
, query: {}
}
]
, [
" http://ionicabizau.net/blog "
, {
protocols: [ "http" ]
, protocol: "http"
, port: null
, port: ""
, resource: "ionicabizau.net"
, user: ""
, pathname: "/blog"
, hash: ""
, search: ""
, href: "http://ionicabizau.net/blog"
, query: {}
}
]
, [
"http://domain.com/path/name?foo=bar&bar=42#some-hash"
, {
protocols: ["http"]
, protocol: "http"
, port: null
, port: ""
, resource: "domain.com"
, user: ""
, pathname: "/path/name"
, hash: "some-hash"
, search: "foo=bar&bar=42"
, query: { foo: "bar", bar: "42" }
, href: "http://domain.com/path/name?foo=bar&bar=42#some-hash"
}
]
, [
"http://domain.com/path/name#some-hash?foo=bar&bar=42"
, {
protocols: ["http"]
, protocol: "http"
, port: null
, port: ""
, resource: "domain.com"
, user: ""
, pathname: "/path/name"
, hash: "some-hash?foo=bar&bar=42"
, search: ""
, query: {}
, href: "http://domain.com/path/name#some-hash?foo=bar&bar=42"
}
]
, [
"git+ssh://git@host.xz/path/name.git"
, {
protocols: ["git", "ssh"]
, protocol: "git"
, port: null
, port: ""
, resource: "host.xz"
, user: "git"
, pathname: "/path/name.git"
, hash: ""
, search: ""
, query: {}
, href: "git+ssh://git@host.xz/path/name.git"
}
]
, [
// NOTE:parse-path will look at this as a local path
// For parsing it as url, please use parse-url
"git@github.com:IonicaBizau/git-stats.git"
, {
protocols: []
, protocol: "ssh"
, port: null
, resource: "github.com"
, user: "git"
, pathname: "/IonicaBizau/git-stats.git"
protocols: ["file"]
, protocol: "file"
, port: ""
, resource: ""
, user: ""
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "git@github.com:IonicaBizau/git-stats.git"
}
]
, [
"/path/to/file.png"
, {
protocols: []
protocols: ["file"]
, protocol: "file"
, port: null
, port: ""
, resource: ""
, user: ""
, pathname: "/path/to/file.png"
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "/path/to/file.png"
}
]
, [
"./path/to/file.png"
, {
protocols: []
protocols: ["file"]
, protocol: "file"
, port: null
, port: ""
, resource: ""
, user: ""
, pathname: "./path/to/file.png"
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "./path/to/file.png"
}
]
, [
"./.path/to/file.png"
, {
protocols: []
protocols: ["file"]
, protocol: "file"
, port: null
, port: ""
, resource: ""
, user: ""
, pathname: "./.path/to/file.png"
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "./.path/to/file.png"
}
]
, [
".path/to/file.png"
, {
protocols: []
protocols: ["file"]
, protocol: "file"
, port: null
, port: ""
, resource: ""
, user: ""
, pathname: ".path/to/file.png"
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: ".path/to/file.png"
}
]
, [
"path/to/file.png"
, {
protocols: []
protocols: ["file"]
, protocol: "file"
, port: null
, port: ""
, resource: ""
, user: ""
, pathname: "path/to/file.png"
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "path/to/file.png"
}
], [
"git@github.com:9IonicaBizau/git-stats.git"
, {
protocols: []
, protocol: "ssh"
, port: null
, resource: "github.com"
, user: "git"
, pathname: "/9IonicaBizau/git-stats.git"
protocols: ["file"]
, protocol: "file"
, port: ""
, resource: ""
, user: ""
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "git@github.com:9IonicaBizau/git-stats.git"
}
], [
"git@github.com:0xABC/git-stats.git"
, {
protocols: []
, protocol: "ssh"
, port: null
, resource: "github.com"
, user: "git"
, pathname: "/0xABC/git-stats.git"
protocols: ["file"]
, protocol: "file"
, port: ""
, resource: ""
, user: ""
, pathname: ""
, hash: ""
, search: ""
, query: {}
, href: "git@github.com:0xABC/git-stats.git"
}
], [
"https://attacker.com\\@example.com"
, {
protocols: ["https"]
, protocol: "https"
, port: null
, port: ""
, resource: "attacker.com"
, user: ""
, pathname: "/@example.com"
, hash: ""
, search: ""
, href: "https://attacker.com/@example.com"
, query: {}
}
], [
"jav\r\nascript://%0aalert(1)"
, {
protocols: ["javascript"]
, protocol: "javascript"
, port: null
, port: ""
, resource: "%0aalert(1)"
, user: ""
, pathname: ""
, hash: ""
, href: "javascript://%0aalert(1)"
, query: {}
, search: ""
}
]
Expand All @@ -201,9 +232,9 @@ const INPUTS = [
tester.describe("check urls", test => {
INPUTS.forEach(function (c) {
test.should("support " + c[0], () => {
c[1].href = c[0].trim().replace(/\r?\n|\r/gm, "");
c[1].query = qs.parse(c[1].search)
test.expect(parseUrl(c[0])).toEqual(c[1]);
const cParsed = parseUrl(c[0])
debugger
test.expect(cParsed).toEqual(c[1]);
});
});
});