-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
223 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var window = require('global/window'); | ||
|
||
const httpResponseHandler = (callback, decodeResponseBody = false) => (err, response, responseBody) => { | ||
// if the XHR failed, return that error | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
// if the HTTP status code is 4xx or 5xx, the request also failed | ||
if (response.statusCode >= 400 && response.statusCode <= 599) { | ||
let cause = responseBody; | ||
|
||
if (decodeResponseBody) { | ||
if (window.TextDecoder) { | ||
const charset = getCharset(response.headers && response.headers['content-type']); | ||
|
||
try { | ||
cause = new TextDecoder(charset).decode(responseBody); | ||
} catch (e) { | ||
} | ||
} else { | ||
cause = String.fromCharCode.apply(null, new Uint8Array(responseBody)); | ||
} | ||
} | ||
|
||
callback({cause}); | ||
return; | ||
} | ||
|
||
// otherwise, request succeeded | ||
callback(null, responseBody); | ||
}; | ||
|
||
function getCharset(contentTypeHeader = '') { | ||
return contentTypeHeader | ||
.toLowerCase() | ||
.split(';') | ||
.reduce((charset, contentType) => { | ||
const [type, value] = contentType.split('='); | ||
|
||
if (type.trim() === 'charset') { | ||
return value.trim(); | ||
} | ||
|
||
return charset; | ||
}, 'utf-8'); | ||
} | ||
|
||
module.exports = httpResponseHandler; |
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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
var window = require("global/window") | ||
var test = require("tape") | ||
var forEach = require("for-each") | ||
|
||
var httpHandler = require("../http-handler.js") | ||
|
||
function toArrayBuffer(item) { | ||
const buffer = new ArrayBuffer(item.length); | ||
const bufferView = new Uint8Array(buffer); | ||
|
||
for (let i = 0; i < item.length; i++) { | ||
bufferView[i] = item.charCodeAt(i); | ||
} | ||
return buffer; | ||
} | ||
|
||
test('httpHandler takes a callback and returns a method of arity 3', function(assert) { | ||
const xhrHandler = httpHandler(() => {}); | ||
|
||
assert.equal(xhrHandler.length, 3); | ||
assert.end(); | ||
}); | ||
|
||
|
||
test('httpHandler returns responseBody to callback if no error and success http status code', function(assert) { | ||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(body, 'hello'); | ||
}); | ||
|
||
xhrHandler(null, { statusCode: 200 }, 'hello'); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler passes error to callback', function(assert) { | ||
const error = new Error('the error'); | ||
|
||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err, error); | ||
}); | ||
|
||
xhrHandler(error, null, 'hello'); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler passes error to callback', function(assert) { | ||
const error = new Error('the error'); | ||
|
||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err, error); | ||
}); | ||
|
||
xhrHandler(error, null, 'hello'); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler returns responseBody as cause for 4xx/5xx responses', function(assert) { | ||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err.cause, "can't touch this"); | ||
}); | ||
|
||
xhrHandler(null, { statusCode: 403 }, "can't touch this"); | ||
xhrHandler(null, { statusCode: 504 }, "can't touch this"); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler decodes responseBody using TextDecoder for 4xx/5xx responses', function(assert) { | ||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err.cause, "can't touch this"); | ||
}, true); | ||
|
||
xhrHandler(null, { statusCode: 403 }, toArrayBuffer("can't touch this")); | ||
xhrHandler(null, { statusCode: 504 }, toArrayBuffer("can't touch this")); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler decodes responseBody using TextDecoder for 4xx/5xx responses', function(assert) { | ||
let xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err.cause, ""); | ||
}, true); | ||
|
||
xhrHandler(null, { statusCode: 403 }, toArrayBuffer("")); | ||
|
||
xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err.cause, null); | ||
}, true); | ||
xhrHandler(null, { statusCode: 504 }, null); | ||
assert.end(); | ||
}); | ||
|
||
test('httpHandler decodes responseBody using fromCharCode if TextDecoder is unavailable for 4xx/5xx responses', function(assert) { | ||
const TextDecoder = window.TextDecoder; | ||
|
||
window.TextDecoder = null; | ||
|
||
const xhrHandler = httpHandler((err, body) => { | ||
assert.equal(err.cause, "can't touch this"); | ||
}, true); | ||
|
||
xhrHandler(null, { statusCode: 403 }, toArrayBuffer("can't touch this")); | ||
xhrHandler(null, { statusCode: 504 }, toArrayBuffer("can't touch this")); | ||
|
||
window.TextDecoder = TextDecoder; | ||
assert.end(); | ||
}); |
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