|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */ |
| 26 | +/*global define, describe, it, expect */ |
| 27 | + |
| 28 | +define(function (require, exports, module) { |
| 29 | + 'use strict'; |
| 30 | + |
| 31 | + var UrlParams = require("utils/UrlParams").UrlParams; |
| 32 | + |
| 33 | + describe("UrlParams", function () { |
| 34 | + describe("Test for Empty parameter list", function () { |
| 35 | + var params = new UrlParams(); |
| 36 | + |
| 37 | + it("should show that the parameter object is empty", function () { |
| 38 | + params.parse("http://www.brackets.io"); |
| 39 | + |
| 40 | + expect(params.isEmpty()).toBeTruthy(); |
| 41 | + expect(params.toString()).toEqual(""); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should show that the parameter object is NOT empty", function () { |
| 45 | + params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); |
| 46 | + |
| 47 | + expect(params.isEmpty()).toBeFalsy(); |
| 48 | + expect(params.toString()).toNotEqual(""); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("Parse and Get URL parameters", function () { |
| 53 | + var params = new UrlParams(); |
| 54 | + |
| 55 | + it("should create a parameter object and get three parameters", function () { |
| 56 | + params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); |
| 57 | + |
| 58 | + expect(params.get("one")).toEqual("1"); |
| 59 | + expect(params.get("two")).toEqual("true"); |
| 60 | + expect(params.get("three")).toEqual("foobar"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should create a parameter object with three parameters with empty string values", function () { |
| 64 | + params.parse("http://www.brackets.io?one&two&three"); |
| 65 | + |
| 66 | + expect(params.get("one")).toEqual(""); |
| 67 | + expect(params.get("two")).toEqual(""); |
| 68 | + expect(params.get("three")).toEqual(""); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should create a parameter object with no parameters", function () { |
| 72 | + params.parse("http://www.brackets.io"); |
| 73 | + |
| 74 | + expect(params.get("one")).toBeUndefined(); |
| 75 | + expect(params.get("two")).toBeUndefined(); |
| 76 | + expect(params.get("three")).toBeUndefined(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("Put and Remove URL parameters", function () { |
| 81 | + var params = new UrlParams(); |
| 82 | + |
| 83 | + it("should put a new parameter three in the list", function () { |
| 84 | + params.parse("http://www.brackets.io?one=1&two=true"); |
| 85 | + |
| 86 | + expect(params.get("one")).toEqual("1"); |
| 87 | + expect(params.get("two")).toEqual("true"); |
| 88 | + expect(params.get("three")).toBeUndefined(); |
| 89 | + |
| 90 | + params.put("three", "foobar"); |
| 91 | + |
| 92 | + expect(params.get("one")).toEqual("1"); |
| 93 | + expect(params.get("two")).toEqual("true"); |
| 94 | + expect(params.get("three")).toEqual("foobar"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should change the value of parameter two", function () { |
| 98 | + params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); |
| 99 | + |
| 100 | + expect(params.get("one")).toEqual("1"); |
| 101 | + expect(params.get("two")).toEqual("true"); |
| 102 | + expect(params.get("three")).toEqual("foobar"); |
| 103 | + |
| 104 | + params.put("two", "false"); |
| 105 | + |
| 106 | + expect(params.get("one")).toEqual("1"); |
| 107 | + expect(params.get("two")).toEqual("false"); |
| 108 | + expect(params.get("three")).toEqual("foobar"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should remove parameter one", function () { |
| 112 | + params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); |
| 113 | + |
| 114 | + expect(params.get("one")).toEqual("1"); |
| 115 | + expect(params.get("two")).toEqual("true"); |
| 116 | + expect(params.get("three")).toEqual("foobar"); |
| 117 | + |
| 118 | + params.remove("one"); |
| 119 | + |
| 120 | + expect(params.get("one")).toBeUndefined(); |
| 121 | + expect(params.get("two")).toEqual("true"); |
| 122 | + expect(params.get("three")).toEqual("foobar"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should remove three parameters, leaving an empty list", function () { |
| 126 | + params.parse("http://www.brackets.io?one=1&two=true&three=foobar"); |
| 127 | + |
| 128 | + expect(params.get("one")).toEqual("1"); |
| 129 | + expect(params.get("two")).toEqual("true"); |
| 130 | + expect(params.get("three")).toEqual("foobar"); |
| 131 | + |
| 132 | + expect(params.isEmpty()).toBeFalsy(); |
| 133 | + expect(params.toString()).toNotEqual(""); |
| 134 | + |
| 135 | + params.remove("one"); |
| 136 | + params.remove("two"); |
| 137 | + params.remove("three"); |
| 138 | + |
| 139 | + expect(params.get("one")).toBeUndefined(); |
| 140 | + expect(params.get("two")).toBeUndefined(); |
| 141 | + expect(params.get("three")).toBeUndefined(); |
| 142 | + |
| 143 | + expect(params.isEmpty()).toBeTruthy(); |
| 144 | + expect(params.toString()).toEqual(""); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should add three parameters to an empty list", function () { |
| 148 | + params.parse("http://www.brackets.io"); |
| 149 | + |
| 150 | + expect(params.get("one")).toBeUndefined(); |
| 151 | + expect(params.get("two")).toBeUndefined(); |
| 152 | + expect(params.get("three")).toBeUndefined(); |
| 153 | + |
| 154 | + expect(params.isEmpty()).toBeTruthy(); |
| 155 | + expect(params.toString()).toEqual(""); |
| 156 | + |
| 157 | + params.put("one", "1"); |
| 158 | + params.put("two", "true"); |
| 159 | + params.put("three", "foobar"); |
| 160 | + |
| 161 | + expect(params.get("one")).toEqual("1"); |
| 162 | + expect(params.get("two")).toEqual("true"); |
| 163 | + expect(params.get("three")).toEqual("foobar"); |
| 164 | + |
| 165 | + expect(params.isEmpty()).toBeFalsy(); |
| 166 | + expect(params.toString()).toNotEqual(""); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe("Test for malformed or unusual query strings", function () { |
| 171 | + var params = new UrlParams(); |
| 172 | + |
| 173 | + it("should parse a missing query string as an empty object", function () { |
| 174 | + params.parse("http://www.brackets.io?"); |
| 175 | + |
| 176 | + expect(params.isEmpty()).toBeTruthy(); |
| 177 | + expect(params.toString()).toEqual(""); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should parse a query string of whitespace as an empty object", function () { |
| 181 | + params.parse("http://www.brackets.io? "); |
| 182 | + |
| 183 | + expect(params.isEmpty()).toBeTruthy(); |
| 184 | + expect(params.toString()).toEqual(""); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should parse a random number (used to circumvent browser cache)", function () { |
| 188 | + params.parse("http://www.brackets.io?28945893575608"); |
| 189 | + |
| 190 | + expect(params.get("28945893575608")).toEqual(""); |
| 191 | + expect(params.isEmpty()).toBeFalsy(); |
| 192 | + expect(params.toString()).toEqual("28945893575608="); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments