return request body params in a consistent and usable way

This commit is contained in:
Elliot Foster & Yuri Green 2012-05-16 11:40:24 -07:00
parent ddd5c50e9e
commit fa9a44a659
2 changed files with 30 additions and 0 deletions

View File

@ -78,6 +78,21 @@ function FakeXMLHttpRequest() {
this.readyState = 2; this.readyState = 2;
}, },
data: function() {
var data = {};
if (typeof this.params !== 'string') return data;
var params = this.params.split('&');
for (var i = 0; i < params.length; ++i) {
var kv = params[i].split('=');
var key = decodeURIComponent(kv[0]);
data[key] = data[key] || [];
data[key].push(decodeURIComponent(kv[1]));
data[key].sort();
}
return data;
},
getResponseHeader: function(name) { getResponseHeader: function(name) {
return this.responseHeaders[name]; return this.responseHeaders[name];
}, },

View File

@ -42,4 +42,19 @@ describe("FakeXMLHttpRequest", function() {
} }
expect(new FakeXMLHttpRequest().foo()).toEqual("foo"); expect(new FakeXMLHttpRequest().foo()).toEqual("foo");
}); });
describe("data", function() {
beforeEach(function() {
xhr.open("POST", "http://example.com?this=that")
xhr.send('stooges=shemp&stooges=larry%20%26%20moe%20%26%20curly&some%3Dthing=else')
});
it("should return request params as a hash of arrays with values sorted alphabetically", function() {
var data = xhr.data();
expect(data['stooges'].length).toEqual(2);
expect(data['stooges'][0]).toEqual('larry & moe & curly');
expect(data['stooges'][1]).toEqual('shemp');
expect(data['some=thing']).toEqual(['else']);
});
});
}); });