Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix: handle not JSON content in response parsing (#59)
* Coveralls Badge Fix

* Coveralls Badge Fix:Final

* Coveralls Badge Fix : istanbul package added

* Coveralls Badge Fix + snyk added

* Coveralls Badge Fix + snyk added

* Snyk removed from Makefile

* Snyk removed for timebeing

* Add more code coverage for OAuthClient (#54)

* Add better code coverage in OAuthClient

* Fix: ValidateIdToken method and unit tests (#58)

* Fix validateIdToken tests

* Pointing README Badge to Develop

* Pointing README Badge to Develop

* Update Develop Coverage Badge

* Fix: handle not JSON content in response parsing

Some intuit API as invoice download return not JSON content (PDF in this
case). `makeApiCall` wasn't working with it because of mandatory
response body parsing.

So if the response is not JSON, we don't want to parse the body.
And simply let the caller decide what to do with it.

Co-authored-by: abisalehalliprasan <[email protected]>
Co-authored-by: Oscar Rabasa <[email protected]>
Co-authored-by: Kevin Tang <[email protected]>
Co-authored-by: abisalehalliprasan <[email protected]>
  • Loading branch information
5 people authored Apr 10, 2020
commit dd84b3bd2ad19d573128a2a8c28b6ea79b6772a1
2 changes: 1 addition & 1 deletion src/response/AuthResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function AuthResponse(params) {
AuthResponse.prototype.processResponse = function processResponse(response) {
this.response = response || '';
this.body = (response && response.body) || '';
this.json = this.body ? JSON.parse(this.body) : null;
this.json = this.body && this.isJson() ? JSON.parse(this.body) : null;
this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || '';
};

Expand Down
66 changes: 66 additions & 0 deletions test/AuthResponseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const OAuthClientTest = require('../src/OAuthClient');
const AuthResponse = require('../src/response/AuthResponse');
const expectedAccessToken = require('./mocks/bearer-token.json');
const expectedResponseMock = require('./mocks/response.json');
const expectedPdfResponseMock = require('./mocks/pdfResponse.json');


const oauthClient = new OAuthClientTest({
Expand Down Expand Up @@ -120,3 +121,68 @@ describe('Tests for AuthResponse', () => {
expect(() => authResponse.processResponse(null)).to.not.throw();
});
});

describe('Tests for AuthResponse with not json content', () => {
let authResponse;
let getStub;
let expectedResponse;

beforeEach(() => {
expectedResponse = JSON.parse(JSON.stringify(expectedPdfResponseMock));
getStub = sinon.stub().returns('application/pdf');
expectedResponse.get = getStub;

authResponse = new AuthResponse({ token: oauthClient.getToken() });
authResponse.processResponse(expectedResponse);
});

afterEach(() => {
getStub.reset();
});

it('Creates a new auth response instance', () => {
expect(authResponse).to.have.property('token');
expect(authResponse).to.have.property('response');
expect(authResponse).to.have.property('body');
expect(authResponse).to.have.property('json');
expect(authResponse).to.have.property('intuit_tid');
});

it('Process Response', () => {
authResponse.processResponse(expectedResponse);
expect(authResponse.response).to.deep.equal(expectedResponse);
});

it('Process Get Token', () => {
const token = authResponse.getToken();
expect(token).to.have.property('token_type');
expect(token).to.have.property('refresh_token');
expect(token).to.have.property('expires_in');
expect(token).to.have.property('x_refresh_token_expires_in');
});

it('Process Text() when there is body ', () => {
const text = authResponse.text();
expect(text).to.be.a('string');
expect(text).to.be.equal('%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>');
});

it('Process Status of AuthResponse', () => {
const status = authResponse.status();
expect(status).to.be.equal(200);
});

it('Process Headers of AuthResponse', () => {
const headers = authResponse.headers();
expect(headers).to.be.equal(expectedResponse.headers);
});

it('Process Get Json to throw an error', () => {
expect(() => authResponse.getJson()).to.throw(Error);
});

it('GetContentType should handle False', () => {
const contentType = authResponse.getContentType();
expect(contentType).to.be.equal('application/pdf');
});
});
16 changes: 16 additions & 0 deletions test/mocks/pdfResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"url":"https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer",
"headers":{
"content-type":"application/pdf",
"content-length":"1636",
"connection":"close",
"server":"nginx",
"date":"Wed, 05 Sep 2018 05:57:09 GMT",
"intuit_tid":"1234-1234-1234-123",
"cache-control":"no-cache, no-store",
"pragma":"no-cache"
},
"body":"%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>",
"status":200,
"statusText":"OK"
}