Allow to parse request params as JSON

fixes #51
This commit is contained in:
slackersoft 2014-06-11 08:03:34 -07:00
parent 477f044b4a
commit 080bbde2b1
2 changed files with 71 additions and 12 deletions

View File

@ -107,6 +107,25 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
};
}
var paramParsers = {
naive: function(paramString) {
var data = {};
var params = paramString.split('&');
for (var i = 0; i < params.length; ++i) {
var kv = params[i].replace(/\+/g, ' ').split('=');
var key = decodeURIComponent(kv[0]);
data[key] = data[key] || [];
data[key].push(decodeURIComponent(kv[1]));
}
return data;
},
json: function(paramString) {
return JSON.parse(paramString);
}
};
function fakeRequest(requestTracker, stubTracker) {
function FakeXMLHttpRequest() {
requestTracker.track(this);
@ -157,18 +176,24 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}
},
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].replace(/\+/g, ' ').split('=');
var key = decodeURIComponent(kv[0]);
data[key] = data[key] || [];
data[key].push(decodeURIComponent(kv[1]));
contentType: function() {
for (var header in this.requestHeaders) {
if (header.toLowerCase() === 'content-type') {
return this.requestHeaders[header];
}
}
},
data: function() {
if (!this.params) {
return {};
}
if (this.contentType() === 'application/json') {
return paramParsers.json(this.params);
} else {
return paramParsers.naive(this.params);
}
return data;
},
getResponseHeader: function(name) {

View File

@ -111,16 +111,50 @@ describe("FakeXMLHttpRequest", function() {
describe("data", function() {
beforeEach(function() {
xhr.open("POST", "http://example.com?this=that");
xhr.send('3+stooges=shemp&3+stooges=larry%20%26%20moe%20%26%20curly&some%3Dthing=else+entirely');
});
it("should be an empty object if no params were sent", function() {
xhr.send();
expect(xhr.data()).toEqual({});
});
it("should return request params as a hash of arrays", function() {
xhr.send('3+stooges=shemp&3+stooges=larry%20%26%20moe%20%26%20curly&some%3Dthing=else+entirely');
var data = xhr.data();
expect(data['3 stooges'].length).toEqual(2);
expect(data['3 stooges'][0]).toEqual('shemp');
expect(data['3 stooges'][1]).toEqual('larry & moe & curly');
expect(data['some=thing']).toEqual(['else entirely']);
});
it("should parse json when the content type is appropriate", function() {
var data = {
foo: 'bar',
baz: ['q', 'u', 'u', 'x'],
nested: {
object: {
with: 'stuff'
}
}
};
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
expect(xhr.data()).toEqual(data);
});
});
describe("contentType", function() {
it("gets the Content-Type", function() {
xhr.setRequestHeader('Content-Type', 'something');
expect(xhr.contentType()).toEqual('something');
});
it("gets the content-type even when the casing is not to spec", function() {
xhr.setRequestHeader('content-Type', 'some other thing');
expect(xhr.contentType()).toEqual('some other thing');
});
});
describe("when a fake XMLHttpRequest is created", function() {