test(util): add more getQuerystring() tests

This commit is contained in:
Adam Bradley
2016-03-07 09:29:23 -06:00
parent 3e201ed304
commit 172a425353
2 changed files with 50 additions and 6 deletions

View File

@ -235,6 +235,28 @@ export function run() {
describe('getQuerystring', function() {
it('should have no entries for empty url', () => {
expect(util.getQuerystring('')).toEqual({});
expect(util.getQuerystring(null)).toEqual({});
expect(util.getQuerystring(undefined)).toEqual({});
});
it('should have no entries when without ?', () => {
expect(util.getQuerystring('http://localhost:1234/')).toEqual({});
});
it('should have no entries with only ?', () => {
expect(util.getQuerystring('http://localhost:1234/?')).toEqual({});
});
it('should have no entries for key with no =', () => {
expect(util.getQuerystring('http://localhost:1234/?key')).toEqual({});
});
it('should have no entries with only #?', () => {
expect(util.getQuerystring('http://localhost:1234/#?')).toEqual({});
});
it('should have no entries with only #?=', () => {
expect(util.getQuerystring('http://localhost:1234/#?=')).toEqual({});
});
it('should have no entries for url with no "?" character', () => {
@ -242,12 +264,25 @@ export function run() {
});
it('should contain key/value entries for all the parameters after "?" character', () => {
expect(util.getQuerystring('http://localhost:1234/#key0=0&key0x=0x?key1=1&key2=2')).toEqual({
expect(util.getQuerystring('http://localhost:1234/#key1=1&key2x=2x?key3=3&key4=4')).toEqual({
key3: '3',
key4: '4'
});
});
it('should lowercase param keys', () => {
expect(util.getQuerystring('http://localhost:1234/#?KEY1=1&kEy2=2')).toEqual({
key1: '1',
key2: '2'
});
});
it('should not include any values when # comes after ?', () => {
expect(util.getQuerystring('http://localhost:1234/?key1=1#key2=2')).toEqual({
key1: '1'
});
});
it('should ignore empty ?& and &&', () => {
expect(util.getQuerystring('http://localhost:1234/#?&&')).toEqual({});
@ -257,6 +292,12 @@ export function run() {
});
});
it('should get "" when key has no value', () => {
expect(util.getQuerystring('http://localhost:1234/#?key=')).toEqual({
key: ''
});
});
});
describe('isTrueProperty', function() {

View File

@ -182,11 +182,14 @@ export function getQuerystring(url: string): any {
const startIndex = url.indexOf('?');
if (startIndex !== -1) {
const queries = url.slice(startIndex + 1).split('&');
queries.filter((param) => { return param.indexOf('=') > 0; }).forEach((param) => {
var split = param.split('=');
if(split.length > 1)
queryParams[split[0].toLowerCase()] = split[1].split('#')[0];
});
for (var i = 0; i < queries.length; i++) {
if (queries[i].indexOf('=') > 0) {
var split = queries[i].split('=');
if (split.length > 1) {
queryParams[split[0].toLowerCase()] = split[1].split('#')[0];
}
}
}
}
}
return queryParams;