Skip to content

Commit

Permalink
Merge branch 'http2' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
n1mmy committed Apr 19, 2012
2 parents 47b9ae1 + 3afa5fd commit 88923f2
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 70 deletions.
23 changes: 14 additions & 9 deletions docs/client/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1401,20 +1401,20 @@ <h2 id="meteor_http"><span>Meteor.http</span></h2>
<dl class="objdesc">

<dt><span class="name">statusCode</span>
<span class="type">Number or null</span></dt>
<span class="type">Number</span></dt>
<dd>Numeric HTTP result status code, or null on error.</dd>

<dt><span class="name">headers()</span>
<span class="type">Object</span></dt>
<dd>Return a dictionary of HTTP headers from the response.</dd>

<dt><span class="name">content()</span>
<dt><span class="name">content</span>
<span class="type">String</span></dt>
<dd>Return the body of the HTTP response as a string.</dd>
<dd>The body of the HTTP response as a string.</dd>

<dt><span class="name">data</span>
<span class="type">Object or null</span></dt>
<dd>If the response headers indicate JSON content, this contains the body of the document parsed as a JSON object.</dd>

<dt><span class="name">data()</span>
<dt><span class="name">headers</span>
<span class="type">Object</span></dt>
<dd>Return the body of the document parsed as a JSON object.</dd>
<dd>A dictionary of HTTP headers from the response.</dd>

<dt><span class="name">error</span>
<span class="type">Error</span></dt>
Expand Down Expand Up @@ -1446,6 +1446,11 @@ <h2 id="meteor_http"><span>Meteor.http</span></h2>


{{/better_markdown}}


{{> api_box httphelpers}}


</template>


Expand Down
7 changes: 7 additions & 0 deletions docs/client/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,4 +791,11 @@ Template.api.httpcall = {
]
};

Template.api.httphelpers = {
id: "meteor_http_helpers",
name: "Meteor.http.[get, post, put, del] (...)",
locus: "Anywhere",
descr: ["Convience shortcuts for `Meteor.http.call` with `method` argument specified."]
};


6 changes: 5 additions & 1 deletion docs/client/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ var toc = [
],

"Meteor.http", [
"Meteor.http.call"
"Meteor.http.call",
{name: "Meteor.http.get", id: "meteor_http_helpers"},
{name: "Meteor.http.post", id: "meteor_http_helpers"},
{name: "Meteor.http.put", id: "meteor_http_helpers"},
{name: "Meteor.http.del", id: "meteor_http_helpers"}
]
],

Expand Down
36 changes: 20 additions & 16 deletions packages/http/httpcall_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,28 @@ Meteor.http = Meteor.http || {};
// no HTTP response
callback(new Error("network"));
} else {

var response = {};
response.statusCode = xhr.status;
response.content = function() {
return xhr.responseText;
};
response.data = function() {
return JSON.parse(response.content());
};
response.headers = function () {
var header_str = xhr.getAllResponseHeaders();
var headers_raw = header_str.split(/\r?\n/);
var headers = {};
_.each(headers_raw, function (h) {
var m = /^(.*?):(?:\s+)(.*)$/.exec(h);
if (m && m.length === 3)
headers[m[1].toLowerCase()] = m[2];
});
return headers;
response.content = xhr.responseText;

response.headers = {};
var header_str = xhr.getAllResponseHeaders();
var headers_raw = header_str.split(/\r?\n/);
_.each(headers_raw, function (h) {
var m = /^(.*?):(?:\s+)(.*)$/.exec(h);
if (m && m.length === 3)
response.headers[m[1].toLowerCase()] = m[2];
});

// only parse data if correct content type.
if (_.include(['application/json', 'text/javascript'],
response.headers['content-type'])) {
try {
response.data = JSON.parse(response.content);
} catch (err) {
response.data = null;
}
};

var error = null;
Expand Down
17 changes: 16 additions & 1 deletion packages/http/httpcall_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
};


})();
19 changes: 11 additions & 8 deletions packages/http/httpcall_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,17 @@ Meteor.http = Meteor.http || {};

response = {};
response.statusCode = res.statusCode;
response.content = function() {
return body;
};
response.data = function() {
return JSON.parse(response.content());
};
response.headers = function () {
return res.headers;
response.content = body;
response.headers = res.headers;

// only parse data if correct content type.
if (_.include(['application/json', 'text/javascript'],
response.headers['content-type'])) {
try {
response.data = JSON.parse(response.content);
} catch (err) {
response.data = null;
}
};

if (res.statusCode >= 400)
Expand Down
65 changes: 30 additions & 35 deletions packages/http/httpcall_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ testAsyncMulti("httpcall - basic", [
test.equal(typeof result, "object");
test.equal(result.statusCode, 200);

var data = result.data();
var data = result.data;

// allow dropping of final ? (which mobile browsers seem to do)
var allowed = [expected_url];
Expand Down Expand Up @@ -105,7 +105,7 @@ testAsyncMulti("httpcall - failure", [
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.url, "/foo");
test.equal(data.method, "GET");

Expand All @@ -123,7 +123,7 @@ testAsyncMulti("httpcall - redirect", [

// should be redirected transparently to /foo
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.url, "/foo");
test.equal(data.method, "GET");
}));
Expand All @@ -142,7 +142,7 @@ testAsyncMulti("httpcall - redirect", [
if (followRedirects) {
// should be redirected transparently to /foo
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.url, "/foo");
test.equal(data.method, "GET");
} else {
Expand All @@ -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) {
Expand All @@ -205,7 +200,7 @@ testAsyncMulti("httpcall - methods", [
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.body, "Hello World!");
}));

Expand All @@ -216,7 +211,7 @@ testAsyncMulti("httpcall - methods", [
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.body, {greeting: "Hello World!"});
}));
}
Expand All @@ -239,7 +234,7 @@ testAsyncMulti("httpcall - http auth", [
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.url, "/login?"+password);
}));

Expand All @@ -264,7 +259,7 @@ testAsyncMulti("httpcall - headers", [
test.isTrue(result);

test.equal(result.statusCode, 200);
var data = result.data();
var data = result.data;
test.equal(data.url, "/foo-with-headers");
test.equal(data.method, "GET");
test.equal(data.headers['test-header'], "Value");
Expand All @@ -278,8 +273,8 @@ testAsyncMulti("httpcall - headers", [
test.isTrue(result);

test.equal(result.statusCode, 201);
test.equal(result.headers()['a-silly-header'], "Tis a");
test.equal(result.headers()['another-silly-header'], "Silly place.");
test.equal(result.headers['a-silly-header'], "Tis a");
test.equal(result.headers['another-silly-header'], "Silly place.");
}));
}
]);
Expand All @@ -304,7 +299,7 @@ testAsyncMulti("httpcall - params", [
test.isTrue(result);
test.equal(result.statusCode, 200);
if (method !== "HEAD") {
var data = result.data();
var data = result.data;
test.equal(data.method, method);
test.equal(data.url, expect_url);
test.equal(data.body, expect_body);
Expand Down
1 change: 1 addition & 0 deletions packages/http/test_responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var respond = function(req, res) {
response_string = JSON.stringify(response_data);

res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(response_string);
});

Expand Down

0 comments on commit 88923f2

Please sign in to comment.