Permalink
Browse files

Merge pull request #1 from jonasfj/master

Added .get, .del, .put... helpers
  • Loading branch information...
2 parents 594d2c4 + 8bd3bd0 commit bdd9a558601d8fd439f4d2ce6a6631368c3eccec @lightsofapollo committed Mar 7, 2014
Showing 348 changed files with 155 additions and 45,216 deletions.
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -1,29 +1,93 @@
/**
-Promise wrapper for superagent
-*/
+ * Promise wrapper for superagent
+ */
-var superagent = require('superagent');
-var Request = superagent.Request;
-var Promise = require('promise');
+var superagent = require('superagent');
+var Promise = require('promise');
+/**
+ * Request object similar to superagent.Request, but with end() returning
+ * a promise.
+ */
function PromiseRequest() {
- Request.apply(this, arguments);
+ superagent.Request.apply(this, arguments);
}
-PromiseRequest.prototype = Object.create(Request.prototype);
+// Inherit form superagent.Request
+PromiseRequest.prototype = Object.create(superagent.Request.prototype);
+/** Send request and get a promise that `end` was emitted */
PromiseRequest.prototype.end = function() {
- var _super = Request.prototype.end;
+ var _super = superagent.Request.prototype.end;
var context = this;
return new Promise(function(accept, reject) {
_super.call(context, function(err, value) {
- if (err) return reject(err);
+ if (err) {
+ return reject(err);
+ }
accept(value);
});
});
};
-module.exports = function(method, url) {
+/**
+ * Request builder with same interface as superagent.
+ * It is convenient to import this as `request` in place of superagent.
+ */
+var request = function(method, url) {
return new PromiseRequest(method, url);
};
+
+/** 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 a head request */
+request.head = function(url, data) {
+ var req = request('HEAD', url);
+ if (data) {
+ req.send(data);
+ }
+ return req;
+};
+
+/** 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);
+ if (data) {
+ req.send(data);
+ }
+ return req;
+};
+
+/** Helper for making a post request */
+request.post = function(url, data) {
+ var req = request('POST', url);
+ if (data) {
+ req.send(data);
+ }
+ return req;
+};
+
+/** Helper for making a put request */
+request.put = function(url, data) {
+ var req = request('PUT', url);
+ if (data) {
+ req.send(data);
+ }
+ return req;
+};
+
+// Export the request builder
+module.exports = request;
View
@@ -1,18 +1,37 @@
suite('superagent-promise', function() {
- var assert = require('assert');
- var agent = require('./');
- var http = require('http');
+ var assert = require('assert');
+ var request = require('./');
+ var http = require('http');
+ var debug = require('debug')('index_test');
// start the server
var server;
- var body = 'woot';
+ var successBody = 'woot';
+ var errorBody = 'Not Found';
setup(function(done) {
server = http.createServer(function(req, res) {
- res.writeHead(200, {
- 'Content-Length': body.length,
- 'Content-Type': 'text/plain'
- });
- res.end(body);
+ if (/success$/.test(req.url)) {
+ debug("Responding with 200");
+ res.writeHead(200, {
+ 'Content-Length': successBody.length,
+ 'Content-Type': 'text/plain'
+ });
+ res.end(successBody);
+ } else if(/NotFound$/.test(req.url)) {
+ debug("Responding with 404");
+ res.writeHead(404, {
+ 'Content-Length': errorBody.length,
+ 'Content-Type': 'text/plain'
+ });
+ res.end(errorBody);
+ } else if(/error$/.test(req.url)) {
+ debug("Responding with 200, but mismatching Content-Length");
+ res.writeHead(404, {
+ 'Content-Length': successBody.length - 2,
+ 'Content-Type': 'text/plain'
+ });
+ res.end(successBody);
+ }
});
server.listen(0, done);
@@ -25,11 +44,11 @@ suite('superagent-promise', function() {
test('issue request', function(done) {
var addr = server.address();
- var url = 'http://' + addr.address + ':' + addr.port;
+ var url = 'http://' + addr.address + ':' + addr.port + "/success";
- agent('GET', url).end().then(
+ request('GET', url).end().then(
function(res) {
- assert.equal(res.text, body);
+ assert.equal(res.text, successBody);
done();
},
@@ -38,4 +57,47 @@ suite('superagent-promise', function() {
}
);
});
+
+ test('issue request with .get', function(done) {
+ var addr = server.address();
+ var url = 'http://' + addr.address + ':' + addr.port + "/success";
+
+ request.get(url).end().then(
+ function(res) {
+ assert.equal(res.text, successBody);
+ done();
+ },
+
+ function(err) {
+ done(err);
+ }
+ );
+ });
+
+ test('issue 404 request', function(done) {
+ var addr = server.address();
+ var url = 'http://' + addr.address + ':' + addr.port + "/NotFound";
+
+ request('GET', url).end().then(function(res) {
+ assert.ok(!res.ok);
+ assert.equal(res.text, errorBody);
+ done();
+ }, function(err) {
+ console.log(err);
+ done(err);
+ });
+ });
+
+ test('test error', function(done) {
+ var addr = server.address();
+ var url = 'http://' + addr.address + ':' + addr.port + "/error";
+
+ request('GET', url).end().then(function(res) {
+ assert.ok(false);
+ done();
+ }, function(err) {
+ assert.ok(err);
+ done();
+ });
+ });
});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit bdd9a55

Please sign in to comment.