Skip to content

Commit 58442c9

Browse files
committed
first relize
2 parents 240de40 + 6dca727 commit 58442c9

File tree

8 files changed

+1217
-114
lines changed

8 files changed

+1217
-114
lines changed

index.js

Lines changed: 241 additions & 70 deletions
Large diffs are not rendered by default.

out/browserMobProxyClientApi.html

Lines changed: 395 additions & 2 deletions
Large diffs are not rendered by default.

out/global.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="browserMo
10091009
<br class="clear">
10101010

10111011
<footer>
1012-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jul 28 2016 00:16:54 GMT+0300 (MSK)
1012+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Jul 29 2016 16:33:34 GMT+0300 (MSK)
10131013
</footer>
10141014

10151015
<script> prettyPrint(); </script>

out/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="browserMo
5656
<br class="clear">
5757

5858
<footer>
59-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jul 28 2016 00:16:54 GMT+0300 (MSK)
59+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Jul 29 2016 16:33:34 GMT+0300 (MSK)
6060
</footer>
6161

6262
<script> prettyPrint(); </script>

out/index.js.html

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ <h1 class="page-title">Source: index.js</h1>
171171
/** Class - client for interaction with BrowserMob Proxy REST API Server. */
172172
class browserMobProxyClientApi {
173173

174-
constructor(urlServerAPI, clientHost, clientPort) {
174+
constructor(urlServerAPI, clientHost, clientPort, trustAllServers = true) {
175175
//save url to BrowserMob Proxy API Server
176176
this.serverUrl = urlServerAPI;
177177

178178
//api path and options
179-
let apiUrl = `${this.serverUrl}/proxy`;
179+
let apiUrl = `${this.serverUrl}/proxy?trustAllServers=${trustAllServers}`;
180180
const options = { method : 'POST' };
181181

182182
//connect to external proxy, if needed
183183
if(clientHost &amp;&amp; clientPort){
184-
apiUrl = `${this.serverUrl}/proxy?httpProxy=${clientHost}:${clientPort}`;
184+
apiUrl = `${this.serverUrl}/proxy?httpProxy=${clientHost}:${clientPort}&amp;trustAllServers=${trustAllServers}`;
185185
}
186186
//create new proxy
187187
const self = this;
@@ -388,39 +388,50 @@ <h1 class="page-title">Source: index.js</h1>
388388
};
389389
/**
390390
* Set and override HTTP Request headers
391-
* @param {object} headers - Represents set of headers, Where key is a header name and value is a value of HTTP header
391+
* @param {object} headers - Represents set of headers, where key is a header name and value is a value of HTTP header
392392
* @returns {Promise}
393393
*/
394394
setHeaders(headers) {
395395

396-
headers = headers || {};
396+
const apiUrl = `${this.apiUrl}/headers`;
397+
const options = { method : 'POST', json : true, body : headers};
397398

398-
let apiUrl = `${this.url}/proxy/${this._lpPort}/headers`;
399-
try {
400-
let options = { method : 'POST', json : true, body : headers};
401-
402-
return co(function* (){
403-
let result = yield lpClass[bmpRequest](apiUrl, options);
404-
return result;
405-
});
406-
} catch (err){
407-
throw err;
408-
}
399+
return co(function* (){
400+
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
401+
});
409402
};
410403

404+
/**
405+
* Overrides normal DNS lookups and remaps the given hosts with the associated IP address
406+
* @param {object} dns - Represents set of of hosts, where key is a host name and value is a IP address which associated with host name
407+
* @returns {Promise}
408+
*/
411409
overrideDNS (dns) {
412410

413-
dns = dns || {};
414-
415-
let apiUrl = `${this.url}/proxy/${this._lpPort}/headers`;
416-
let options = { method : 'POST', json : dns};
411+
const apiUrl = `${this.apiUrl}/hosts`;
412+
const options = { method : 'POST', json : true, body : dns};
417413

418414
return co(function* (){
419-
let result = yield lpClass[bmpRequest](apiUrl, options);
420-
return result;
415+
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
421416
});
422417
};
418+
/**
419+
* Sets automatic basic authentication for the specified domain. This method supports only BASIC authentication.
420+
* @param {object} auth - Object describes authentication data
421+
* @param {string} auth.username - Login
422+
* @param {string} auth.password - Password
423+
* @param {string} domain - At the domain will be applying basic auth
424+
* @returns {Promise}
425+
*/
426+
setAutoAuthentication (auth, domain) {
423427

428+
const apiUrl = `${this.apiUrl}/hosts`;
429+
const options = { method : 'POST', json : true, body : dns};
430+
431+
return co(function* (){
432+
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
433+
});
434+
}
424435
//does it work? test fails
425436
waitRequests({quitePeriodInMs = 0, timeoutInMs = 0}) {
426437

@@ -532,7 +543,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="browserMo
532543
<br class="clear">
533544

534545
<footer>
535-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jul 28 2016 00:16:54 GMT+0300 (MSK)
546+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Jul 29 2016 16:33:34 GMT+0300 (MSK)
536547
</footer>
537548

538549
<script> prettyPrint(); </script>

tests/helper/moronHTTP.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ const createHTTPServer = (port = 58080) => {
1414
const pathToImage = path.join(moronLocalPath, imageName);
1515

1616
createHTTPServer.imageName = imageName;
17+
createHTTPServer.defaultContent = '<html><body><h1>MoronHTTP</h1></body></html>';
1718
createHTTPServer.image = (fs.readFileSync(pathToImage));
1819
createHTTPServer.imageBase64 = createHTTPServer.image.toString('base64');
1920
createHTTPServer.oneMbitBuffer = buffer.alloc(1024 * 1024, 74);
21+
createHTTPServer.authUserName = 'user';
22+
createHTTPServer.authPassword = 'password';
23+
2024
fs.stat(pathToImage, (err, stat) => {
2125
createHTTPServer.imageSize = stat.size;
2226
});
@@ -46,12 +50,28 @@ const createHTTPServer = (port = 58080) => {
4650
res.write('<html><body><img src="moron.jpeg"></body></html>');
4751
res.end();
4852
break;
53+
case '/retries' :
54+
res.setHeader('Content-Type', 'text/html');
55+
res.writeHeader(200);
56+
//TODO should return number of retries.
57+
res.write('retries');
58+
res.end();
59+
break;
4960
case '/1MbitContent' :
5061
res.setHeader('Content-Type', 'text/plain');
5162
res.writeHeader(200);
5263
res.write(createHTTPServer.oneMbitBuffer);
5364
res.end();
5465
break;
66+
case '/readTimeout' :
67+
res.setHeader('Content-Type', 'text/plain');
68+
res.writeHeader(200);
69+
res.write(createHTTPServer.oneMbitBuffer, () => {
70+
setTimeout(() => {
71+
res.end();
72+
}, 10000);
73+
});
74+
break;
5575
case '/upload1Mbit':
5676
req.on('data', (data) => {
5777
return;
@@ -65,13 +85,30 @@ const createHTTPServer = (port = 58080) => {
6585
case '/returnHeaders' :
6686
res.writeHeader(200, {'Content-Type' : 'application/json'});
6787
res.end(JSON.stringify(req.headers));
88+
case '/auth':
89+
console.log(req.headers.authorization);
90+
if (req.headers.authorization) {
91+
const auth = new Buffer(req.headers.authorization.split(/\s+/).pop(), 'base64').toString();
92+
const user = auth.split(':')[0];
93+
const pass = auth.split(':')[1];
94+
if (user === createHTTPServer.authUserName && pass === createHTTPServer.authPassword) {
95+
res.setHeader('Content-Type', 'text/html');
96+
res.writeHeader(200);
97+
res.write(createHTTPServer.defaultContent);
98+
} else {
99+
res.writeHeader(401);
100+
}
101+
} else {
102+
res.writeHeader(401);
103+
}
104+
res.end();
68105
break;
69106
default:
70107
res.setHeader('Content-Type', 'text/html');
71108
res.setHeader('Header1', 'value1');
72109
res.setHeader('Header2', 'value2');
73110
res.writeHeader(200);
74-
res.write('<html><body><h1>MoronHTTP</h1></body></html>');
111+
res.write(createHTTPServer.defaultContent);
75112
res.end();
76113
break;
77114
}

tests/helper/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const promiseRequest = (url, anyParam) => {
1818
try {
1919
if(response.statusCode == 200 || response.statusCode == 204){
2020
console.log('parsed status (OK) : ', response.statusCode);
21-
return resolve();
21+
return resolve(body);
2222
} else {
2323
console.log('parsed status (FAIL) : ', response.statusCode);
2424
return reject(response);

0 commit comments

Comments
 (0)