Skip to content

Commit 172a425

Browse files
committed
test(util): add more getQuerystring() tests
1 parent 3e201ed commit 172a425

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

ionic/util/test/util.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,54 @@ export function run() {
235235
describe('getQuerystring', function() {
236236
it('should have no entries for empty url', () => {
237237
expect(util.getQuerystring('')).toEqual({});
238+
expect(util.getQuerystring(null)).toEqual({});
239+
expect(util.getQuerystring(undefined)).toEqual({});
240+
});
241+
242+
it('should have no entries when without ?', () => {
243+
expect(util.getQuerystring('http://localhost:1234/')).toEqual({});
244+
});
245+
246+
it('should have no entries with only ?', () => {
247+
expect(util.getQuerystring('http://localhost:1234/?')).toEqual({});
248+
});
249+
250+
it('should have no entries for key with no =', () => {
251+
expect(util.getQuerystring('http://localhost:1234/?key')).toEqual({});
252+
});
253+
254+
it('should have no entries with only #?', () => {
255+
expect(util.getQuerystring('http://localhost:1234/#?')).toEqual({});
256+
});
257+
258+
it('should have no entries with only #?=', () => {
259+
expect(util.getQuerystring('http://localhost:1234/#?=')).toEqual({});
238260
});
239261

240262
it('should have no entries for url with no "?" character', () => {
241263
expect(util.getQuerystring('http://localhost:1234/#key1=1&key2=2')).toEqual({});
242264
});
243265

244266
it('should contain key/value entries for all the parameters after "?" character', () => {
245-
expect(util.getQuerystring('http://localhost:1234/#key0=0&key0x=0x?key1=1&key2=2')).toEqual({
267+
expect(util.getQuerystring('http://localhost:1234/#key1=1&key2x=2x?key3=3&key4=4')).toEqual({
268+
key3: '3',
269+
key4: '4'
270+
});
271+
});
272+
273+
it('should lowercase param keys', () => {
274+
expect(util.getQuerystring('http://localhost:1234/#?KEY1=1&kEy2=2')).toEqual({
246275
key1: '1',
247276
key2: '2'
248277
});
249278
});
250279

280+
it('should not include any values when # comes after ?', () => {
281+
expect(util.getQuerystring('http://localhost:1234/?key1=1#key2=2')).toEqual({
282+
key1: '1'
283+
});
284+
});
285+
251286
it('should ignore empty ?& and &&', () => {
252287
expect(util.getQuerystring('http://localhost:1234/#?&&')).toEqual({});
253288

@@ -257,6 +292,12 @@ export function run() {
257292
});
258293
});
259294

295+
it('should get "" when key has no value', () => {
296+
expect(util.getQuerystring('http://localhost:1234/#?key=')).toEqual({
297+
key: ''
298+
});
299+
});
300+
260301
});
261302

262303
describe('isTrueProperty', function() {

ionic/util/util.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,14 @@ export function getQuerystring(url: string): any {
182182
const startIndex = url.indexOf('?');
183183
if (startIndex !== -1) {
184184
const queries = url.slice(startIndex + 1).split('&');
185-
queries.filter((param) => { return param.indexOf('=') > 0; }).forEach((param) => {
186-
var split = param.split('=');
187-
if(split.length > 1)
188-
queryParams[split[0].toLowerCase()] = split[1].split('#')[0];
189-
});
185+
for (var i = 0; i < queries.length; i++) {
186+
if (queries[i].indexOf('=') > 0) {
187+
var split = queries[i].split('=');
188+
if (split.length > 1) {
189+
queryParams[split[0].toLowerCase()] = split[1].split('#')[0];
190+
}
191+
}
192+
}
190193
}
191194
}
192195
return queryParams;

0 commit comments

Comments
 (0)