urns
Version:
An RFC 8141 compliant URN library with some interesting type related functionality
78 lines (77 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var equivalent_1 = require("./equivalent");
var parser_1 = require("./parser");
/** This tests specifically the URN parsing, per RFC 8141 */
describe("Test URN parsing functionality", function () {
/** This function decode and re-encodes the URN to ensure it round trips properly */
var roundTrip = function (s, parsed) {
/** First, parse it and compare against the expected parsed result */
var result = parser_1.parseURN(s);
expect(result).toEqual(parsed);
/** Test the nide and nss functions to ensure they get the expected results */
expect(parser_1.nid(s)).toEqual(parsed.nid);
expect(parser_1.nss(s)).toEqual(parsed.nss);
/** Now "unparse" the URN and compare the result with the original. */
var orig = parser_1.unparseURN(result);
expect(orig).toEqual(s);
expect(equivalent_1.equivalent(s, orig));
};
it("should parse a simple URN", function () {
roundTrip("urn:nid:nss", {
nid: "nid",
nss: "nss",
nss_encoded: "nss",
rcomponent: null,
qcomponent: null,
fragment: null,
});
});
it("should parse a complex URN", function () {
/** These exercises pretty much ever part of RFC 8141 */
roundTrip("urn:example:a123,0%7C00~&z456/789?+abc?=xyz#12/3", {
nid: "example",
nss: "a123,0|00~&z456/789",
nss_encoded: "a123,0%7C00~&z456/789",
rcomponent: "abc",
qcomponent: "xyz",
fragment: "12/3",
});
});
it("should handle emojis", function () {
/** These case deals ensures that the NSS of the URN is properly URI decoded */
roundTrip("urn:feeling:%F0%9F%98%83", {
nid: "feeling",
nss: "😃",
nss_encoded: "%F0%9F%98%83",
rcomponent: null,
qcomponent: null,
fragment: null,
});
});
});
/** These tests various other aspects of RFC 8141. */
describe("Test validation of URNs", function () {
it("should fail if NID is longer than 31 characters", function () {
expect(function () {
return parser_1.createURN("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "1");
}).toThrow("Unable to create a syntactically valid URN");
});
it("should fail if NID is empty", function () {
expect(function () { return parser_1.createURN("", "a"); }).toThrow("Unable to create a syntactically valid URN");
});
it("should fail to unparse if NID is empty", function () {
expect(function () {
return parser_1.unparseURN({
nid: "",
nss: "a",
qcomponent: null,
rcomponent: null,
fragment: null,
});
}).toThrow("Unable to create a syntactically valid URN");
});
it("should fail if the string isn't a URN", function () {
expect(function () { return parser_1.parseURN("http://example.com/path"); }).toThrow("String \"http://example.com/path\" is not a valid RFC8141 compliant URN");
});
});