superagent-promise
Advanced tools
Comparing version 1.0.0 to 1.0.3
@@ -1,27 +0,34 @@ | ||
suite('superagent-promise', function() { | ||
var assert = require('assert'); | ||
var request = require('./')(require('superagent'), require('es6-promise').Promise); | ||
var http = require('http'); | ||
var debug = require('debug')('index_test'); | ||
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'; | ||
setup(function(done) { | ||
var connections = {}; | ||
before(function(done) { | ||
server = http.createServer(function(req, res) { | ||
if (/success$/.test(req.url)) { | ||
debug('Responding with 200'); | ||
res.writeHead(200, { | ||
'Content-Length': successBody.length, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(successBody); | ||
respondWith(res, 200, successBody); | ||
return; | ||
} else if(/NotFound$/.test(req.url)) { | ||
debug('Responding with 404'); | ||
res.writeHead(404, { | ||
'Content-Length': errorBody.length, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(errorBody); | ||
respondWith(res, 404, errorBody); | ||
} else if(/error$/.test(req.url)) { | ||
@@ -34,7 +41,27 @@ debug('Responding with 200, but mismatching Content-Length'); | ||
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(); | ||
} | ||
}); | ||
var i = 0; | ||
// for some reason the checks on convenience methods open connections | ||
// that don't get closed so we have to do some clean up | ||
server.on('connection', function(conn) { | ||
conn.__connCount = i; | ||
connections[i++] = conn; | ||
conn.on('close', function() { | ||
delete connections[conn.__connCount]; | ||
}) | ||
}); | ||
server.listen(0, function() { | ||
debug('listen'); | ||
var addr = server.address(); | ||
debug('server up at', addr); | ||
baseURL = 'http://' + addr.address + ':' + addr.port; | ||
done(); | ||
@@ -44,5 +71,8 @@ }); | ||
teardown(function(done) { | ||
after(function(done) { | ||
for (var conn in connections) { | ||
connections[conn].destroy(); | ||
} | ||
server.close(function() { | ||
debug('teardown'); | ||
debug('server down'); | ||
done(); | ||
@@ -53,17 +83,37 @@ }); | ||
suite('#end', function() { | ||
test('issue request', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/success'; | ||
describe('convenience methods', function() { | ||
[ | ||
'head', | ||
'options', | ||
'get', | ||
'post', | ||
'put', | ||
'patch', | ||
'del' | ||
].forEach(function(method) { | ||
describe('#'+method, function() { | ||
it('should have `then` and `end`', function() { | ||
var promiseRequest = request[method](baseURL); | ||
assert(promiseRequest.then instanceof Function); | ||
assert(promiseRequest.end instanceof Function); | ||
}); | ||
request('GET', url).end().then(function(res) { | ||
assert.equal(res.text, successBody); | ||
}).then(done).catch(done); | ||
it('`end` should return a promise', function() { | ||
var p = request[method](baseURL).end(); | ||
assert(p instanceof Promise); | ||
}); | ||
it('`then` should return a promise', function() { | ||
var p = request[method](baseURL).then(function() { }); | ||
assert(p instanceof Promise); | ||
}); | ||
}) | ||
}); | ||
}) | ||
test('issue request with .get', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/success'; | ||
describe('#end', function() { | ||
it('should succeed on 200', function(done) { | ||
var url = baseURL + '/success'; | ||
request.get(url).end().then(function(res) { | ||
request('GET', url).end().then(function(res) { | ||
assert.equal(res.text, successBody); | ||
@@ -73,5 +123,4 @@ }).then(done).catch(done); | ||
test('issue 404 request', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/NotFound'; | ||
it('should fail on 404', function(done) { | ||
var url = baseURL + '/NotFound'; | ||
@@ -85,30 +134,28 @@ request('GET', url).end().then(undefined, function(err) { | ||
test('test error', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/error'; | ||
it('should fail if content length is mismatched', function(done) { | ||
var url = baseURL + '/error'; | ||
request('GET', url).end().then(function(res) { | ||
done(new Error('error should not should not succeed')); | ||
done(new Error('Got response for mismatched Content-Length')) | ||
}, function(err) { | ||
assert.ok(err); | ||
}).then(done).catch(done); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
suite('#then', function() { | ||
test('issue request', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/success'; | ||
it('should follow redirects', function() { | ||
var url = baseURL + '/redirect'; | ||
request('GET', url).then(function(res) { | ||
return request('GET', url).end().then(function(res) { | ||
assert.equal(res.text, successBody); | ||
}).then(done).catch(done); | ||
}); | ||
}); | ||
}); | ||
test('issue request with .get', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/success'; | ||
describe('#then', function() { | ||
it('should succeed on 200', function(done) { | ||
var url = baseURL + '/success'; | ||
request.get(url).then(function(res) { | ||
request('GET', url).then(function(res) { | ||
assert.equal(res.text, successBody); | ||
@@ -118,5 +165,4 @@ }).then(done).catch(done); | ||
test('issue 404 request', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/NotFound'; | ||
it('issue 404 request', function(done) { | ||
var url = baseURL + '/NotFound'; | ||
@@ -130,5 +176,4 @@ request('GET', url).then(undefined, function(err) { | ||
test('test error', function(done) { | ||
var addr = server.address(); | ||
var url = 'http://' + addr.address + ':' + addr.port + '/error'; | ||
it('test error', function(done) { | ||
var url = baseURL + '/error'; | ||
@@ -140,5 +185,15 @@ request('GET', url).then(function(res) { | ||
assert.ok(err); | ||
}).then(done).catch(done); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
it('issue request w. redirect', function() { | ||
var url = baseURL + '/redirect'; | ||
return request('GET', url).then(function(res) { | ||
assert.equal(res.text, successBody); | ||
}); | ||
}); | ||
}); | ||
}); |
49
index.js
@@ -23,5 +23,5 @@ /** | ||
return new Promise(function(accept, reject) { | ||
_end.call(self, function(err, value) { | ||
_end.call(self, function(err, response) { | ||
if (cb) { | ||
cb(err, value); | ||
cb(err, response); | ||
} | ||
@@ -32,3 +32,3 @@ | ||
} else { | ||
accept(value); | ||
accept(response); | ||
} | ||
@@ -45,7 +45,7 @@ }); | ||
return new Promise(function(accept, reject) { | ||
_end.call(self, function(err, value) { | ||
_end.call(self, function(err, response) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
accept(value); | ||
accept(response); | ||
} | ||
@@ -64,10 +64,6 @@ }); | ||
/** Helper for making a get request */ | ||
request.get = function(url, data) { | ||
var req = request('GET', url); | ||
if (data) { | ||
req.query(data); | ||
} | ||
return req; | ||
}; | ||
/** Helper for making an options request */ | ||
request.options = function(url) { | ||
return request('OPTIONS', url); | ||
} | ||
@@ -83,12 +79,7 @@ /** Helper for making a head request */ | ||
/** Helper for making a delete request */ | ||
request.del = function(url) { | ||
return request('DELETE', url); | ||
}; | ||
/** Helper for making a patch request */ | ||
request.patch = function(url, data) { | ||
var req = request('PATCH', url); | ||
/** Helper for making a get request */ | ||
request.get = function(url, data) { | ||
var req = request('GET', url); | ||
if (data) { | ||
req.send(data); | ||
req.query(data); | ||
} | ||
@@ -116,2 +107,16 @@ return req; | ||
/** Helper for making a patch request */ | ||
request.patch = function(url, data) { | ||
var req = request('PATCH', url); | ||
if (data) { | ||
req.send(data); | ||
} | ||
return req; | ||
}; | ||
/** Helper for making a delete request */ | ||
request.del = function(url) { | ||
return request('DELETE', url); | ||
}; | ||
// Export the request builder | ||
@@ -118,0 +123,0 @@ return request; |
{ | ||
"name": "superagent-promise", | ||
"version": "1.0.0", | ||
"version": "1.0.3", | ||
"description": "superagent promise wrapper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://img.shields.io/travis/lightsofapollo/superagent-promise/master.svg)](https://travis-ci.org/lightsofapollo/superagent-promise) | ||
superagent-promise | ||
@@ -10,3 +12,4 @@ ================== | ||
```js | ||
var agent = require('superagent-promise')(require('superagent'), this.Promise || require('promise')); | ||
var Promise = this.Promise || require('promise'); | ||
var agent = require('superagent-promise')(require('superagent'), Promise); | ||
@@ -27,3 +30,3 @@ // method, url form with `end` | ||
// helper functions: get, head, patch, post, put, del | ||
// helper functions: options, head, get, post, put, patch, del | ||
agent.put('http://myxfoo', 'data') | ||
@@ -35,3 +38,3 @@ .end() | ||
// helper functions: get, head, patch, post, put, del | ||
// helper functions: options, head, get, post, put, patch, del | ||
agent.put('http://myxfoo', 'data'). | ||
@@ -38,0 +41,0 @@ .then(function(res) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13844
16
316
44
1