Skip to content

Commit

Permalink
net: adding glow.net.send for using other HTTP verbs (thanks to Max N…
Browse files Browse the repository at this point in the history
…oe max.noe@bbc.co.uk)
  • Loading branch information
jakearchibald committed Feb 16, 2010
1 parent 543ddde commit d5e70a0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/net/net.js
Expand Up @@ -263,6 +263,47 @@
return makeXhrRequest('POST', url, o);
};

/**
@name glow.net.send
@function
@description Makes a custom HTTP request to a given url
Not all HTTP verbs work cross-browser. Use {@link glow.net.get get},
{@link glow.net.post post}, {@link glow.net.put put} or
{@link glow.net.del del} for 'safe' methods.
@param {String} method
The HTTP method to use for the request. Methods are case sensitive
in some browsers.
@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 post, either as a JSON-style object or a urlEncoded string
@param {Object} opts
Same options as {@link glow.net.get}
@returns {glow.net.Request|glow.net.Response}
A response object for non-defered sync requests, otherwise a
request object is returned
@example
glow.net.send('HEAD', 'myFile.html', null, {
onLoad: function(response) {
// ...
}
});
*/
r.send = function(method, url, data, o) {
// Ensure that an empty body does not cause a 411 error.
data = data || '';

o = populateOptions(o);
o.data = data;

return makeXhrRequest(method, url, o);
};

/**
@name glow.net.put
@function
Expand Down Expand Up @@ -840,3 +881,4 @@
}
});


21 changes: 21 additions & 0 deletions test/glow/net/net.js
Expand Up @@ -473,3 +473,24 @@ t.test("glow.net.del aync", function() {
}
);
});

t.test("glow.net.send", function() {
t.expect(2);
t.stop();
var request = glow.net.send('OPTIONS', "testdata/xhr/verb.php", 'hello=world', {
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(), "OPTIONS: world", "Using put method" );
t.start();
},
onError: function() {
t.ok(false, "correct callback used");
t.start();
}
});
});
4 changes: 4 additions & 0 deletions test/testdata/xhr/verb.php
@@ -0,0 +1,4 @@
<?php

parse_str(file_get_contents('php://input'), $inputVars);
echo $_SERVER['REQUEST_METHOD'] . ': ' . $inputVars['hello'];

0 comments on commit d5e70a0

Please sign in to comment.