From 848e7556dc42af550f3a689e40203c9ac4a4c2cb Mon Sep 17 00:00:00 2001 From: Jake Dluhy Date: Mon, 3 Apr 2017 18:54:35 -0700 Subject: [PATCH 1/3] Add support for parsing json response --- fake_xml_http_request.js | 6 ++++++ src/fake-xml-http-request.js | 6 ++++++ test/responding_test.js | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/fake_xml_http_request.js b/fake_xml_http_request.js index 86b15d2..60fdcdd 100644 --- a/fake_xml_http_request.js +++ b/fake_xml_http_request.js @@ -447,6 +447,12 @@ } catch (e) { // Unable to parse XML - no biggie } + } else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { + try { + this.response = JSON.parse(this.responseText); + } catch (e) { + // Unable to parse JSON - no biggie + } } if (this.async) { diff --git a/src/fake-xml-http-request.js b/src/fake-xml-http-request.js index ae46d77..b3d5b4e 100644 --- a/src/fake-xml-http-request.js +++ b/src/fake-xml-http-request.js @@ -441,6 +441,12 @@ var FakeXMLHttpRequestProto = { } catch (e) { // Unable to parse XML - no biggie } + } else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { + try { + this.response = JSON.parse(this.responseText); + } catch (e) { + // Unable to parse JSON - no biggie + } } if (this.async) { diff --git a/test/responding_test.js b/test/responding_test.js index 6db50c2..34696fe 100644 --- a/test/responding_test.js +++ b/test/responding_test.js @@ -61,6 +61,17 @@ test("does not parse the body if it's XML and another content type is set", func equal(xhr.responseXML, undefined); }); +test("parses the body if it's JSON and the json content type is set", function(){ + const body = { key: 'value' }; + xhr.respond(200, {'Content-Type':'application/json'}, JSON.stringify(body)); + deepEqual(xhr.response, body); +}); + +test("does not parse the JSON body when another content type is set", function() { + xhr.respond(200, {'Content-Type':'application/xml'}, '{"a":"key"}'); + equal(xhr.response, undefined); +}); + test("calls the onload callback once", function(){ var wasCalled = 0; From adf6e15a47478abea911ff0016606deb1bef726f Mon Sep 17 00:00:00 2001 From: Jake Dluhy Date: Fri, 2 Mar 2018 12:03:27 -0800 Subject: [PATCH 2/3] Make CR changes: change if spacing and error catching --- fake_xml_http_request.js | 4 ++-- src/fake-xml-http-request.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fake_xml_http_request.js b/fake_xml_http_request.js index 60fdcdd..2fb09c8 100644 --- a/fake_xml_http_request.js +++ b/fake_xml_http_request.js @@ -447,11 +447,11 @@ } catch (e) { // Unable to parse XML - no biggie } - } else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { + } else if (this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { try { this.response = JSON.parse(this.responseText); } catch (e) { - // Unable to parse JSON - no biggie + if (!(e instanceof SyntaxError)) throw e; } } diff --git a/src/fake-xml-http-request.js b/src/fake-xml-http-request.js index b3d5b4e..85fdfb7 100644 --- a/src/fake-xml-http-request.js +++ b/src/fake-xml-http-request.js @@ -441,11 +441,11 @@ var FakeXMLHttpRequestProto = { } catch (e) { // Unable to parse XML - no biggie } - } else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { + } else if (this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) { try { this.response = JSON.parse(this.responseText); } catch (e) { - // Unable to parse JSON - no biggie + if (!(e instanceof SyntaxError)) throw e; } } From f43cdf4a80ee4f81c337bcd072dd7c4f19c4c3aa Mon Sep 17 00:00:00 2001 From: Jake Dluhy Date: Fri, 2 Mar 2018 12:06:18 -0800 Subject: [PATCH 3/3] Update README detailing the additional response key --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cea44c1..e475566 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ xhr.respond(200, {"Content-Type": "application/json"}, '{"key":"value"}'); xhr.status; // 200 xhr.statusText; // "OK" xhr.responseText; // '{"key":"value"}' +xhr.response; // { key: "value" } (parsed JSON object) // simulate failed response xhr = new FakeXMLHttpRequest(); @@ -48,4 +49,4 @@ karma start In order to have a more open and welcoming community this project adheres to a [code of conduct](CONDUCT.md) adapted from the [contributor covenant](http://contributor-covenant.org/). -Please adhere to this code of conduct in any interactions you have with this project's community. If you encounter someone violating these terms, please let a maintainer (@trek) know and we will address it as soon as possible. \ No newline at end of file +Please adhere to this code of conduct in any interactions you have with this project's community. If you encounter someone violating these terms, please let a maintainer (@trek) know and we will address it as soon as possible.