Skip to content

Commit

Permalink
Add support for response type with xhr
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Aug 5, 2015
1 parent 2a4656e commit 75ab1bd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The default plugins under node are `[stringify, headers, cookieJar, unzip, conca
**Options using browser transport**

* **withCredentials** Send cookies with CORS requests (default: `false`)
* **responseType** Set the XHR `responseType` (default: `undefined`)

#### Short-hand Methods

Expand Down
13 changes: 12 additions & 1 deletion lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function open (request: Request) {
return new Promise(function (resolve, reject) {
const url = request.fullUrl()
const method = request.method
const responseType = request.options.responseType

// Loading HTTP resources from HTTPS is restricted and uncatchable.
if (window.location.protocol === 'https:' && /^http\:/.test(url)) {
Expand All @@ -28,7 +29,7 @@ function open (request: Request) {
return resolve({
status: xhr.status === 1223 ? 204 : xhr.status,
headers: getHeaders(xhr.getAllResponseHeaders()),
body: xhr.responseText,
body: responseType ? xhr.response : xhr.responseText,
url: xhr.responseURL
})
}
Expand Down Expand Up @@ -76,6 +77,16 @@ function open (request: Request) {
xhr.withCredentials = true
}

if (responseType) {
try {
xhr.responseType = responseType
} finally {
if (xhr.responseType !== responseType) {
throw request.error(`Unsupported response type: ${responseType}`, 'ERESPONSETYPE')
}
}
}

// Set all headers with original casing.
Object.keys(request.headers).forEach(function (header) {
xhr.setRequestHeader(request.name(header), request.get(header))
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ function stringifyRequest (request: Request) {
function parseResponse (response: Response) {
const body = response.body

if (typeof body !== 'string') {
return
}

if (body === '') {
response.body = null

Expand Down
13 changes: 12 additions & 1 deletion popsicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ function open(request) {
return new Promise(function (resolve, reject) {
var url = request.fullUrl();
var method = request.method;
var responseType = request.options.responseType;
if (window.location.protocol === 'https:' && /^http\:/.test(url)) {
return reject(request.error("The request to \"" + url + "\" was blocked", 'EBLOCKED'));
}
Expand All @@ -996,7 +997,7 @@ function open(request) {
return resolve({
status: xhr.status === 1223 ? 204 : xhr.status,
headers: get_headers_1.parse(xhr.getAllResponseHeaders()),
body: xhr.responseText,
body: responseType ? xhr.response : xhr.responseText,
url: xhr.responseURL
});
};
Expand Down Expand Up @@ -1033,6 +1034,16 @@ function open(request) {
if (request.options.withCredentials) {
xhr.withCredentials = true;
}
if (responseType) {
try {
xhr.responseType = responseType;
}
finally {
if (xhr.responseType !== responseType) {
throw request.error("Unsupported response type: " + responseType, 'ERESPONSETYPE');
}
}
}
Object.keys(request.headers).forEach(function (header) {
xhr.setRequestHeader(request.name(header), request.get(header));
});
Expand Down
31 changes: 29 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ test('response body', function (t) {
})
})

t.test('should unzip contents', function (t) {
t.test('unzip contents', function (t) {
return popsicle({
url: REMOTE_URL + '/echo/zip',
body: fs.createReadStream(filename)
Expand All @@ -572,7 +572,7 @@ test('response body', function (t) {
})
})

t.test('should unzip with gzip encoding', function (t) {
t.test('unzip with gzip encoding', function (t) {
return popsicle({
url: REMOTE_URL + '/echo/zip',
body: fs.createReadStream(filename),
Expand All @@ -585,6 +585,33 @@ test('response body', function (t) {
t.equal(res.body, filecontents)
})
})
} else {
t.test('browser response type', function (t) {
return popsicle({
url: REMOTE_URL + '/text',
options: {
responseType: 'arraybuffer'
}
})
.then(function (res) {
t.ok(res.body instanceof ArrayBuffer)
})
})

t.test('throw on unsupported response type', function (t) {
t.plan(2)

return popsicle({
url: REMOTE_URL + '/text',
options: {
responseType: 'foobar'
}
})
.catch(function (err) {
t.equal(err.message, 'Unsupported response type: foobar')
t.equal(err.type, 'ERESPONSETYPE')
})
})
}
})

Expand Down

0 comments on commit 75ab1bd

Please sign in to comment.