Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit b5f8e36

Browse files
author
Greg Heitz
committed
Add documentation
1 parent bb99c97 commit b5f8e36

File tree

5 files changed

+227
-30
lines changed

5 files changed

+227
-30
lines changed

README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
The [Codeception](http://codeception.com/) module for [BrowserMob Proxy](http://bmp.lightbody.net/)
1212

13-
:bangbang: **THIS MODULE IS UNDER DEVELOPMENT**
13+
## Roadmap
14+
- [x] **0.1**: Initial version based on library [PHPBrowserMobProxy by chartjes](https://github.com/chartjes/PHPBrowserMobProxy/) with imited support of BrowserMob Proxy legacy API and no Littleproxy support
15+
- [ ] 0.2: New PHP BrowserMob Proxy with full REST API support (Jetty and Littleproxy) and [proxy auto-configuration](https://en.wikipedia.org/wiki/Proxy_auto-config) feature.
1416

1517
## Minimum Requirements
1618
- Codeception 2.2
1719
- PHP 5.5
18-
- BrowserMob Proxy
20+
- BrowserMob Proxy 2.0
1921

2022
## Installation
2123
The module can be installed using [Composer](https://getcomposer.org)
@@ -34,4 +36,61 @@ Enabling **BrowserMob** is done in your configuration file `.yml`.
3436
module:
3537
enabled:
3638
- Codeception\Extension\BrowserMob
39+
host: 'localhost'
40+
port: 8080
3741
```
42+
43+
### Parameters
44+
**BrowserMob** support following configuration parameters:
45+
46+
| Parameter | Type | Description |
47+
|-----------|------|-------------|
48+
| `host` | **string** | BrowserMob Proxy host |
49+
| `port` | **integer** | BrowserMob Proxy port |
50+
| `autostart` | **boolean** | Start a new proxy instance automatically [*default = false*] |
51+
| `whitelist` | **array** | <p>URLs whitelisting</p><p>`code`: HTTP status code for URLs that do not match patterns</p><p>`patterns`: array of whitelisted URLs patterns<p> |
52+
| `blacklist` | **array** | <p>URLs blacklisting</p><p>`code`: HTTP status code for URLs that match patterns</p><p>`patterns`: array of blacklisted URLs patterns<p> |
53+
| `limits` | **array** | Bandwidth limits through the proxy (see [BrowserMob Proxy REST API](https://github.com/lightbody/browsermob-proxy#rest-api))|
54+
| `timeouts` | **array** | Proxy timeouts (see [BrowserMob Proxy REST API](https://github.com/lightbody/browsermob-proxy#rest-api)) |
55+
| `basicAuth` | **array** | <p>Sets automatic basic authentication a list of domains</p><p>`domain`: domain name</p><p>`options`:HTTP authentication parameters using format *`name: value`* (see [BrowserMob Proxy REST API](https://github.com/lightbody/browsermob-proxy#rest-api))</p> |
56+
| `dns` | **array** | <p>Internal proxy DNS using pairs of domain/IP to map</p><p>`domain`: domain name</p><p>`ip`: matching IP</p> |
57+
| `retry` | **integer** | Number of times a method will be retried [*default = 0*] |
58+
59+
***Example***
60+
```yaml
61+
modules:
62+
config:
63+
Codeception\Extension\BrowserMob:
64+
host: 'localhost'
65+
port: 9090
66+
autostart: true
67+
whitelist:
68+
code: 404
69+
patterns:
70+
- 'http://codeception.com/'
71+
limits:
72+
downstreamKbps: 12
73+
upstreamKbps: 12
74+
latency: 1
75+
timeouts:
76+
request: 10
77+
read: 10
78+
connection: 10
79+
dns: 10
80+
basicAuth:
81+
- domain: example.local
82+
options:
83+
username: myUsername
84+
password: myPassword
85+
dns:
86+
- domain: example.local
87+
ip: 127.0.0.1
88+
- domain: wikipedia.org
89+
ip: 192.168.1.1
90+
retry: 3
91+
```
92+
93+
## Documentation
94+
The module documentation is available in the [wiki](https://github.com/edno/codeception-browsermob/wiki/Codeception-BrowserMob-Proxy-extension-Documentation).
95+
96+
For more information on how to use BrowserMob proxy, refer to [BrowserMob Proxy REST API documentation](https://github.com/lightbody/browsermob-proxy#rest-api).

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
},
1818
"require-dev": {
1919
"satooshi/php-coveralls": "^1.0",
20-
"victorjonsson/markdowndocs": "dev-master"
20+
"phpdocumentor/phpdocumentor": "^2.0",
21+
"cvuorinen/phpdoc-markdown-public": "*"
2122
},
2223
"autoload": {
2324
"psr-4": {
@@ -45,8 +46,10 @@
4546
"docker rmi codecept/browsermob"
4647
],
4748
"phpdoc-md": [
48-
"mkdir -p docs",
49-
"vendor/victorjonsson/markdowndocs/bin/phpdoc-md generate src > docs/extension.md"
49+
"rm -rf docs",
50+
"git clone https://github.com/edno/codeception-browsermob.wiki.git docs",
51+
"./vendor/bin/phpdoc --quiet",
52+
"mv docs/README.md docs/Codeception-BrowserMob-Proxy-extension-Documentation.md"
5053
]
5154
}
5255
}

docs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 7bc48687231c101860cf64bf233a9b7fc0cea7f4
1+
Subproject commit a83d179f05830c127f26806b01543df2de6d8a89

phpdoc.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpdoc>
3+
<title>Codeception BrowserMob Proxy extension Documentation</title>
4+
<parser>
5+
<target>docs/build</target>
6+
</parser>
7+
<transformer>
8+
<target>docs</target>
9+
</transformer>
10+
<transformations>
11+
<template name="vendor/cvuorinen/phpdoc-markdown-public/data/templates/markdown-public" />
12+
</transformations>
13+
<files>
14+
<directory>src</directory>
15+
<ignore>tests/*</ignore>
16+
<ignore>vendor/*</ignore>
17+
</files>
18+
</phpdoc>

src/BrowserMob.php

Lines changed: 141 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,45 @@
99
use \RuntimeException;
1010

1111
/**
12-
* @method void _open()
13-
* @method void _close()
14-
* @method string _newHar(string $label='')
15-
* @method string _newPage(string $label='')
16-
* @method string _blacklist(string $regexp, integer $status_code)
17-
* @method string _whitelist(string $regexp, integer $status_code)
18-
* @method string _basicAuth(string $domain, string[] $options)
19-
* @method string _headers(string[] $options)
20-
* @method string _responseInterceptor(string $js)
21-
* @method string _requestInterceptor(string $js)
22-
* @method Requests_Response _limits(string[] $options)
23-
* @method Requests_Response _timeouts(string[] $options)
24-
* @method string _remapHosts(string $address, string $ip_address)
25-
* @method string _waitForTrafficToStop(integer $quiet_period, integer $timeout)
26-
* @method string _clearDnsCache()
27-
* @method string _rewriteUrl(string $match, string $replace)
28-
* @method string _retry(integer $retry_count)
12+
* @method void _open() Open a new proxy using the PHPBrowserMobProxy_Client method
13+
* @method void _close() Close current proxy using the PHPBrowserMobProxy_Client method
14+
* @method \Requests_Response _newHar(string $label='') Start new HAR using the PHPBrowserMobProxy_Client method
15+
* @method \Requests_Response _newPage(string $label='') Start new HAR page using the PHPBrowserMobProxy_Client method
16+
* @method \Requests_Response _blacklist(string $regexp, integer $status_code) Blacklist URLs using the PHPBrowserMobProxy_Client method
17+
* @method \Requests_Response _whitelist(string $regexp, integer $status_code) Whitelist URLs using the PHPBrowserMobProxy_Client method
18+
* @method \Requests_Response _basicAuth(string $domain, string[] $options) Set HTTP authentication headers using the PHPBrowserMobProxy_Client method
19+
* @method \Requests_Response _headers(string[] $options) Override requests HTTP headers using the PHPBrowserMobProxy_Client method
20+
* @method \Requests_Response _responseInterceptor(string $js) Intercept HTTP responses using the PHPBrowserMobProxy_Client method
21+
* @method \Requests_Response _requestInterceptor(string $js) Intercept HTTP requests using the PHPBrowserMobProxy_Client method
22+
* @method \Requests_Response _limits(string[] $options) Set proxy limits using the PHPBrowserMobProxy_Client method
23+
* @method \Requests_Response _timeouts(string[] $options) Set proxy timeouts using the PHPBrowserMobProxy_Client method
24+
* @method \Requests_Response _remapHosts(string $address, string $ip_address) Map hosts to IP using the PHPBrowserMobProxy_Client method
25+
* @method \Requests_Response _waitForTrafficToStop(integer $quiet_period, integer $timeout) Wait for traffic before stopping proxy using the PHPBrowserMobProxy_Client method
26+
* @method string _clearDnsCache() Flux proxy DNS cache using the PHPBrowserMobProxy_Client method
27+
* @method \Requests_Response _rewriteUrl(string $match, string $replace) Rewrite URLs using the PHPBrowserMobProxy_Client method
28+
* @method \Requests_Response _retry(integer $retry_count) Set proxy retries using the PHPBrowserMobProxy_Client method
2929
*/
3030
class BrowserMob extends Module
3131
{
3232

33+
/**
34+
* @var string[] $config
35+
*/
3336
protected $config = ['host', 'port', 'autostart', 'blacklist', 'whitelist', 'limits', 'timeouts', 'dns', 'retry', 'basicAuth', 'littleproxy'];
3437

35-
protected $requiredFields = ['host'];
38+
/**
39+
* @var string[] $requiredFields
40+
*/
41+
protected $requiredFields = ['host', 'port'];
3642

43+
/**
44+
* @var Requests $response
45+
*/
3746
protected $response;
3847

48+
/**
49+
* @var PHPBrowserMobProxy_Client $bmp
50+
*/
3951
private $bmp;
4052

4153
/**
@@ -44,10 +56,7 @@ class BrowserMob extends Module
4456
*/
4557
public function _initialize()
4658
{
47-
$host = $this->config['host'];
48-
if (isset($this->config['port'])) {
49-
$host = $host.':'.$this->config['port'];
50-
}
59+
$host = $this->config['host'].':'.$this->config['port'];
5160

5261
// test if proxy is available
5362
if (static::__pingProxy($host)) {
@@ -64,7 +73,14 @@ public function _initialize()
6473
}
6574
}
6675

67-
protected static function __pingProxy($url)
76+
/**
77+
* Verify if the BrowserMobProxy is reachable
78+
*
79+
* @param string $host BrowserMob Proxy host, format host:port
80+
*
81+
* @return boolean Returns true if proxy available, else false
82+
*/
83+
protected static function __pingProxy($host)
6884
{
6985
try {
7086
$response = Requests::get('http://'.$url.'/proxy/');
@@ -75,6 +91,21 @@ protected static function __pingProxy($url)
7591
return $response->success;
7692
}
7793

94+
/**
95+
* Set proxy capabilitites: blacklist, whitelist, limits, timeouts, dns, retry, basicAuth
96+
*
97+
* @uses BrowserMob::_blacklist
98+
* @uses BrowserMob::_whitelist
99+
* @uses BrowserMob::_limits
100+
* @uses BrowserMob::_timeouts
101+
* @uses BrowserMob::_remapHosts
102+
* @uses BrowserMob::_retry
103+
* @uses BrowserMob::_basicAuth
104+
*
105+
* @param mixed[] $options Array of options (see extension configuration)
106+
*
107+
* @return void
108+
*/
78109
protected function __setProxyCapabilities($options)
79110
{
80111
foreach ($options as $config => $data) {
@@ -115,11 +146,29 @@ protected function __setProxyCapabilities($options)
115146
}
116147
}
117148

149+
/**
150+
* Return current proxy port opened on BrowserMobProxy
151+
*
152+
* @return integer Proxy port
153+
*/
118154
public function getProxyPort()
119155
{
120156
return $this->bmp->port;
121157
}
122158

159+
/**
160+
* Open a new proxy on BrowserMobProxy
161+
*
162+
* @see BrowserMob::__setProxyCapabilities
163+
*
164+
* @uses BrowserMob::_open
165+
* @uses BrowserMob::__setProxyCapabilities
166+
* @uses BrowserMob::getProxyPort
167+
*
168+
* @param mixed[]|null $capabilities Array of capabilities. Use extension configuration if null
169+
*
170+
* @return integer Proxy port
171+
*/
123172
public function openProxy($capabilities = null)
124173
{
125174
$this->_open();
@@ -130,53 +179,121 @@ public function openProxy($capabilities = null)
130179
return $this->getProxyPort();
131180
}
132181

182+
/**
183+
* Close the current proxy opened BrowserMobProxy
184+
* Not supported by library chartjes/php-browsermob-proxy
185+
*
186+
* @uses BrowserMob::_close
187+
*
188+
* @return void
189+
*/
133190
public function closeProxy()
134191
{
135192
$this->_close();
136193
}
137194

195+
/**
196+
* Start to capture the HTTP archive
197+
*
198+
* @uses BrowserMob::_newHar
199+
*
200+
* @param string|null $label Title of first HAR page
201+
*
202+
* @return boolean Command status
203+
*/
138204
public function startHar($label = '')
139205
{
140206
$this->_newHar($label);
141207
return $this->response->success;
142208
}
143209

210+
/**
211+
* Add a new page to the HTTP archive
212+
*
213+
* @uses BrowserMob::_newPage
214+
*
215+
* @param string|null $label Title of new HAR page
216+
*
217+
* @return boolean Command status
218+
*/
144219
public function addPage($label = '')
145220
{
146221
$this->_newPage($label);
147222
return $this->response->success;
148223
}
149224

225+
/**
226+
* Get the HTTP archive captured
227+
*
228+
* @return mixed[] HTTP archive
229+
*/
150230
public function getHar()
151231
{
152232
return $this->bmp->har;
153233
}
154234

235+
/**
236+
* Override HTTP request headers
237+
*
238+
* @uses BrowserMob::_headers
239+
*
240+
* @param string[] $headers Array of HTTP headers
241+
*
242+
* @return boolean Command status
243+
*/
155244
public function setHeaders($headers)
156245
{
157246
$this->_headers($headers);
158247
return $this->response->success;
159248
}
160249

250+
/**
251+
* Rewrite URLs with regex
252+
*
253+
* @uses BrowserMob::_rewriteUrl
254+
*
255+
* @param string $match Matching URL regular expression
256+
* @param string $replace Replacement URL
257+
*
258+
* @return boolean Command status
259+
*/
161260
public function redirectUrl($match, $replace)
162261
{
163262
$this->_rewriteUrl($match, $replace);
164263
return $this->response->success;
165264
}
166265

266+
/**
267+
* Run Javascript against requests before sending them
268+
*
269+
* @param string $script Javascript code
270+
*
271+
* @return boolean Command status
272+
*/
167273
public function filterRequest($script)
168274
{
169275
$this->_requestInterceptor($script);
170276
return $this->response->success;
171277
}
172278

279+
/**
280+
* Run Javascript against responses received
281+
*
282+
* @param string $script Javascript code
283+
*
284+
* @return boolean Command status
285+
*/
173286
public function filterResponse($script)
174287
{
175288
$this->_responseInterceptor($script);
176289
return $this->response->success;
177290
}
178291

179-
// magic function that exposes BrowserMobProxy API pulic methods
292+
/**
293+
* Magic function that exposes BrowserMobProxy API pulic methods
294+
*
295+
* @ignore Exclude from documentation
296+
*/
180297
public function __call($name, $args)
181298
{
182299
// check if is a command call

0 commit comments

Comments
 (0)