Add helper functions for common cases.

This commit is contained in:
Nick Martin
2012-04-19 14:32:59 -07:00
parent 47b9ae1f98
commit a6da0da862
2 changed files with 35 additions and 25 deletions

View File

@@ -35,4 +35,19 @@ Meteor.http = Meteor.http || {};
return url;
};
})();
Meteor.http.get = function (/* varargs */) {
return Meteor.http.call.apply(this, ["GET"].concat(_.toArray(arguments)));
};
Meteor.http.post = function (/* varargs */) {
return Meteor.http.call.apply(this, ["POST"].concat(_.toArray(arguments)));
};
Meteor.http.put = function (/* varargs */) {
return Meteor.http.call.apply(this, ["PUT"].concat(_.toArray(arguments)));
};
Meteor.http.del = function (/* varargs */) {
return Meteor.http.call.apply(this, ["DELETE"].concat(_.toArray(arguments)));
};
})();

View File

@@ -166,34 +166,29 @@ testAsyncMulti("httpcall - methods", [
function(test, expect) {
// non-get methods
var test_method = function(meth, should_throw) {
var maybe_expect = (should_throw ? _.identity : expect);
var func = function() {
Meteor.http.call(
meth, url_prefix()+"/foo",
maybe_expect(function(error, result) {
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
test.equal(data.url, "/foo");
// IE <= 8 turns seems to turn POSTs with no body into
// GETs, inexplicably.
if (Meteor.is_client && $.browser.msie && $.browser.version <= 8
&& meth === "POST")
meth = "GET";
test.equal(data.method, meth);
}));
};
if (should_throw)
test.throws(func);
else
func();
var test_method = function(meth, func_name) {
func_name = func_name || meth.toLowerCase();
Meteor.http[func_name](
url_prefix()+"/foo",
expect(function(error, result) {
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
test.equal(data.url, "/foo");
// IE <= 8 turns seems to turn POSTs with no body into
// GETs, inexplicably.
if (Meteor.is_client && $.browser.msie && $.browser.version <= 8
&& meth === "POST")
meth = "GET";
test.equal(data.method, meth);
}));
};
test_method("GET");
test_method("POST");
test_method("PUT");
test_method("DELETE");
test_method("DELETE", 'del');
},
function(test, expect) {