Question

I'm using jsdom with node.js and I'm trying to get it to provide me with some indication that an http error has occurred. I've set up a test server that simply returns an http 500 header for all requests, but when I attempt to load it with jsdom, jsdom doesn't throw any error and doesn't seem to provide me with any information that would identify that an http 500 error was returned. What's the best way to detect an http 500 error?

Was it helpful?

Solution

I looks like jsdom doesn't check status codes. 500 status codes are not considered errors by Node's HTTP module, so they're considered a correct response by jsdom.

I suggest you test the status code yourself before using jsdom and pass jsdom the body of your response, ie:

var jsdom = require('jsdom').jsdom;
var request = require('request');  //jsdom already uses request*

request("http://www.google.com/", function (err, response, body) {
  if (!err && response.statusCode == 200) {
    jsdom.env(body, function (errors, window) {
      // your code
    });
  }
});

[*] Request module: https://github.com/mikeal/request

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top