From a6da0da86283d2e3323197c66b1dcbf76ef4d0e7 Mon Sep 17 00:00:00 2001 From: Nick Martin Date: Thu, 19 Apr 2012 14:32:59 -0700 Subject: [PATCH] Add helper functions for common cases. --- packages/http/httpcall_common.js | 17 ++++++++++++- packages/http/httpcall_tests.js | 43 ++++++++++++++------------------ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/packages/http/httpcall_common.js b/packages/http/httpcall_common.js index e6755e458a..61ee09239a 100644 --- a/packages/http/httpcall_common.js +++ b/packages/http/httpcall_common.js @@ -35,4 +35,19 @@ Meteor.http = Meteor.http || {}; return url; }; -})(); \ No newline at end of file + + 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))); + }; + + +})(); diff --git a/packages/http/httpcall_tests.js b/packages/http/httpcall_tests.js index c0aa891dc8..899e492fb2 100644 --- a/packages/http/httpcall_tests.js +++ b/packages/http/httpcall_tests.js @@ -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) {