<commit_before>var assert  = require('assert');
var Promise = require('es6-promise').Promise
var request = require('./')(require('superagent'), Promise);
var http    = require('http');
var debug   = require('debug')('test:index');

function respondWith(res, status, body) {
  res.writeHead(status, {
    'Content-Type': 'text/plain',
    'Content-Length': body.length
  });
  res.end(body);
}

describe('superagent-promise', function() {
  // start the server
  var server;
  var baseURL;
  var successBody = 'woot';
  var errorBody = 'Not Found';

  before(function(done) {
    server = http.createServer(function(req, res) {
      if (/success$/.test(req.url)) {
        debug('Responding with 200');
        respondWith(res, 200, successBody);
        return;

      } else if(/NotFound$/.test(req.url)) {
        debug('Responding with 404');
        respondWith(res, 404, errorBody);

      } else if(/error$/.test(req.url)) {
        debug('Responding with 200, but mismatching Content-Length');
        res.writeHead(200, {
          'Content-Length': successBody.length - 2,
          'Content-Type': 'text/plain'
        });
        res.end(successBody);
      } else if (/redirect/.test(req.url)) {
        debug('Responding with a 302 redirect');
        var url = baseURL + '/success';
        res.writeHead(303, {
          'Location': url
        });
        res.end();
       }
    });

    server.listen(0, function() {
      var addr = server.address();
      debug('server up at', addr);
      baseURL = 'http://' + addr.address + ':' + addr.port;
      done();
    });
  });

  after(function(done) {
    server.close(function() {
      debug('server down');
      done();
    });
    server = undefined;
  });

  describe('convenience methods', function() {
    [
      'head',
      'options',
      'get',
      'post',
      'put',
      'patch',
      'del'
    ].forEach(function(method) {
      describe('#'+method, function() {
        it('should have `then` and `end`', function() {
          assert(request[method]().then instanceof Function);
          assert(request[method]().end instanceof Function);
        });

        it('`end` should return a promise', function() {
          assert(request[method]().end() instanceof Promise);
        });

        it('`then` should return a promise', function() {
          assert(request[method]().then() instanceof Promise);
        });
      })
    });
  })

  describe('#end', function() {
    it('should succeed on 200', function(done) {
      var url = baseURL + '/success';

      request('GET', url).end().then(function(res) {
        assert.equal(res.text, successBody);
      }).then(done).catch(done);
    });

    it('should fail on 404', function(done) {
      var url = baseURL + '/NotFound';

      request('GET', url).end().then(undefined, function(err) {
        assert.equal(err.status, 404)
        assert.equal(err.response.text, errorBody);

      }).then(done).catch(done);
    });

    it('should fail if content length is mismatched', function(done) {
      var url = baseURL + '/error';

      request('GET', url).end().then(function(res) {
        done(new Error('Got response for mismatched Content-Length'))

      }, function(err) {
        assert.ok(err);
        done();
      });
    });

    it('should follow redirects', function() {
      var url = baseURL + '/redirect';

      return request('GET', url).end().then(function(res) {
        assert.equal(res.text, successBody);
      });
    });
  });

  describe('#then', function() {
    it('should succeed on 200', function(done) {
      var url = baseURL + '/success';

      request('GET', url).then(function(res) {
        assert.equal(res.text, successBody);
      }).then(done).catch(done);
    });

    it('issue 404 request', function(done) {
      var url = baseURL + '/NotFound';

      request('GET', url).then(undefined, function(err) {
        assert.equal(err.status, 404)
        assert.equal(err.response.text, errorBody);

      }).then(done).catch(done);
    });

    it('test error', function(done) {
      var url = baseURL + '/error';

      request('GET', url).then(function(res) {
        done(new Error('error should not should not succeed'));

      }, function(err) {
        assert.ok(err);
        done();
      });
    });

    it('issue request w. redirect', function() {
      var url = baseURL + '/redirect';

      return request('GET', url).then(function(res) {
        assert.equal(res.text, successBody);
      });
    });

  });
});
<commit_msg>Update tests to pass on iojs<commit_after>var assert  = require('assert');
var Promise = require('es6-promise').Promise
var request = require('./')(require('superagent'), Promise);
var http    = require('http');
var debug   = require('debug')('test:index');

