Skip to content

Commit

Permalink
added glow.net.put & glow.net.del to do PUT & DELETE requests with a …
Browse files Browse the repository at this point in the history
…couple of tests
  • Loading branch information
gillesruppert committed Feb 11, 2010
1 parent 6b35e3d commit 83fafc0
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
72 changes: 72 additions & 0 deletions src/net/net.js
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/glow/dom/dom.js
Expand Up @@ -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("[");
Expand Down Expand Up @@ -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.");
});

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
49 changes: 48 additions & 1 deletion test/glow/net/net.js
Expand Up @@ -425,4 +425,51 @@ t.test("glow.net.loadScript aborting", function() {
t.ok(onAbortCalled, "onAbort called");
t.start();
}, 3000);
})
})

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) == '<?' ) {
t.start();
t.skip("This test requires a web server running PHP5");
return;
}
t.ok(true, "correct callback used");
t.equals( response.text(), "PUT: putData", "Using put method" );
t.start();
},
onError: function() {
t.ok(false, "correct callback used");
t.start();
}
}
);
});

t.test("glow.net.del aync", function() {
t.expect(2);
t.stop();
var request = glow.net.del("testdata/xhr/delete.php",
{
onLoad: function(response) {
if ( response.text().slice(0, 2) == '<?' ) {
t.start();
t.skip("This test requires a web server running PHP5");
return;
}
t.ok(true, "correct callback used");
t.equals( response.text(), "DELETE request", "Using delete method" );
t.start();
},
onError: function() {
t.ok(false, "correct callback used");
t.start();
}
}
);
});
10 changes: 10 additions & 0 deletions test/testdata/xhr/delete.php
@@ -0,0 +1,10 @@
<?php

if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
echo 'DELETE request';
}
else {
echo 'NOT a delete request';
}


14 changes: 14 additions & 0 deletions test/testdata/xhr/put.php
@@ -0,0 +1,14 @@
<?php

if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
// since PHP doesn't have a $_PUT global variable,
// we need to get the body this way
parse_str(file_get_contents('php://input'), $put_vars);
// from a purist point of view, PUT doesn't send a response
// apart from the headers, but this is useful for testing
// to see whether the body arrived properly
echo 'PUT: ' . $put_vars['some'];
}
else {
echo 'not a put request';
}

0 comments on commit 83fafc0

Please sign in to comment.