From fa0f2ac817c5dc6bbb46551dbcbcff9d4c4885b4 Mon Sep 17 00:00:00 2001 From: Bajax Date: Fri, 27 Oct 2017 18:32:22 -0400 Subject: [PATCH] client - adding ability to include query string in request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s ugly, but not against the specification. In an indirect, kind of heavily interpreted way, it’s explicitly allowed, see: http://xmlrpc.scripting.com/spec.html >The format of the URI in the first line of the header is not specified. This, as far as I can tell, indicates that it CAN include query strings. Tests are included. --- lib/client.js | 20 +++++++++++++------- test/client_test.js | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/client.js b/lib/client.js index b875461..23299f1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -31,16 +31,22 @@ function Client(options, isSecure) { // If a string URI is passed in, converts to URI fields if (typeof options === 'string') { - options = url.parse(options) - options.host = options.hostname - options.path = options.pathname + var parsedUrl = url.parse(options) + options = {} + options.host = parsedUrl.hostname + options.path = parsedUrl.pathname + options.port = parsedUrl.port + if (parsedUrl.query) + options.path += '?' + parsedUrl.query } if (typeof options.url !== 'undefined') { - var parsedUrl = url.parse(options.url); - options.host = parsedUrl.hostname; - options.path = parsedUrl.pathname; - options.port = parsedUrl.port; + var parsedUrl = url.parse(options.url) + options.host = parsedUrl.hostname + options.path = parsedUrl.pathname + options.port = parsedUrl.port + if (parsedUrl.query) + options.path += '?' + parsedUrl.query } // Set the HTTP request headers diff --git a/test/client_test.js b/test/client_test.js index 77cba21..c9454b4 100644 --- a/test/client_test.js +++ b/test/client_test.js @@ -59,6 +59,30 @@ vows.describe('Client').addBatch({ assert.deepEqual(topic, { host: 'localhost', port: 9999, path: '/', method: 'POST', headers: headers }) } } + // Test with a string for options, including a query string in the path + , 'with a string URI for options that includes a path with a query string' : { + topic: function () { + var client = new Client('http://localhost:9999?test=test', false) + return client.options + } + , 'parses the string URI into URI fields' : function (topic) { + assert.strictEqual(topic.host, 'localhost') + assert.strictEqual(topic.path, '/?test=test') + assert.equal(topic.port, 9999) + } + } + // Test with options object, including a path with a query string. + , 'with a string URI for options that includes a query string' : { + topic: function () { + var client = new Client({host:'localhost', path : '/?test=test', port : 9999}, false) + return client.options + } + , 'parses the string URI into URI fields' : function (topic) { + assert.strictEqual(topic.host, 'localhost') + assert.strictEqual(topic.path, '/?test=test') + assert.equal(topic.port, 9999) + } + } // Test passing HTTP Basic authentication credentials , 'with basic auth passed' : { topic: function () {