Skip to content

Commit 7964cc5

Browse files
committed
Merge pull request #1 from jim-coffey/proxy-port-precedence
Allow configuration of proxy port in different places
2 parents 4e8e007 + 6ddf1e7 commit 7964cc5

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,58 @@ proxy.doHAR('http://yahoo.com', function(err, data) {
2626
});
2727
```
2828

29+
Configuring the Proxy Object
30+
----------------------------
31+
32+
You need to tell the Proxy() constructor where browsermob-proxy is running. The defaults are running on 'localhost' port 8080:
33+
34+
```javascript
35+
36+
proxy = new Proxy();
37+
```
38+
39+
Starting browsermob-proxy somewhere else?:
40+
41+
```javascript
42+
43+
proxy = new Proxy({ host: 'some.other.host', port: <some other port> });
44+
```
45+
46+
**Optionally Specifying a fixed Proxy Port**
47+
48+
When you create new proxies, browsermob-proxy can automatically choose which port to open them on.
49+
50+
Alternatively, you can specify the port you expect proxes to be created on when you create the proxy object by setting it here:
51+
52+
```javascript
53+
54+
proxy = new Proxy({ proxyPort: <some other port> });
55+
```
56+
57+
*N.B.* You can also specify a port when using the doHAR or cbHAR functions for more fine tuned control
58+
59+
Ports passed to doHAR / cbHAR take override any proxyPort set here. And if no proxy port is set here or passed to those functions, then browsermob-proxy will fall back to automatcially choosing a port and reporting to the user the host:port on which the proxy has been created.
60+
61+
**Optionally Specifying Selenium Host & Port**
62+
63+
IF using Selenium you can specify the host and port. The defaults are 'localhost' port 4444:
64+
65+
```javascript
66+
67+
proxy = new Proxy({ selHost: 'some.other.host', selPort: <some other port> });
68+
```
69+
70+
**Bandwidth Limits**
71+
72+
In the Proxy constructor you can specify bandwidth and latency limitations like so:
73+
74+
```javascript
75+
var proxy = new Proxy( { downloadKbps => 56, uploadKbps => 56, latency 200 } );
76+
```
77+
78+
Would tell the proxy to act like a 56K modem with 200ms latency.
79+
80+
2981
Details
3082
-------
3183

@@ -47,22 +99,6 @@ CasperJS
4799

48100
Grab the latest version of [CasperJS](http://casperjs.org)
49101

50-
Configure the Proxy Object
51-
---------------------------
52-
53-
You need to tell the Proxy() constructor where browsermob-proxy is running. The defaults are running on 'localhost' port 8080:
54-
55-
```javascript
56-
57-
proxy = new Proxy();
58-
```
59-
60-
Somewhere else?:
61-
62-
```javascript
63-
64-
proxy = new Proxy({ host: 'some.other.host', port: <some other port> });
65-
```
66102

67103
Convenience API
68104
----------------
@@ -82,6 +118,7 @@ PARAMETERS
82118
* CALLBACK(ERROR, HAR) function
83119
1. ERROR string if there was an error
84120
2. HAR string data
121+
* (optional) PROXY_PORT - port on which proxy will be available (If passed then this port will override any proxyPort set in configuration when creating the proxy)
85122

86123
EXAMPLE:
87124

@@ -105,7 +142,7 @@ Convenience method to get HAR data - this method allows you to generate whatever
105142

106143
PARAMETERS
107144

108-
* OPTIONS is an object with keys 'port', 'name', 'captureHeaders', 'captureContent' and 'captureBinaryContent'; 'port' the port number on which this proxy should be available; 'name' is an abritrary name for this run - like 'yahoo.com' or whatever you like; 'captureHeaders', 'captureContent' and 'captureBinaryContent' expect booleans indicating whether to capture resp headers, body of http transactions, and binary body of transactions. For backwards compatibility reasons, if OPTIONS is a string, it will be interpreted as the name for the run.
145+
* OPTIONS is an object with keys 'proxyPort', 'name', 'captureHeaders', 'captureContent' and 'captureBinaryContent'; 'proxyPort' the port number on which this proxy should be available (If passed then this port will override any proxyPort set in configuration when creating the proxy); 'name' is an abritrary name for this run - like 'yahoo.com' or whatever you like; 'captureHeaders', 'captureContent' and 'captureBinaryContent' expect booleans indicating whether to capture resp headers, body of http transactions, and binary body of transactions. For backwards compatibility reasons, if OPTIONS is a string, it will be interpreted as the name for the run.
109146
* GENERATE_TRAFFIC_CALLBACK(PROXY, DONE_CALLBACK)
110147

111148
PARAMETERS
@@ -184,16 +221,6 @@ Here is it put all together:
184221

185222
This will dump a HAR file named: 'searchYahooCasper.js.har' in the current directory.
186223

187-
Bandwidth Limits
188-
----------------
189-
190-
In the Proxy constructor you can specify bandwidth and latency limitations like so:
191-
192-
```javascript
193-
var proxy = new Proxy( { downloadKbps => 56, uploadKbps => 56, latency 200 } );
194-
```
195-
196-
Would tell the proxy to act like a 56K modem with 200ms latency.
197224

198225
Gory Details
199226
------------

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var http = require('http')
99
function Proxy(conf) {
1010
this.host = (conf && conf.host) || 'localhost';
1111
this.port = (conf && conf.port) || 8080;
12+
this.proxyPort = (conf && conf.proxyPort) || null;
1213
this.selHost = (conf && conf.selHost) || 'localhost';
1314
this.selPort = (conf && conf.selPort) || 4444;
1415

@@ -38,11 +39,12 @@ Proxy.prototype = {
3839
},
3940

4041
cbHAR: function(options, selCB, cb) {
41-
var _this = this;
42+
var _this = this,
43+
port = options.proxyPort || this.proxyPort;
4244
if (typeof options === "string") {
4345
options = { name: options};
4446
}
45-
this.start(options.port, function(err, data) {
47+
this.start(port, function(err, data) {
4648
if (!err) {
4749
_this.startHAR(data.port, options.name, options.captureHeaders, options.captureContent, options.captureBinaryContent, function(err, resp) {
4850
if (!err) {
@@ -159,9 +161,10 @@ Proxy.prototype = {
159161
this.doReq('PUT', '/proxy/' + port + '/limit', data, cb);
160162
},
161163

162-
doHAR: function(url, cb) {
163-
var _this = this;
164-
this.start(function(err, data) {
164+
doHAR: function(url, cb, proxyPort) {
165+
var _this = this,
166+
port = proxyPort || this.proxyPort;
167+
this.start(port, function(err, data) {
165168
if (!err) {
166169
_this.startHAR(data.port, url, function(err, resp) {
167170
if (!err) {

0 commit comments

Comments
 (0)