Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withCredentials option. Fix #29 #33

Merged
merged 2 commits into from
Jul 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})