KyleAMathews / superagent-bluebird-promise

Add promise support to superagent using Bluebird
MIT License
182 stars 37 forks source link

Compatibility with superagent-mocker #53

Open zachlysobey opened 8 years ago

zachlysobey commented 8 years ago

I'm using superagent-mocker in my unit tests to mock out the super-agent responses, but it seems not to work for non-200 responses.

The following code ends up causing an error:

Uncaught TypeError: Cannot read property 'status' of null

if (typeof res !== "undefined" && res.status >= 400) {
  var msg = 'cannot ' + req.method + ' ' + req.url + ' (' + res.status + ')'

From: https://github.com/KyleAMathews/superagent-bluebird-promise/blob/master/index.js#L66-L67

This is happening when superagent-mocker calls that function with res passed in as null rather than undefined.

https://github.com/A/superagent-mocker/blob/master/index.js#L99-L100

I'm not entirely sure which of your libraries is responsible for this bug, but either case looks like a pretty easy fix. (I'll submit an issue to superagent-mocker as well)

If you want, I can submit a PR to change

if (typeof res !== "undefined" && res.status >= 400) {

to

if (res && res.status >= 400) {

Thanks for taking your time to contribute this very useful library to the OSS community!