Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
- switched to mocky.io as it now can return CORS headers
- added missing useXDR
- timeout test not cross-domain
- turned off 404 test for IE, as it can't handle HTTP errors cross-domain
- updated dev dependencies
  • Loading branch information
naugtur committed Jul 10, 2015
1 parent dfa298f commit adf63ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"parse-headers": "^2.0.0"
},
"devDependencies": {
"pre-commit": "0.0.9",
"run-browser": "^1.3.1",
"tap-spec": "^0.1.8",
"tape": "^2.12.2"
"pre-commit": "1.0.10",
"run-browser": "^2.0.2",
"tap-spec": "^4.0.2",
"tape": "^4.0.0"
},
"licenses": [
{
Expand Down
59 changes: 22 additions & 37 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,48 @@ test("constructs and calls callback without throwing", function (assert) {
})
})

test("can GET a url", function (assert) {
test("[func] Can GET a url (cross-domain)", function (assert) {
xhr({
headers: {
accept: "text/html"
},
uri: "http://reqr.es/api/stuff"
uri: "http://www.mocky.io/v2/55a02cb72651260b1a94f024",
useXDR: true
}, function (err, resp, body) {
assert.ifError(err, "no err")
assert.equal(resp.statusCode, 200)
assert.equal(typeof resp.rawRequest, "object")
assert.equal(resp.headers['content-type'].indexOf('application/json'), 0)
assert.notEqual(resp.body.length, 0)
assert.equal(resp.body,'{"a":1}')
assert.notEqual(body.length, 0)
assert.end()
})
})

test("Returns http error responses like npm's request", function (assert) {
xhr({
headers: {
accept: "text/html"
},
uri: "http://reqr.es/api/stuff/23"
}, function (err, resp, body) {
assert.ifError(err, "no err")
assert.equal(resp.statusCode, 404)
assert.equal(typeof resp.rawRequest, "object")
assert.end()
})
test("[func] Returns http error responses like npm's request (cross-domain)", function (assert) {
if (!window.XDomainRequest) {
xhr({
uri: "http://www.mocky.io/v2/55a02d63265126221a94f025",
useXDR: true
}, function (err, resp, body) {
assert.ifError(err, "no err")
assert.equal(resp.statusCode, 404)
assert.equal(typeof resp.rawRequest, "object")
assert.end()
})
} else {
assert.end();
}
})

test("Times out to an error ", function (assert) {
test("[func] Times out to an error ", function (assert) {
xhr({
headers: {
accept: "text/html"
},
timeout: 1000,
uri: "http://reqr.es/api/stuff?delay=10"
timeout: 1,
uri: "/should-take-a-bit-to-parse?" + (new Array(300)).join("cachebreaker=" + Math.random().toFixed(5) + "&")
}, function (err, resp, body) {
assert.ok(err instanceof Error, "should return error")
assert.equal(resp.statusCode, 0)
assert.end()
})
})


test("withCredentials option", function (assert) {
if (!window.XDomainRequest) {
var req = xhr({}, function () {})
Expand All @@ -69,11 +65,6 @@ test("withCredentials option", function (assert) {
req.withCredentials,
"withCredentials set to true"
)
} else {
assert.ok(
true,
"no point testing withCredentials in IE8/9"
)
}
assert.end()
})
Expand All @@ -84,15 +75,9 @@ test("withCredentials ignored when using synchronous requests", function (assert
withCredentials: true,
sync: true
}, function () {})
assert.ok(
!req.withCredentials,
assert.ok(!req.withCredentials,
"sync overrides withCredentials"
)
} else {
assert.ok(
true,
"no point testing withCredentials in IE8/9"
)
}
assert.end()
})
Expand Down

0 comments on commit adf63ad

Please sign in to comment.