-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix UrlParams parsing when URL has no parameters. Add remove() and isEmpty() methods. #6246
Changes from all commits
7c9e7aa
6cd9418
353ffe9
44d8b9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/* | ||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
|
||
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */ | ||
/*global define, describe, it, expect */ | ||
|
||
define(function (require, exports, module) { | ||
'use strict'; | ||
|
||
var UrlParams = require("utils/UrlParams").UrlParams; | ||
|
||
describe("UrlParams", function () { | ||
describe("Test for Empty parameter list", function () { | ||
var params = new UrlParams(); | ||
|
||
it("should show that the parameter object is empty", function () { | ||
params.parse("http://www.brackets.io"); | ||
|
||
expect(params.isEmpty()).toBeTruthy(); | ||
expect(params.toString()).toEqual(""); | ||
}); | ||
|
||
it("should show that the parameter object is NOT empty", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); | ||
|
||
expect(params.isEmpty()).toBeFalsy(); | ||
expect(params.toString()).toNotEqual(""); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add some tests for some edge cases:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @redmunds, yes, this sounds good. I'm going to put these test cases in and see if I can break the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @redmunds, one nit I ran into while describing these tests. I am now calling the portion of the URL after the question mark the "query string" instead of the "search string" naming convention I used in my original code. Even though we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-paired values: http://www.brackets.io?one&two&three Fixed. Parsing this URL now produces an object with properties "one", "two", and "three", each with the value of an empty string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty params with and without whitespace at the end: http://www.brackets.io? Fixed. Parsing these URLs now produce an empty object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Random number used to circumvent browser cache: http://www.brackets.io?28945893575608 Fixed by the non-paired values fix above. Parsing this URL now produces an object with one property "28945893575608" that has the value of empty string. |
||
|
||
describe("Parse and Get URL parameters", function () { | ||
var params = new UrlParams(); | ||
|
||
it("should create a parameter object and get three parameters", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
}); | ||
|
||
it("should create a parameter object with three parameters with empty string values", function () { | ||
params.parse("http://www.brackets.io?one&two&three"); | ||
|
||
expect(params.get("one")).toEqual(""); | ||
expect(params.get("two")).toEqual(""); | ||
expect(params.get("three")).toEqual(""); | ||
}); | ||
|
||
it("should create a parameter object with no parameters", function () { | ||
params.parse("http://www.brackets.io"); | ||
|
||
expect(params.get("one")).toBeUndefined(); | ||
expect(params.get("two")).toBeUndefined(); | ||
expect(params.get("three")).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("Put and Remove URL parameters", function () { | ||
var params = new UrlParams(); | ||
|
||
it("should put a new parameter three in the list", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toBeUndefined(); | ||
|
||
params.put("three", "foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
}); | ||
|
||
it("should change the value of parameter two", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
|
||
params.put("two", "false"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("false"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
}); | ||
|
||
it("should remove parameter one", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
|
||
params.remove("one"); | ||
|
||
expect(params.get("one")).toBeUndefined(); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
}); | ||
|
||
it("should remove three parameters, leaving an empty list", function () { | ||
params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
|
||
expect(params.isEmpty()).toBeFalsy(); | ||
expect(params.toString()).toNotEqual(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably makes more sense to test:
Or is order not guaranteed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @redmunds, correct, the order is not guaranteed, and I couldn't think of a better test for toString() than checking for an empty string. Is there a way to test a toString() value of an object more accurately? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My knee-jerk expectation was that the UrlParams object would maintain order when converting back to string, but I can't think of a scenario where it's important. I guess this test is ok like it is. |
||
|
||
params.remove("one"); | ||
params.remove("two"); | ||
params.remove("three"); | ||
|
||
expect(params.get("one")).toBeUndefined(); | ||
expect(params.get("two")).toBeUndefined(); | ||
expect(params.get("three")).toBeUndefined(); | ||
|
||
expect(params.isEmpty()).toBeTruthy(); | ||
expect(params.toString()).toEqual(""); | ||
}); | ||
|
||
it("should add three parameters to an empty list", function () { | ||
params.parse("http://www.brackets.io"); | ||
|
||
expect(params.get("one")).toBeUndefined(); | ||
expect(params.get("two")).toBeUndefined(); | ||
expect(params.get("three")).toBeUndefined(); | ||
|
||
expect(params.isEmpty()).toBeTruthy(); | ||
expect(params.toString()).toEqual(""); | ||
|
||
params.put("one", "1"); | ||
params.put("two", "true"); | ||
params.put("three", "foobar"); | ||
|
||
expect(params.get("one")).toEqual("1"); | ||
expect(params.get("two")).toEqual("true"); | ||
expect(params.get("three")).toEqual("foobar"); | ||
|
||
expect(params.isEmpty()).toBeFalsy(); | ||
expect(params.toString()).toNotEqual(""); | ||
}); | ||
}); | ||
|
||
describe("Test for malformed or unusual query strings", function () { | ||
var params = new UrlParams(); | ||
|
||
it("should parse a missing query string as an empty object", function () { | ||
params.parse("http://www.brackets.io?"); | ||
|
||
expect(params.isEmpty()).toBeTruthy(); | ||
expect(params.toString()).toEqual(""); | ||
}); | ||
|
||
it("should parse a query string of whitespace as an empty object", function () { | ||
params.parse("http://www.brackets.io? "); | ||
|
||
expect(params.isEmpty()).toBeTruthy(); | ||
expect(params.toString()).toEqual(""); | ||
}); | ||
|
||
it("should parse a random number (used to circumvent browser cache)", function () { | ||
params.parse("http://www.brackets.io?28945893575608"); | ||
|
||
expect(params.get("28945893575608")).toEqual(""); | ||
expect(params.isEmpty()).toBeFalsy(); | ||
expect(params.toString()).toEqual("28945893575608="); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it's possible for this code could provide unexpected results for
self._store
. For example, if url ends with a ?. Also, if all search parameters are integers, thenself._store
would be an array.I think it would be safest to initialize it to an empty object in all cases:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@redmunds, I believe this is already addressed in the URLParams constructor, which initializes self._store to an empty object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the constructor made the call to parse() and parse() was private method, then I would agree. But parse() is public and can be called multiple times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I missed comment until after my last commit. Let me look into it.