function respondWith(res, status, body) {
  res.writeHead(status, {
    'Content-Type': 'text/plain',
    'Content-Length': body.length
  });
  res.end(body);
}

describe('superagent-promise', function() {
  // start the server
  var server;
  var baseURL;
  var successBody = 'woot';
  var errorBody = 'Not Found';

  before(function(done) {
    server = http.createServer(function(req, res) {
      if (/success$/.test(req.url)) {
        debug('Responding with 200');
        respondWith(res, 200, successBody);
        return;

      } else if(/NotFound$/.test(req.url)) {
        debug('Responding with 404');
        respondWith(res, 404, errorBody);

      } else if(/error$/.test(req.url)) {
        debug('Responding with 200, but mismatching Content-Length');
        res.writeHead(200, {
          'Content-Length': successBody.length - 2,
          'Content-Type': 'text/plain'
        });
        res.end(successBody);
      } else if (/redirect/.test(req.url)) {
        debug('Responding with a 302 redirect');
        var url = baseURL + '/success';
        res.writeHead(303, {
          'Location': url
        });
        res.end();
       }
    });

    server.listen(0, function() {
      var addr = server.address();
      debug('server up at', addr);
      baseURL = 'http://' + addr.address + ':' + addr.port;
      done();
    });
  });

  after(function(done) {
    server.close(function() {
      debug('server down');
      done();
    });
    server = undefined;
  });

  describe('convenience methods', function() {
    [
      'head',
      'options',
      'get',
      'post',
      'put',
      'patch',
      'del'
    ].forEach(function(method) {
      describe('#'+method, function() {
        it('should have `then` and `end`', function() {
          assert(request[method](baseURL).then instanceof Function);
          assert(request[method](baseURL).end instanceof Function);
        });

        it('`end` should return a promise', function() {
          assert(request[method](baseURL).end() instanceof Promise);
        });

        it('`then` should return a promise', function() {
          assert(request[method](baseURL).then() instanceof Promise);
        });
      })
    });
  })

  describe('#end', function() {
    it('should succeed on 200', function(done) {
      var url = baseURL + '/success';

      request('GET', url).end().then(function(res) {
        assert.equal(res.text, successBody);
      }).then(done).catch(done);
    });

    it('should fail on 404', function(done) {
      var url = baseURL + '/NotFound';

      request('GET', url).end().then(undefined, function(err) {
        assert.equal(err.status, 404)
        assert.equal(err.response.text, errorBody);

      }).then(done).catch(done);
    });

    it('should fail if content length is mismatched', function(done) {
      var url = baseURL + '/error';

      request('GET', url).end().then(function(res) {
        done(new Error('Got response for mismatched Content-Length'))

      }, function(err) {
        assert.ok(err);
        done();
      });
    });

    it('should follow redirects', function() {
      var url = baseURL + '/redirect';

      return request('GET', url).end().then(function(res) {
        assert.equal(res.text, successBody);
      });
    });
  });

  describe('#then', function() {
    it('should succeed on 200', function(done) {
      var url = baseURL + '/success';

      request('GET', url).then(function(res) {
        assert.equal(res.text, successBody);
      }).then(done).catch(done);
    });

    it('issue 404 request', function(done) {
      var url = baseURL + '/NotFound';

      request('GET', url).then(undefined, function(err) {
        assert.equal(err.status, 404)
        assert.equal(err.response.text, errorBody);

      }).then(done).catch(done);
    });

    it('test error', function(done) {
      var url = baseURL + '/error';

      request('GET', url).then(function(res) {
        done(new Error('error should not should not succeed'));

      }, function(err) {
        assert.ok(err);
        done();
      });
    });

    it('issue request w. redirect', function() {
      var url = baseURL + '/redirect';

      return request('GET', url).then(function(res) {
        assert.equal(res.text, successBody);
      });
    });

  });
});

{'fraction_non_alphanumeric': 0.17639969321792484, 'fraction_numerical': 0.009860852415908842, 'mean_word_length': 2.4733637747336377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2585814', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2674, 'n_words': 589}