Merge lp:~allenap/launchpad/deriveDistroSeries-json-weirdness-bug-753249 into lp:launchpad

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 12925
Proposed branch: lp:~allenap/launchpad/deriveDistroSeries-json-weirdness-bug-753249
Merge into: lp:launchpad
Diff against target: 117 lines (+51/-22)
2 files modified
lib/lp/app/javascript/client.js (+12/-8)
lib/lp/app/javascript/tests/test_lp_client.js (+39/-14)
To merge this branch: bzr merge lp:~allenap/launchpad/deriveDistroSeries-json-weirdness-bug-753249
Reviewer Review Type Date Requested Status
Gary Poster (community) Approve
Review via email: mp+59120@code.launchpad.net

Commit message

[r=gary][bug=753249] Enable append_qs() to deal with array argument values.

Description of the change

append_qs() in client.js could not deal with array argument values
properly. This is something that any API call that accepts a list
needs, so I've implemented it.

I've manually verified that this fixes the linked bug. I started work
on a Windmill test, but got stupid errors from Windmill regarding
profiles in Firefox. Purging Firefox and re-installing makes no
difference. I've dumped the work I've started into another branch and
I'll file a bug for that. Meanwhile I want to get this landed.

To post a comment you must log in.
Revision history for this message
Gary Poster (gary) wrote :

Very nice, Gavin. Thank you.

If you haven't run "make lint," please do--I have been led to believe that we now have Crockford's jslint hooked up.

