From 83fafc01b68d78bfd308d4fb57e7ae39857264cc Mon Sep 17 00:00:00 2001 From: Gilles Ruppert Date: Thu, 11 Feb 2010 22:22:39 +0000 Subject: [PATCH] added glow.net.put & glow.net.del to do PUT & DELETE requests with a couple of tests --- src/net/net.js | 72 ++++++++++++++++++++++++++++++++++++ test/glow/dom/dom.js | 3 +- test/glow/net/net.js | 49 +++++++++++++++++++++++- test/testdata/xhr/delete.php | 10 +++++ test/testdata/xhr/put.php | 14 +++++++ 5 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 test/testdata/xhr/delete.php create mode 100644 test/testdata/xhr/put.php diff --git a/src/net/net.js b/src/net/net.js index c0ea27f..b8e71c7 100644 --- a/src/net/net.js +++ b/src/net/net.js @@ -265,6 +265,78 @@ return makeXhrRequest('POST', url, o); }; + /** + @name glow.net.put + @function + @description Makes an HTTP PUT request to a given url + + @param {String} url + Url to make the request to. This can be a relative path. You cannot make requests + for files on other domains, to do that you must put your data in a javascript + file and use {@link glow.net.loadScript} to fetch it. + @param {Object|String} data + Data to put, either as a JSON-style object or a urlEncoded string + @param {Object} opts + Same options as {@link glow.net.get} + + @returns {Number|glow.net.Response} + An integer identifying the async request, or the response object for sync requests + + @example + var postRef = glow.net.put("myFile.html", + {key:"value", otherkey:["value1", "value2"]}, + { + onLoad: function(response) { + alert("Got file:\n\n" + response.text()); + }, + onError: function(response) { + alert("Error getting file: " + response.statusText()); + } + } + ); + */ + r.put = function(url, data, o) { + o = populateOptions(o); + o.data = data; + if (!o.headers["Content-Type"]) { + o.headers["Content-Type"] = STR.POST_DEFAULT_CONTENT_TYPE; + } + return makeXhrRequest('PUT', url, o); + }; + + /** + @name glow.net.del + @function + @description Makes an HTTP DELETE request to a given url + we can't use glow.net.delete as it is a reserved keyword + + @param {String} url + Url to make the request to. This can be a relative path. You cannot make requests + for files on other domains, to do that you must put your data in a javascript + file and use {@link glow.net.loadScript} to fetch it. + @param {Object} opts + Same options as {@link glow.net.get} + + @returns {Number|glow.net.Response} + An integer identifying the async request, or the response object for sync requests + + @example + var postRef = glow.net.del("myFile.html", + { + onLoad: function(response) { + alert("Got file:\n\n" + response.text()); + }, + onError: function(response) { + alert("Error getting file: " + response.statusText()); + } + } + ); + */ + r.del = function(url, o) { + o = populateOptions(o); + return makeXhrRequest('DELETE', url, o); + }; + /** @name glow.net.loadScript @function diff --git a/test/glow/dom/dom.js b/test/glow/dom/dom.js index 48c2376..a1b76ff 100644 --- a/test/glow/dom/dom.js +++ b/test/glow/dom/dom.js @@ -10,6 +10,7 @@ t.test("Load DOM", function() { }); t.test("broken", function() { + console.log('testing whether it works'); t.expect(7); try { glow.dom.get("["); @@ -2561,4 +2562,4 @@ t.test("glow.dom.NodeList#data Destroy", function() { t.equals(testData.data("tea"), undefined, "After being destroyed the node's data is undefined."); }); -/*-----------------------------------------------------------------------------*/ \ No newline at end of file +/*-----------------------------------------------------------------------------*/ diff --git a/test/glow/net/net.js b/test/glow/net/net.js index 05cd498..043a21a 100644 --- a/test/glow/net/net.js +++ b/test/glow/net/net.js @@ -425,4 +425,51 @@ t.test("glow.net.loadScript aborting", function() { t.ok(onAbortCalled, "onAbort called"); t.start(); }, 3000); -}) \ No newline at end of file +}) + +t.test("glow.net.put aync json", function() { + t.expect(2); + t.stop(); + var request = glow.net.put("testdata/xhr/put.php", + {some:"putData", blah:["something", "somethingElse"]}, + { + onLoad: function(response) { + if ( response.text().slice(0, 2) == '