Skip to content

Commit

Permalink
Support XMLHttpRequest withCredentials property
Browse files Browse the repository at this point in the history
There are flags on the XHR that are important to set correctly in certain
contexts. The 'withCredentials' property used by CORS is one example. As
the context is essential, there is no good default value, thus we need to
make it open to configuration. Rather than explicitly support each property,
the xhr client now supports `request.mixin` which will copy all its
properties directly on to the raw XHR object.

Issue: cujojs#40
  • Loading branch information
scothis committed Sep 17, 2013
1 parent e5a7318 commit 3b33bb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
client = response.raw = new XMLHttpRequest();
client.open(method, url, true);

if (request.mixin) {
Object.keys(request.mixin).forEach(function (prop) {
// make sure the property already exists as
// IE 6 will blow up if we add a new prop
if (request.mixin.hasOwnProperty(prop) && prop in client) {
client[prop] = request.mixin[prop];
}
});
}

headers = request.headers;
for (headerName in headers) {
/*jshint forin:false */
Expand Down
6 changes: 6 additions & 0 deletions docs/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ The default client for browsers. The XHR client utilizes the XMLHttpRequest obj
<td>window.XMLHttpRequest</td>
<td>The XMLHttpRequest instance to use</td>
</tr>
<tr>
<td>request.mixin</td>
<td>optional</td>
<td><em>none</em></td>
<td>Additional properties to mix into the XHR object</td>
</tr>
</table>

**Know limitations**
Expand Down
11 changes: 11 additions & 0 deletions test/client/xhr-test-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@
refute(request.canceled);
}).otherwise(fail);
},
'should mixin additional properties': {
requiresSupportFor: { timeout: XMLHttpRequest && 'timeout' in new XMLHttpRequest() },
'': function () {
var request = { path: '/', mixin: { timeout: 1000, foo: 'bar' } };
return client(request).then(function (response) {
var xhr = response.raw;
assert.equals(xhr.timeout, 1000);
refute.equals(xhr.foo, 'bar');
}).otherwise(fail);
}
},
'//should abort the request if canceled': function (done) {
// TODO find an endpoint that takes a bit to respond, cached files may return synchronously
// this test misbehavies in IE6, the response is recieved before the request can cancel
Expand Down

0 comments on commit 3b33bb9

Please sign in to comment.