url-parse-utility
Version:
A simple RegEx based utility for parsing each part of the URL. This would help to get the different constituents of the URL.
103 lines (89 loc) • 3.68 kB
JavaScript
var URL = require('../lib');
var TEST_URL = 'https://www.google.co.in/search?q=sample+URL+with+all+possible+combinations&oq=sample+URL+with+all+possible+combinations&aqs=chrome..69i57.8450j0j7&sourceid=chrome&ie=UTF-8/#/TEST_HASH/'
describe('Identify Protocol',function(){
it('should get the protocol of the URL in question',function(){
var url = new URL(TEST_URL);
expect(url.getProtocol()==='http:' || url.getProtocol()==='https:').toBeTruthy();
});
});
describe('Identify Slashes',function(){
it('should get true if slash exist',function(){
var url = new URL(TEST_URL);
expect(typeof url.getSlashes()).toEqual('boolean');
});
});
describe('Identify Auth',function(){
it('should get an object if auth',function(){
var url = new URL(TEST_URL);
expect(typeof url.getAuth()).toEqual('object');
});
});
describe('Identify Host',function(){
it('should get the host',function(){
var url = new URL(TEST_URL);
console.log(url.getHost());
expect(url.getHost()).toBe('www.google.co.in');
});
});
describe('Identify Port',function(){
it('should get the Port',function(){
var url = new URL(TEST_URL);
console.log(url.getPort());
expect(url.getPort()).toBe(null);
});
});
describe('Identify HostName',function(){
it('should get the HostName',function(){
var url = new URL(TEST_URL);
console.log(url.getHostname());
expect(url.getHostname()).toBe('www.google.co.in');
});
});
describe('Identify Hash',function(){
it('should get the Hash',function(){
var url = new URL(TEST_URL);
console.log(url.getHash());
expect(url.getHash()).toBe('#/TEST_HASH/');
});
});
describe('Identify Search',function(){
it('should get the Search parameter after ?',function(){
var url = new URL(TEST_URL);
console.log(url.getSearch());
expect(url.getSearch()).toBe('?q=sample+URL+with+all+possible+combinations&oq=sample+URL+with+all+possible+combinations&aqs=chrome..69i57.8450j0j7&sourceid=chrome&ie=UTF-8/');
});
});
describe('Identify Query',function(){
it('should get the Query parameter',function(){
var url = new URL(TEST_URL);
console.log(url.getQuery());
expect(url.getQuery()).toBe('q=sample+URL+with+all+possible+combinations&oq=sample+URL+with+all+possible+combinations&aqs=chrome..69i57.8450j0j7&sourceid=chrome&ie=UTF-8/');
});
});
describe('Identify PathName',function(){
it('should get the Path name',function(){
var url = new URL(TEST_URL);
console.log(url.getPathname());
expect(url.getPathname()).toBe('/search');
});
});
describe('Identify Path',function(){
it('should get the full Path',function(){
var url = new URL(TEST_URL);
console.log(url.getPath());
expect(url.getPath()).toBe('/search?q=sample+URL+with+all+possible+combinations&oq=sample+URL+with+all+possible+combinations&aqs=chrome..69i57.8450j0j7&sourceid=chrome&ie=UTF-8/');
});
});
describe('Identify Href',function(){
it('should get the full Href/URL',function(){
var url = new URL(TEST_URL);
console.log(url.getHref());
expect(url.getHref()).toBe('https://www.google.co.in/search?q=sample+URL+with+all+possible+combinations&oq=sample+URL+with+all+possible+combinations&aqs=chrome..69i57.8450j0j7&sourceid=chrome&ie=UTF-8/#/TEST_HASH/');
});
});
describe('Get Parsed URL',function(){
it('should get an object of Parse URL',function(){
var url = new URL(TEST_URL);
expect(typeof url.getParsedObject()).toEqual('object');
});
});