Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving request normalization #87

Merged
merged 1 commit into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions http.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,16 @@ exports.normalizeRequest = function (request) {
}
if (request.url) {
var url = URL.parse(request.url);
request.host = url.hostname;
request.port = url.port;
request.ssl = url.protocol === "https:";
request.method = request.method || "GET";
request.host = url.hostname;
request.port = url.port || (request.ssl ? "443" : "80");
request.path = (url.pathname || "") + (url.search || "");
request.headers = request.headers || {};
request.headers.host = url.hostname; // FIXME name consistency
}
request.method = request.method || "GET";
request.port = request.port || "80";
request.path = request.path || "/";
request.headers = request.headers || {};
request.headers.host = request.headers.host || request.host;
return request;
};

Expand Down Expand Up @@ -276,21 +278,9 @@ exports.request = function (request) {
request = exports.normalizeRequest(request);

var deferred = Q.defer();
var ssl = request.ssl;
var http = ssl ? HTTPS : HTTP;

var headers = request.headers || {};

headers.host = headers.host || request.host;
var http = request.ssl ? HTTPS : HTTP;

var _request = http.request({
"host": request.host,
"port": request.port || (ssl ? 443 : 80),
"path": request.path || "/",
"method": request.method || "GET",
"headers": headers,
"agent": request.agent
}, function (_response) {
var _request = http.request(request, function (_response) {
deferred.resolve(exports.ClientResponse(_response, request.charset));
_response.on("error", function (error) {
// TODO find a better way to channel
Expand Down
190 changes: 190 additions & 0 deletions spec/http/normalize-request-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
var normalizeRequest = require("../../http").normalizeRequest;

describe("request normalization", function () {

it("must parse string HTTP URL into components", function () {
var request = normalizeRequest("http://google.com");
expect(request).toEqual({
url: "http://google.com",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/",
headers: { host: "google.com" }
});
});

it("must parse string HTTP URL with path into components with path specified", function () {
var request = normalizeRequest("http://google.com/search?q=q-io");
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must parse string HTTP URL with port into components with port specified", function () {
var request = normalizeRequest("http://google.com:8080/search?q=q-io");
expect(request).toEqual({
url: "http://google.com:8080/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "8080",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must parse string HTTPS URL into components", function () {
var request = normalizeRequest("https://google.com");
expect(request).toEqual({
url: "https://google.com",
method: "GET",
ssl: true,
host: "google.com",
port: "443",
path: "/",
headers: { host: "google.com" }
});
});

it("must parse string HTTPS URL with port into components with port overriden", function () {
var request = normalizeRequest("https://google.com:8080");
expect(request).toEqual({
url: "https://google.com:8080",
method: "GET",
ssl: true,
host: "google.com",
port: "8080",
path: "/",
headers: { host: "google.com" }
});
});

it("must parse string HTTP URL of request object into components", function () {
var request = normalizeRequest({ url: "http://google.com/search?q=q-io" });
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must preserve request method of request object with string HTTP URL", function () {
var request = normalizeRequest({
method: "POST",
url: "http://google.com/search?q=q-io"
});
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "POST",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must preserve host header of request object with string HTTP URL", function () {
var request = normalizeRequest({
url: "http://google.com/search?q=q-io",
headers: { host: "yahoo.com" }
});
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "yahoo.com" }
});
});

it("must ignore host of request object with string HTTP URL", function () {
var request = normalizeRequest({
url: "http://google.com/search?q=q-io",
host: "yahoo.com"
});
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must ignore port number of request object with string HTTP URL", function () {
var request = normalizeRequest({
url: "http://google.com/search?q=q-io",
port: "8080"
});
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must ignore path string of request object with string HTTP URL", function () {
var request = normalizeRequest({
url: "http://google.com/search?q=q-io",
path: "/"
});
expect(request).toEqual({
url: "http://google.com/search?q=q-io",
method: "GET",
ssl: false,
host: "google.com",
port: "80",
path: "/search?q=q-io",
headers: { host: "google.com" }
});
});

it("must fill all missing fields of request object", function () {
var request = normalizeRequest({
host: "google.com"
});
expect(request).toEqual({
method: "GET",
host: "google.com",
port: "80",
path: "/",
headers: { host: "google.com" }
});
});

it("must preserve host header of request object", function () {
var request = normalizeRequest({
host: "google.com",
headers: { host: "yahoo.com" }
});
expect(request).toEqual({
method: "GET",
host: "google.com",
port: "80",
path: "/",
headers: { host: "yahoo.com" }
});
});

});