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
1=== modified file 'lib/lp/app/javascript/client.js'
2--- lib/lp/app/javascript/client.js 2011-04-19 18:14:20 +0000
3+++ lib/lp/app/javascript/client.js 2011-04-26 20:56:03 +0000
4@@ -86,14 +86,18 @@
5 // Generally useful functions.
6 module.append_qs = function(qs, key, value) {
7 /* Append a key-value pair to a query string. */
8- if (qs === undefined) {
9- qs = "";
10- }
11- if (qs.length > 0) {
12- qs += '&';
13- }
14- qs += encodeURIComponent(key) + "=" + encodeURIComponent(value);
15- return qs;
16+ var elems = (qs && qs.length > 0) ? [qs] : [];
17+ var enc = encodeURIComponent;
18+ if (Y.Lang.isArray(value)) {
19+ var index;
20+ for (index = 0; index < value.length; index++) {
21+ elems.push(enc(key) + "=" + enc(value[index]));
22+ }
23+ }
24+ else {
25+ elems.push(enc(key) + "=" + enc(value));
26+ }
27+ return elems.join("&");
28 };
29
30 module.normalize_uri = function(uri) {
31
32=== modified file 'lib/lp/app/javascript/tests/test_lp_client.js'
33--- lib/lp/app/javascript/tests/test_lp_client.js 2011-04-11 12:52:27 +0000
34+++ lib/lp/app/javascript/tests/test_lp_client.js 2011-04-26 20:56:03 +0000
35@@ -19,12 +19,19 @@
36
37 test_normalize_uri: function() {
38 var normalize = Y.lp.client.normalize_uri;
39- Assert.areEqual(normalize("http://www.example.com/api/devel/foo"), "/api/devel/foo");
40- Assert.areEqual(normalize("http://www.example.com/foo/bar"), "/foo/bar");
41- Assert.areEqual(normalize("/foo/bar"), "/api/devel/foo/bar");
42- Assert.areEqual(normalize("/api/devel/foo/bar"), "/api/devel/foo/bar");
43- Assert.areEqual(normalize("foo/bar"), "/api/devel/foo/bar");
44- Assert.areEqual(normalize("api/devel/foo/bar"), "/api/devel/foo/bar");
45+ Assert.areEqual(
46+ normalize("http://www.example.com/api/devel/foo"),
47+ "/api/devel/foo");
48+ Assert.areEqual(
49+ normalize("http://www.example.com/foo/bar"), "/foo/bar");
50+ Assert.areEqual(
51+ normalize("/foo/bar"), "/api/devel/foo/bar");
52+ Assert.areEqual(
53+ normalize("/api/devel/foo/bar"), "/api/devel/foo/bar");
54+ Assert.areEqual(
55+ normalize("foo/bar"), "/api/devel/foo/bar");
56+ Assert.areEqual(
57+ normalize("api/devel/foo/bar"), "/api/devel/foo/bar");
58 },
59
60 test_append_qs: function() {
61@@ -33,14 +40,28 @@
62 Assert.areEqual("P%C3%83%C2%B6ll%C3%83%C2%A4=Perell%C3%83%C2%B3", qs);
63 },
64
65+ test_append_qs_with_array: function() {
66+ // append_qs() appends multiple arguments to the query string
67+ // when a parameter value is an array.
68+ var qs = "";
69+ qs = Y.lp.client.append_qs(qs, "foo", ["bar", "baz"]);
70+ Assert.areEqual("foo=bar&foo=baz", qs);
71+ // All values in the array are encoded correctly too.
72+ qs = Y.lp.client.append_qs(qs, "a&b", ["a+b"]);
73+ Assert.areEqual("foo=bar&foo=baz&a%26b=a%2Bb", qs);
74+ },
75+
76 test_field_uri: function() {
77 var get_field_uri = Y.lp.client.get_field_uri;
78- Assert.areEqual(get_field_uri("http://www.example.com/api/devel/foo", "field"),
79- "/api/devel/foo/field");
80- Assert.areEqual(get_field_uri("/no/slash", "field"),
81- "/api/devel/no/slash/field");
82- Assert.areEqual(get_field_uri("/has/slash/", "field"),
83- "/api/devel/has/slash/field");
84+ Assert.areEqual(
85+ get_field_uri("http://www.example.com/api/devel/foo", "field"),
86+ "/api/devel/foo/field");
87+ Assert.areEqual(
88+ get_field_uri("/no/slash", "field"),
89+ "/api/devel/no/slash/field");
90+ Assert.areEqual(
91+ get_field_uri("/has/slash/", "field"),
92+ "/api/devel/has/slash/field");
93 }
94 }));
95
96@@ -90,7 +111,9 @@
97 'lp_html': {'first': "<p>Hello</p><p>World</p>"}
98 };
99 var entry = new Y.lp.client.Entry(null, entry_repr, "a_self_link");
100- Assert.areEqual("<p>Hello</p><p>World</p>", entry.getHTML('first').get('innerHTML'));
101+ Assert.areEqual(
102+ "<p>Hello</p><p>World</p>",
103+ entry.getHTML('first').get('innerHTML'));
104 // If there is no html representation, null is returned.
105 Assert.areEqual(null, entry.getHTML('second'));
106 },
107@@ -152,7 +175,9 @@
108 Assert.areEqual('first', first_event.name);
109 Assert.areEqual('Hello', first_event.old_value);
110 Assert.areEqual('World<boo/>', first_event.new_value);
111- Assert.areEqual('<p>World html<boo></boo></p>', first_event.new_value_html.get('innerHTML'));
112+ Assert.areEqual(
113+ '<p>World html<boo></boo></p>',
114+ first_event.new_value_html.get('innerHTML'));
115 Assert.areEqual(entry, first_event.entry);
116
117 Assert.areEqual('second', second_event.name);