Skip to content

Commit

Permalink
Merge pull request #33 from gsf/withCredentials-option
Browse files Browse the repository at this point in the history
Add withCredentials option. Fix #29
  • Loading branch information
Raynos committed Jul 1, 2014
2 parents 280e3f8 + b2a49ce commit 58e4e30
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type XhrOptions = String | {
timeout: Number?,
headers: Object?,
body: String?,
json: Object?
json: Object?,
withCredentials: Boolean?
}
xhr := (XhrOptions, Callback<Response>) => Request
```
Expand Down Expand Up @@ -100,6 +101,13 @@ A valid JSON serializable value to be send to the server. If this
Additionally the response body is parsed as JSON
### `options.withCredentials`
Specify whether user credentials are to be included in a cross-origin
request. Sets [`xhr.withCredentials`][10]. Defaults to true
when `options.cors` is true.
## MIT Licenced
[1]: http://xhr.spec.whatwg.org/#the-send()-method
Expand All @@ -111,3 +119,4 @@ Additionally the response body is parsed as JSON
[7]: http://xhr.spec.whatwg.org/#the-responsetext-attribute
[8]: http://xhr.spec.whatwg.org/#the-responsexml-attribute
[9]: http://xhr.spec.whatwg.org/#the-setrequestheader()-method
[10]: http://xhr.spec.whatwg.org/#the-withcredentials-attribute
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ function createXHR(options, callback) {
// hate IE
xhr.ontimeout = noop
xhr.open(method, uri, !sync)
if (options.cors) {

if (options.cors && options.withCredentials !== false) {
xhr.withCredentials = true
}

// Cannot set timeout with sync request
if (!sync) {
xhr.timeout = "timeout" in options ? options.timeout : 5000
Expand Down
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ test("can GET current page", function(assert) {
})
})

test("withCredentials option", function(assert) {
var req = xhr({}, function () {})
assert.ok(
!req.withCredentials,
"withCredentials not true when nothing set in options"
)
req = xhr({
cors: true
}, function () {})
assert.ok(
req.withCredentials,
"withCredentials set to true when cors is true in options"
)
req = xhr({
cors: true,
withCredentials: false
}, function () {})
assert.ok(
!req.withCredentials,
"withCredentials set to false when set to false in options"
)
assert.end()
})

0 comments on commit 58e4e30

Please sign in to comment.