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

Ianwremmel: fix frozen objects, fixes #149 #182

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ function _createXHR(options) {
method: method,
headers: {},
url: uri,
rawRequest: xhr
rawRequest: xhr,
request: request
}
if(xhr.getAllResponseHeaders){ //remember xhr can in fact be XDR for CORS in IE
response.headers = parseHeaders(xhr.getAllResponseHeaders())
Expand All @@ -141,11 +142,19 @@ function _createXHR(options) {

var key
var aborted
var uri = xhr.url = options.uri || options.url
var method = xhr.method = options.method || "GET"
var uri = options.uri || options.url

var method = options.method || "GET"
var body = options.body || options.data
var headers = xhr.headers = options.headers || {}
var headers = options.headers || {}
var sync = !!options.sync
var request = {
uri: uri,
url:uri,
headers:headers,
body:body,
method:method
}
var isJson = false
var timeoutTimer
var failureResponse = {
Expand All @@ -154,7 +163,8 @@ function _createXHR(options) {
statusCode: 0,
method: method,
url: uri,
rawRequest: xhr
rawRequest: xhr,
request: request
}

if ("json" in options && options.json !== false) {
Expand Down
23 changes: 13 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test("[func] Can GET a url (cross-domain)", function(assert) {
assert.ifError(err, "no err")
assert.equal(resp.statusCode, 200)
assert.equal(typeof resp.rawRequest, "object")
assert.equal(typeof resp.request, "object")
assert.notEqual(resp.body.length, 0)
assert.equal(resp.body, '{"a":1}')
assert.notEqual(body.length, 0)
Expand All @@ -34,6 +35,7 @@ test("[func] Returns http error responses like npm's request (cross-domain)", fu
assert.ifError(err, "no err")
assert.equal(resp.statusCode, 404)
assert.equal(typeof resp.rawRequest, "object")
assert.equal(typeof resp.request, "object")
assert.end()
})
} else {
Expand All @@ -48,6 +50,7 @@ test("[func] Request to domain with not allowed cross-domain", function(assert)
assert.ok(err instanceof Error, "should return error")
assert.equal(resp.statusCode, 0)
assert.equal(typeof resp.rawRequest, "object")
assert.equal(typeof resp.request, "object")
assert.end()
})
})
Expand Down Expand Up @@ -191,11 +194,11 @@ test("[func] xhr[method] get, put, post, patch", function(assert) {
test("xhr[method] get, put, post, patch with url shorthands", function(assert) {
var i = 0
forEach(methods, function(method) {
var req = xhr[method]("/some-test", function() {})
i++
assert.equal(req.method, method.toUpperCase())

if (i === methods.length) assert.end()
var req = xhr[method]("/some-404", function(err, resp) {
i++
assert.equal(resp.request.method, method.toUpperCase())
if (i === methods.length) assert.end()
})
})
})

Expand All @@ -206,7 +209,7 @@ test("[func] sends options.body as json body when options.json === true", functi
foo: "bar"
}
}, function(err, resp, body) {
assert.equal(resp.rawRequest.headers["Content-Type"], "application/json")
assert.equal(resp.request.headers["Content-Type"], "application/json")
assert.deepEqual(body, {
foo: "bar"
})
Expand All @@ -219,7 +222,7 @@ test("[func] doesn't freak out when json option is false", function(assert) {
json: false,
body: "{\"a\":1}"
}, function(err, resp, body) {
assert.notEqual(resp.rawRequest.headers["Content-Type"], "application/json")
assert.notEqual(resp.request.headers["Content-Type"], "application/json")
assert.equal(body, "{\"a\":1}")
assert.end()
})
Expand All @@ -231,7 +234,7 @@ test("[func] sends options.json as body when it's not a boolean", function(asser
foo: "bar"
}
}, function(err, resp, body) {
assert.equal(resp.rawRequest.headers["Content-Type"], "application/json")
assert.equal(resp.request.headers["Content-Type"], "application/json")
assert.deepEqual(body, {
foo: "bar"
})
Expand All @@ -248,7 +251,7 @@ test("xhr[method] get, put, post, patch with url shorthands and options", functi
}
}, function(err, resp, body) {
i++
assert.equal(resp.rawRequest.headers.foo, 'bar')
assert.equal(resp.request.headers.foo, 'bar')
assert.equal(resp.method, method.toUpperCase())

if (i === methods.length) assert.end()
Expand Down Expand Up @@ -307,7 +310,7 @@ test("url signature with object", function(assert) {
}
}, function(err, resp, body) {
assert.equal(resp.url, '/some-test')
assert.equal(resp.rawRequest.headers.foo, 'bar')
assert.equal(resp.request.headers.foo, 'bar')
assert.end()
})
})
Expand Down