Skip to content

Commit

Permalink
Merge pull request #14 from nikulkarni/master
Browse files Browse the repository at this point in the history
Added support to set and override HTTP Request headers
  • Loading branch information
zzo committed Sep 3, 2014
2 parents 8c6334a + 06c5268 commit 71733a9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
64 changes: 45 additions & 19 deletions README.md
Expand Up @@ -210,30 +210,45 @@ var webdriverjs = require("webdriverjs")

var proxy = new Proxy( { host: proxyHost });
proxy.start(function(err, data) {
if (!err) {
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) {
Expand Down Expand Up @@ -338,3 +353,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

52 changes: 30 additions & 22 deletions index.js
Expand Up @@ -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);

Expand Down

0 comments on commit 71733a9

Please sign in to comment.