diff --git a/http.js b/http.js index 0dbcde2..70aedab 100644 --- a/http.js +++ b/http.js @@ -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; }; @@ -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 diff --git a/spec/http/normalize-request-spec.js b/spec/http/normalize-request-spec.js new file mode 100644 index 0000000..97de985 --- /dev/null +++ b/spec/http/normalize-request-spec.js @@ -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" } + }); + }); + +}); \ No newline at end of file