As a matter of sharing a discussion we've been having, we've been talking about putting "var" outside of for statements, to accurately represent scope. Crockford apparently recommends this. Do what you will, if make lint does not complain. :-)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/app/javascript/client.js'
--- lib/lp/app/javascript/client.js 2011-04-19 18:14:20 +0000
+++ lib/lp/app/javascript/client.js 2011-04-26 20:56:03 +0000
@@ -86,14 +86,18 @@
86// Generally useful functions.86// Generally useful functions.
87module.append_qs = function(qs, key, value) {87module.append_qs = function(qs, key, value) {
88 /* Append a key-value pair to a query string. */88 /* Append a key-value pair to a query string. */
89 if (qs === undefined) {89 var elems = (qs && qs.length > 0) ? [qs] : [];
90 qs = "";90 var enc = encodeURIComponent;
91 }91 if (Y.Lang.isArray(value)) {
92 if (qs.length > 0) {92 var index;
93 qs += '&';93 for (index = 0; index < value.length; index++) {
94 }94 elems.push(enc(key) + "=" + enc(value[index]));
95 qs += encodeURIComponent(key) + "=" + encodeURIComponent(value);95 }
96 return qs;96 }
97 else {
98 elems.push(enc(key) + "=" + enc(value));
99 }
100 return elems.join("&");
97};101};
98102
99module.normalize_uri = function(uri) {103module.normalize_uri = function(uri) {
100104
=== modified file 'lib/lp/app/javascript/tests/test_lp_client.js'
--- lib/lp/app/javascript/tests/test_lp_client.js 2011-04-11 12:52:27 +0000
+++ lib/lp/app/javascript/tests/test_lp_client.js 2011-04-26 20:56:03 +0000
@@ -19,12 +19,19 @@
1919
20 test_normalize_uri: function() {20 test_normalize_uri: function() {
21 var normalize = Y.lp.client.normalize_uri;21 var normalize = Y.lp.client.normalize_uri;
22 Assert.areEqual(normalize("http://www.example.com/api/devel/foo"), "/api/devel/foo");22 Assert.areEqual(
23 Assert.areEqual(normalize("http://www.example.com/foo/bar"), "/foo/bar");23 normalize("http://www.example.com/api/devel/foo"),
24 Assert.areEqual(normalize("/foo/bar"), "/api/devel/foo/bar");24 "/api/devel/foo");
25 Assert.areEqual(normalize("/api/devel/foo/bar"), "/api/devel/foo/bar");25 Assert.areEqual(
26 Assert.areEqual(normalize("foo/bar"), "/api/devel/foo/bar");26 normalize("http://www.example.com/foo/bar"), "/foo/bar");
27 Assert.areEqual(normalize("api/devel/foo/bar"), "/api/devel/foo/bar");27 Assert.areEqual(
28 normalize("/foo/bar"), "/api/devel/foo/bar");
29 Assert.areEqual(
30 normalize("/api/devel/foo/bar"), "/api/devel/foo/bar");
31 Assert.areEqual(
32 normalize("foo/bar"), "/api/devel/foo/bar");
33 Assert.areEqual(
34 normalize("api/devel/foo/bar"), "/api/devel/foo/bar");
28 },35 },
2936
30 test_append_qs: function() {37 test_append_qs: function() {
@@ -33,14 +40,28 @@
33 Assert.areEqual("P%C3%83%C2%B6ll%C3%83%C2%A4=Perell%C3%83%C2%B3", qs);40 Assert.areEqual("P%C3%83%C2%B6ll%C3%83%C2%A4=Perell%C3%83%C2%B3", qs);
34 },41 },
3542
43 test_append_qs_with_array: function() {
44 // append_qs() appends multiple arguments to the query string
45 // when a parameter value is an array.
46 var qs = "";
47 qs = Y.lp.client.append_qs(qs, "foo", ["bar", "baz"]);
48 Assert.areEqual("foo=bar&foo=baz", qs);
49 // All values in the array are encoded correctly too.
50 qs = Y.lp.client.append_qs(qs, "a&b", ["a+b"]);
51 Assert.areEqual("foo=bar&foo=baz&a%26b=a%2Bb", qs);
52 },
53
36 test_field_uri: function() {54 test_field_uri: function() {
37 var get_field_uri = Y.lp.client.get_field_uri;55 var get_field_uri = Y.lp.client.get_field_uri;
38 Assert.areEqual(get_field_uri("http://www.example.com/api/devel/foo", "field"),56 Assert.areEqual(
39 "/api/devel/foo/field");57 get_field_uri("http://www.example.com/api/devel/foo", "field"),
40 Assert.areEqual(get_field_uri("/no/slash", "field"),58 "/api/devel/foo/field");
41 "/api/devel/no/slash/field");59 Assert.areEqual(
42 Assert.areEqual(get_field_uri("/has/slash/", "field"),60 get_field_uri("/no/slash", "field"),
43 "/api/devel/has/slash/field");61 "/api/devel/no/slash/field");
62 Assert.areEqual(
63 get_field_uri("/has/slash/", "field"),
64 "/api/devel/has/slash/field");
44 }65 }
45}));66}));
4667
@@ -90,7 +111,9 @@
90 'lp_html': {'first': "<p>Hello</p><p>World</p>"}111 'lp_html': {'first': "<p>Hello</p><p>World</p>"}
91 };112 };
92 var entry = new Y.lp.client.Entry(null, entry_repr, "a_self_link");113 var entry = new Y.lp.client.Entry(null, entry_repr, "a_self_link");
93 Assert.areEqual("<p>Hello</p><p>World</p>", entry.getHTML('first').get('innerHTML'));114 Assert.areEqual(
115 "<p>Hello</p><p>World</p>",
116 entry.getHTML('first').get('innerHTML'));
94 // If there is no html representation, null is returned.117 // If there is no html representation, null is returned.
95 Assert.areEqual(null, entry.getHTML('second'));118 Assert.areEqual(null, entry.getHTML('second'));
96 },119 },
@@ -152,7 +175,9 @@
152 Assert.areEqual('first', first_event.name);175 Assert.areEqual('first', first_event.name);
153 Assert.areEqual('Hello', first_event.old_value);176 Assert.areEqual('Hello', first_event.old_value);
154 Assert.areEqual('World<boo/>', first_event.new_value);177 Assert.areEqual('World<boo/>', first_event.new_value);
155 Assert.areEqual('<p>World html<boo></boo></p>', first_event.new_value_html.get('innerHTML'));178 Assert.areEqual(
179 '<p>World html<boo></boo></p>',
180 first_event.new_value_html.get('innerHTML'));
156 Assert.areEqual(entry, first_event.entry);181 Assert.areEqual(entry, first_event.entry);
157182
158 Assert.areEqual('second', second_event.name);183 Assert.areEqual('second', second_event.name);