ts-prime
Version:
A utility library for JavaScript and Typescript.
91 lines (90 loc) • 5.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var url_1 = require("./url");
describe('url join', function () {
it('should work for simple case', function () {
expect(url_1.urlJoin('http://www.google.com/', 'foo/bar', '?test=123')).toEqual('http://www.google.com/foo/bar?test=123');
});
it('should work for hashbang urls', function () {
expect(url_1.urlJoin('http://www.google.com', '#!', 'foo/bar', '?test=123')).toEqual('http://www.google.com/#!/foo/bar?test=123');
});
it('should be able to join protocol', function () {
expect(url_1.urlJoin('http:', 'www.google.com/', 'foo/bar', '?test=123')).toEqual('http://www.google.com/foo/bar?test=123');
});
it('should be able to join protocol with slashes', function () {
expect(url_1.urlJoin('http://', 'www.google.com/', 'foo/bar', '?test=123')).toEqual('http://www.google.com/foo/bar?test=123');
});
it('should remove extra slashes', function () {
expect(url_1.urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123')).toEqual('http://www.google.com/foo/bar?test=123');
});
it('should not remove extra slashes in an encoded URL', function () {
expect(url_1.urlJoin('http:', 'www.google.com///', 'foo/bar', '?url=http%3A//Ftest.com')).toEqual('http://www.google.com/foo/bar?url=http%3A//Ftest.com');
});
it('should support anchors in urls', function () {
expect(url_1.urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa')).toEqual('http://www.google.com/foo/bar?test=123#faaaaa');
});
it('should support protocol-relative urls', function () {
expect(url_1.urlJoin('//www.google.com', 'foo/bar', '?test=123')).toEqual('//www.google.com/foo/bar?test=123');
});
it('should support file protocol urls', function () {
expect(url_1.urlJoin('file:/', 'android_asset', 'foo/bar')).toEqual("file://android_asset/foo/bar");
expect(url_1.urlJoin('file:', '/android_asset', 'foo/bar')).toEqual('file://android_asset/foo/bar');
});
it('should support absolute file protocol urls', function () {
expect(url_1.urlJoin('file:', '///android_asset', 'foo/bar')).toEqual('file:///android_asset/foo/bar');
expect(url_1.urlJoin('file:///', 'android_asset', 'foo/bar')).toEqual('file:///android_asset/foo/bar');
expect(url_1.urlJoin('file:///', '//android_asset', 'foo/bar')).toEqual('file:///android_asset/foo/bar');
expect(url_1.urlJoin('file:///android_asset', 'foo/bar')).toEqual('file:///android_asset/foo/bar');
});
it('should merge multiple query params properly', function () {
expect(url_1.urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456')).toEqual('http://www.google.com/foo/bar?test=123&key=456');
expect(url_1.urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456')).toEqual('http://www.google.com/foo/bar?test=123&boom=value&key=456');
expect(url_1.urlJoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4')).toEqual('http://example.org/x?a=1&b=2&c=3&d=4');
});
it('should merge slashes in paths correctly', function () {
expect(url_1.urlJoin('http://example.org', 'a//', 'b//', 'A//', 'B//')).toEqual('http://example.org/a/b/A/B/');
});
it('should merge colons in paths correctly', function () {
expect(url_1.urlJoin('http://example.org/', ':foo:', 'bar')).toEqual('http://example.org/:foo:/bar');
});
it('should merge just a simple path without URL correctly', function () {
expect(url_1.urlJoin('/', 'test'))
.toEqual('/test');
});
it('should merge a path with colon properly', function () {
expect(url_1.urlJoin('/users/:userId', '/cars/:carId'))
.toEqual('/users/:userId/cars/:carId');
});
it('should merge slashes in protocol correctly', function () {
expect(url_1.urlJoin('http://example.org', 'a'))
.toEqual('http://example.org/a');
expect(url_1.urlJoin('http:', '//example.org', 'a'))
.toEqual('http://example.org/a');
expect(url_1.urlJoin('http:///example.org', 'a'))
.toEqual('http://example.org/a');
expect(url_1.urlJoin('file:///example.org', 'a'))
.toEqual('file:///example.org/a');
expect(url_1.urlJoin('file:example.org', 'a'))
.toEqual('file://example.org/a');
expect(url_1.urlJoin('file:/', 'example.org', 'a'))
.toEqual('file://example.org/a');
expect(url_1.urlJoin('file:', '/example.org', 'a'))
.toEqual('file://example.org/a');
expect(url_1.urlJoin('file:', '//example.org', 'a'))
.toEqual('file://example.org/a');
});
it('should skip empty strings', function () {
expect(url_1.urlJoin('http://foobar.com', '', 'test'))
.toEqual('http://foobar.com/test');
expect(url_1.urlJoin('', 'http://foobar.com', '', 'test'))
.toEqual('http://foobar.com/test');
});
it('should return an empty string if no arguments are supplied', function () {
expect(url_1.urlJoin()).toEqual('');
});
});
describe("convert url to relative", function () {
test("expect relative url", function () {
expect(url_1.urlToRelative("https://www.npmjs.com/package/qs")).toEqual("/package/qs");
});
});