1
|
|
|
<?php |
2
|
|
|
namespace Codeception\Extension; |
3
|
|
|
|
4
|
|
|
use Codeception\Module; |
5
|
|
|
use Codeception\Exception\ModuleException; |
6
|
|
|
use Codeception\Exception\ModuleConfigException; |
7
|
|
|
use \PHPBrowserMobProxy_Client as BMP; |
8
|
|
|
use \Requests; |
9
|
|
|
|
10
|
|
|
class BrowserMob extends Module |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $config = ['host', 'port', 'blacklist', 'whitelist', 'limits', 'timeouts', 'redirect', 'retry']; |
14
|
|
|
|
15
|
|
|
protected $requiredFields = ['host']; |
16
|
|
|
|
17
|
|
|
protected $lastResponse; |
18
|
|
|
|
19
|
|
|
private $bmp; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @codeCoverageIgnore |
23
|
|
|
* @ignore Codeception specific |
24
|
|
|
*/ |
25
|
|
|
public function _initialize() |
26
|
|
|
{ |
27
|
|
|
$host = $this->config['host']; |
28
|
|
|
if (isset($this->config['port'])) { |
29
|
|
|
$host = $host.':'.$this->config['port']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// test if proxy is available |
33
|
|
|
if (static::__pingProxy($host)) { |
34
|
|
|
$this->bmp = new BMP($host); |
35
|
|
|
} else { |
36
|
|
|
throw new ModuleConfigException(__CLASS__, "Proxy '{$host}' cannot be reached"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// start a new BrowserMobProxy session |
40
|
|
|
$this->bmp->open(); |
41
|
|
|
|
42
|
|
|
// set BrowserMobProxy options |
43
|
|
|
$this->__setProxyCapabilities(); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected static function __pingProxy($url) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$response = Requests::get('http://'.$url.'/proxy/'); |
51
|
|
|
} catch(\Exception $e) { |
52
|
|
|
throw new ModuleException(__CLASS__, $e->getMessage()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $response->success; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function __setProxyCapabilities() |
59
|
|
|
{ |
60
|
|
|
$response = null; |
61
|
|
|
|
62
|
|
|
foreach ($this->config as $config => $data) { |
63
|
|
|
try { |
64
|
|
|
if (false === empty($data)) { |
65
|
|
|
switch ($config) { |
66
|
|
|
case 0: // fix a weird PHP behaviour: when $config === 0 then go in 'blacklist' |
67
|
|
|
break; |
68
|
|
|
case 'blacklist': |
69
|
|
|
$response = $this->bmp->blacklist($data); |
|
|
|
|
70
|
|
|
break; |
71
|
|
|
case 'whitelist': |
72
|
|
|
$response = $this->bmp->whitelist($data); |
|
|
|
|
73
|
|
|
break; |
74
|
|
|
case 'limits': |
75
|
|
|
$response = $this->bmp->limits($data); |
76
|
|
|
break; |
77
|
|
|
case 'timeouts': |
78
|
|
|
$response = $this->bmp->timeouts($data); |
79
|
|
|
break; |
80
|
|
|
case 'redirect': |
81
|
|
|
$response = $this->bmp->remapHosts($data); |
|
|
|
|
82
|
|
|
break; |
83
|
|
|
case 'retry': |
84
|
|
|
$response = $this->bmp->retry($data); |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
// do nothing |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} catch(\Exception $e) { |
91
|
|
|
throw new ModuleConfigException(__CLASS__, $e->getMessage()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (get_class($response) === 'Request') |
95
|
|
|
{ |
96
|
|
|
if (false === $response->success) { |
97
|
|
|
throw new ModuleConfigException(__CLASS__, "Proxy response error '{$reponse->status_code}' {$respone->body}"); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
This check looks for function calls that miss required arguments.