-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fetch: handle invalid server responses, set responseType (#329)
- Loading branch information
1 parent
ff6d813
commit 56d77c9
Showing
3 changed files
with
188 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,178 @@ | ||
/* global Map */ | ||
describe('fetchRequest', function () { | ||
var mockFetchResult; | ||
var mockFetchObj = { | ||
let fetchSpy; | ||
|
||
let requestHeaders; | ||
let requestMethod; | ||
let requestUrl; | ||
let response; | ||
let responseHeaders; | ||
let responseJSON; | ||
let responseText; | ||
|
||
const mockFetchObj = { | ||
fetch: function mockFetchFunc() { | ||
return Promise.resolve(mockFetchResult); | ||
return Promise.resolve(response); | ||
} | ||
} | ||
jest.setMock('cross-fetch', function() { | ||
return mockFetchObj.fetch.apply(null, arguments); | ||
}); | ||
|
||
var fetchRequest = require('../../lib/fetch/fetchRequest'); | ||
const fetchRequest = require('../../lib/fetch/fetchRequest'); | ||
|
||
beforeEach(function() { | ||
/* global Map */ | ||
mockFetchResult = { | ||
headers: new Map(), | ||
fetchSpy = jest.spyOn(mockFetchObj, 'fetch'); | ||
responseHeaders = new Map(); | ||
responseHeaders.set('Content-Type', 'application/json'); | ||
responseJSON = { isFakeResponse: true }; | ||
responseText = JSON.stringify(responseJSON); | ||
response = { | ||
headers: responseHeaders, | ||
status: 200, | ||
ok: true, | ||
json: function() { | ||
return Promise.resolve(); | ||
return Promise.resolve(responseJSON); | ||
}, | ||
text: function() { | ||
return Promise.resolve(); | ||
return Promise.resolve(responseText); | ||
} | ||
}; | ||
|
||
requestHeaders = { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
} | ||
requestMethod = 'GET'; | ||
requestUrl = 'http://fakey.local'; | ||
}); | ||
|
||
it('JSON encodes request body if request Content-Type is application/json', function() { | ||
var spy = jest.spyOn(mockFetchObj, 'fetch'); | ||
var method = 'GET'; | ||
var url = 'http://fakey.local'; | ||
var headers = { | ||
'Content-Type': 'application/json' | ||
}; | ||
var obj = { | ||
foo: 'bar' | ||
}; | ||
var jsonObj = JSON.stringify(obj); | ||
describe('request', () => { | ||
it('JSON encodes request body if request header Content-Type is application/json', function() { | ||
const requestJSON = { | ||
foo: 'bar' | ||
}; | ||
return fetchRequest(requestMethod, requestUrl, { | ||
headers: requestHeaders, | ||
data: requestJSON | ||
}) | ||
.then(() => { | ||
expect(fetchSpy).toHaveBeenCalledWith(requestUrl, { | ||
method: requestMethod, | ||
headers: requestHeaders, | ||
body: JSON.stringify(requestJSON), | ||
credentials: 'include' | ||
}); | ||
}); | ||
}); | ||
|
||
fetchRequest(method, url, { | ||
headers: headers, | ||
data: obj | ||
it('Leaves request body unchanged if request header Content-Type is NOT application/json', function() { | ||
requestHeaders = { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}; | ||
const requestText = 'string=1&fake=2'; | ||
return fetchRequest(requestMethod, requestUrl, { | ||
headers: requestHeaders, | ||
data: requestText | ||
}) | ||
.then(() => { | ||
expect(fetchSpy).toHaveBeenCalledWith(requestUrl, { | ||
method: requestMethod, | ||
headers: requestHeaders, | ||
body: requestText, | ||
credentials: 'include' | ||
}); | ||
}); | ||
}); | ||
|
||
expect(spy).toHaveBeenCalledWith(url, { | ||
method: method, | ||
headers: headers, | ||
body: jsonObj, | ||
credentials: 'include' | ||
|
||
it('Can omit credentials', function() { | ||
return fetchRequest(requestMethod, requestUrl, { | ||
withCredentials: false | ||
}) | ||
.then(() => { | ||
expect(fetchSpy).toHaveBeenCalledWith(requestUrl, { | ||
method: requestMethod, | ||
credentials: 'omit' | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('Leaves request body unchanged if request Content-Type is NOT application/json', function() { | ||
var spy = jest.spyOn(mockFetchObj, 'fetch'); | ||
var method = 'GET'; | ||
var url = 'http://fakey.local'; | ||
var obj = { | ||
foo: 'bar' | ||
}; | ||
describe('response', () => { | ||
|
||
fetchRequest(method, url, { | ||
data: obj | ||
it('Returns JSON if response header Content-Type is application/json', function() { | ||
return fetchRequest(requestMethod, requestUrl, {}) | ||
.then(res => { | ||
expect(res).toEqual({ | ||
status: response.status, | ||
responseJSON, | ||
responseText, | ||
responseType: 'json' | ||
}); | ||
}); | ||
}); | ||
|
||
expect(spy).toHaveBeenCalledWith(url, { | ||
method: method, | ||
body: obj, | ||
credentials: 'include' | ||
it('Returns text if response header Content-Type is NOT application/json', function() { | ||
responseHeaders.set('Content-Type', 'application/x-www-form-urlencoded'); | ||
return fetchRequest(requestMethod, requestUrl, {}) | ||
.then(res => { | ||
expect(res).toEqual({ | ||
status: response.status, | ||
responseText | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('Can omit credentials', function() { | ||
var spy = jest.spyOn(mockFetchObj, 'fetch'); | ||
var method = 'GET'; | ||
var url = 'http://fakey.local'; | ||
it('Throws the response if response.ok is false (JSON)', () => { | ||
response.status = 401; | ||
response.ok = false; | ||
return fetchRequest(requestMethod, requestUrl, {}) | ||
.catch(err => { | ||
expect(err).toEqual({ | ||
status: response.status, | ||
responseText, | ||
responseType: 'json', | ||
responseJSON | ||
}); | ||
}); | ||
}); | ||
|
||
fetchRequest(method, url, { | ||
withCredentials: false | ||
it('Throws the response if response.ok is false (text)', () => { | ||
response.status = 401; | ||
response.ok = false; | ||
responseHeaders.set('Content-Type', 'application/x-www-form-urlencoded'); | ||
return fetchRequest(requestMethod, requestUrl, {}) | ||
.catch(err => { | ||
expect(err).toEqual({ | ||
status: response.status, | ||
responseText | ||
}); | ||
}); | ||
}); | ||
|
||
expect(spy).toHaveBeenCalledWith(url, { | ||
method: method, | ||
credentials: 'omit' | ||
it('Throws the response if response.ok is false (invalid JSON)', () => { | ||
var error = new Error('A fake error, ignore me'); | ||
response.status = 401; | ||
response.ok = false; | ||
response.json = function() { | ||
return Promise.reject(error); | ||
}; | ||
|
||
var errorJSON = { | ||
error: error, | ||
errorSummary: 'Could not parse server response' | ||
}; | ||
|
||
return fetchRequest(requestMethod, requestUrl, {}) | ||
.catch(err => { | ||
expect(err).toEqual({ | ||
status: response.status, | ||
responseText: JSON.stringify(errorJSON), | ||
responseJSON: errorJSON, | ||
responseType: 'json' | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |