From 0d2f30c90e73f7f2ab54daf2c5d8fb21c14c7051 Mon Sep 17 00:00:00 2001 From: Nilesh Kulkarni Date: Wed, 3 Sep 2014 16:37:14 -0400 Subject: [PATCH 1/4] Added addHeader API info --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 9314afe..56615a9 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,12 @@ var webdriverjs = require("webdriverjs") var proxy = new Proxy( { host: proxyHost }); proxy.start(function(err, data) { if (!err) { + var headersToSet = { + 'User-Agent': 'Bananabot/1.0' + 'custom-header1':'custom-header1-value', + 'custom-header2':'custom-header2-value' + } + proxy.addHeader(that.port,headersToSet,function(){}); proxy.startHAR(data.port, 'http://search.yahoo.com', function(err, resp) { if (!err) { // DO WHATEVER WEB INTERFACTION YOU WANT USING THE PROXY @@ -338,3 +344,14 @@ Full API * CALLBACK(ERROR) function 1. ERROR string if there was an error +**addHeader(PORT, HEADERSTOSET, CALLBACK)** + + Set and override HTTP Request headers + + PARAMETERS: + + * PORT of proxy for this command + * Headers to set + * CALLBACK(ERROR) function + 1. ERROR string if there was an error + \ No newline at end of file From 04388be44dd90e3968aa52db7fda52ade0d759aa Mon Sep 17 00:00:00 2001 From: Nilesh Kulkarni Date: Wed, 3 Sep 2014 16:49:58 -0400 Subject: [PATCH 2/4] Added support for setting/overriding http request headers --- index.js | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 5e1e92b..430e493 100644 --- a/index.js +++ b/index.js @@ -185,33 +185,41 @@ Proxy.prototype = { } }); }, + addHeader: function (port, headersToAdd, cb) { + var postData = JSON.stringify(headersToAdd); + var options = { + host: this.host, port: this.port, method: 'POST', path: '/proxy/' + port + '/headers', headers: { + 'Content-Type': 'application/json' + } + }; + this.doReqWithOptions(options, postData, cb); + }, - doReq: function(method, url, postData, cb) { - - if (!cb) { // for empty requests - cb = postData; - } - + doReq: function (method, url, postData, cb) { var options = { - host: this.host - , port: this.port - , method: method - , path: url - , headers: { + host: this.host, port: this.port, method: method, path: url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } + }; + this.doReqWithOptions(options, postData, cb); + + }, + doReqWithOptions: function (options, postData, cb) { + + if (!cb) { // for empty requests + cb = postData; } - , req = http.request(options, function(res) { - var data = ''; - res.setEncoding('utf8'); - res.on('data', function (chunk) { - data += chunk; - }); - res.on('end', function() { - cb(null, data); - }); - }) - ; + var req = http.request(options, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (chunk) { + data += chunk; + }); + res.on('end', function () { + cb(null, data); + }); + }) + ; req.on('error', cb); From d4cbf35c5e1eb43798b8e1aaf4738bf55927a199 Mon Sep 17 00:00:00 2001 From: Nilesh Kulkarni Date: Wed, 3 Sep 2014 17:15:39 -0400 Subject: [PATCH 3/4] Updated docs --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56615a9..0739a5f 100644 --- a/README.md +++ b/README.md @@ -211,12 +211,13 @@ var webdriverjs = require("webdriverjs") var proxy = new Proxy( { host: proxyHost }); proxy.start(function(err, data) { if (!err) { + // SET AND OVERRIDE HTTP REQUEST HEADERS IF YOU WANT TO var headersToSet = { 'User-Agent': 'Bananabot/1.0' 'custom-header1':'custom-header1-value', 'custom-header2':'custom-header2-value' } - proxy.addHeader(that.port,headersToSet,function(){}); + proxy.addHeader(data.port,headersToSet,function(){}); proxy.startHAR(data.port, 'http://search.yahoo.com', function(err, resp) { if (!err) { // DO WHATEVER WEB INTERFACTION YOU WANT USING THE PROXY From 06c526854463f8227a19ae172def2e0bc5f92820 Mon Sep 17 00:00:00 2001 From: Nilesh Kulkarni Date: Wed, 3 Sep 2014 18:22:15 -0400 Subject: [PATCH 4/4] fixed the doc --- README.md | 60 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 0739a5f..5e24fda 100644 --- a/README.md +++ b/README.md @@ -210,37 +210,45 @@ var webdriverjs = require("webdriverjs") var proxy = new Proxy( { host: proxyHost }); proxy.start(function(err, data) { - if (!err) { - // SET AND OVERRIDE HTTP REQUEST HEADERS IF YOU WANT TO - var headersToSet = { - 'User-Agent': 'Bananabot/1.0' - 'custom-header1':'custom-header1-value', - 'custom-header2':'custom-header2-value' - } - proxy.addHeader(data.port,headersToSet,function(){}); - proxy.startHAR(data.port, 'http://search.yahoo.com', function(err, resp) { if (!err) { - // DO WHATEVER WEB INTERFACTION YOU WANT USING THE PROXY - doSeleniumStuff(proxyHost + ':' + data.port, function () { - proxy.getHAR(data.port, function(err, resp) { - if (!err) { - console.log(resp); - fs.writeFileSync('output.har', resp, 'utf8'); - } else { - console.err('Error getting HAR file: ' + err); - } - proxy.stop(data.port, function() {}); - }); + // SET AND OVERRIDE HTTP REQUEST HEADERS IF YOU WANT TO + var headersToSet = { + 'User-Agent': 'Bananabot/1.0', + 'custom-header1': 'custom-header1-value', + 'custom-header2': 'custom-header2-value' + } + proxy.addHeader(data.port, headersToSet, function (err,resp) { + if(!err) { + proxy.startHAR(data.port, 'http://localhost:8004', function (err, resp) { + if (!err) { + // DO WHATEVER WEB INTERFACTION YOU WANT USING THE PROXY + doSeleniumStuff(proxyHost + ':' + data.port, function () { + proxy.getHAR(data.port, function(err, resp) { + if (!err) { + console.log(resp); + fs.writeFileSync('output.har', resp, 'utf8'); + } else { + console.err('Error getting HAR file: ' + err); + } + proxy.stop(data.port, function() {}); + }); + }); + } else { + console.error('Error starting HAR: ' + err); + proxy.stop(data.port, function () { + }); + } + }); + } else { + console.error('Error setting the custom headers'); + proxy.stop(data.port, function () { + }); + } }); } else { - console.error('Error starting HAR: ' + err); - proxy.stop(data.port,function() {}); + console.error('Error starting proxy: ' + err); } }); - } else { - console.error('Error starting proxy: ' + err); - } - }); function doSeleniumStuff(proxy, cb) {