From 99be892bf7c35ba6477088357d0a5e32079883a9 Mon Sep 17 00:00:00 2001 From: Tommy Messbauer Date: Tue, 8 Oct 2013 16:41:52 -0500 Subject: [PATCH 1/2] full support for node url object --- .gitignore | 1 + index.js | 2 +- lib/request.js | 15 ++++++++++----- package.json | 6 +++++- tests/request_url.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 tests/request_url.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js index 8dc17f6..18153d1 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var Request = require('./lib/request'); http.request = function (params, cb) { if (!params) params = {}; - if (!params.host) params.host = window.location.host.split(':')[0]; + if (!params.host) params.host = window.location.host; if (!params.port) params.port = window.location.port; if (!params.scheme) params.scheme = window.location.protocol.split(':')[0]; diff --git a/lib/request.js b/lib/request.js index 5266ae0..80e3ad7 100644 --- a/lib/request.js +++ b/lib/request.js @@ -8,16 +8,21 @@ var Request = module.exports = function (xhr, params) { var self = this; self.writable = true; self.xhr = xhr; - self.body = concatStream() + self.body = concatStream(); + + if (!params.host && params.hostname) { + params.host = params.hostname + (params.port ? ':' + params.port : ''); + } - var uri = params.host - + (params.port ? ':' + params.port : '') + self.uri = + (params.scheme || 'http') + '://' + + params.host + (params.path || '/') ; - + xhr.open( params.method || 'GET', - (params.scheme || 'http') + '://' + uri, + self.uri, true ); diff --git a/package.json b/package.json index 19b5398..99293c4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "Base64": "~0.1.2" }, "devDependencies": { - "ecstatic": "~0.1.6" + "ecstatic": "~0.1.6", + "tape": "~1.1.1" }, "repository": { "type": "git", @@ -35,5 +36,8 @@ "license": "MIT/X11", "engine": { "node": ">=0.4" + }, + "scripts": { + "test": "tape tests/*.js" } } diff --git a/tests/request_url.js b/tests/request_url.js new file mode 100644 index 0000000..38002e1 --- /dev/null +++ b/tests/request_url.js @@ -0,0 +1,49 @@ +global.window = { + location: { + host: 'localhost:8081', + port: 8081, + protocol: 'http:' + } +}; + +var noop = function() {}; +global.window.XMLHttpRequest = function() { + this.open = noop; + this.send = noop; +}; + +var test = require('tape').test; +var http = require('../index.js'); + + +test('Test simple url string', function(t) { + var url = { path: '/api/foo' }; + var request = http.get(url, noop); + + t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct'); + t.end(); + +}); + + +test('Test full url object', function(t) { + var url = { + host: "localhost:8081", + hostname: "localhost", + href: "http://localhost:8081/api/foo?bar=baz", + method: "GET", + path: "/api/foo?bar=baz", + pathname: "/api/foo", + port: "8081", + protocol: "http:", + query: "bar=baz", + search: "?bar=baz", + slashes: true + }; + + var request = http.get(url, noop); + + t.equal( request.uri, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct'); + t.end(); + +}); \ No newline at end of file -- 1.8.4 From 04a8d2b09bd29e15cadd26ab76659d497206d93b Mon Sep 17 00:00:00 2001 From: Tommy Messbauer Date: Mon, 14 Oct 2013 21:34:44 -0500 Subject: [PATCH 2/2] remove gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b512c09..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules \ No newline at end of file -- 1.8.4