1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use GuzzleHttp\Middleware; |
21
|
|
|
use Phing\Exception\BuildException; |
22
|
|
|
use Phing\Type\Parameter; |
23
|
|
|
use Phing\Util\Regexp; |
24
|
|
|
use Phing\Util\RegexpException; |
25
|
|
|
use Phing\Util\StringHelper; |
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
27
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
28
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A HTTP request task. |
32
|
|
|
* Making an HTTP request and try to match the response against an provided |
33
|
|
|
* regular expression. |
34
|
|
|
* |
35
|
|
|
* @package phing.tasks.ext |
36
|
|
|
* @author Benjamin Schultz <[email protected]> |
37
|
|
|
* @since 2.4.1 |
38
|
|
|
*/ |
39
|
|
|
class HttpRequestTask extends HttpTask |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Holds the regular expression that should match the response |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $responseRegex = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Holds the regular expression that should match the response code |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $responseCodeRegex = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether to enable detailed logging |
57
|
|
|
* |
58
|
|
|
* @var boolean |
59
|
|
|
*/ |
60
|
|
|
protected $verbose = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Holds additional post parameters for the request |
64
|
|
|
* |
65
|
|
|
* @var Parameter[] |
66
|
|
|
*/ |
67
|
|
|
protected $postParameters = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var Regexp |
71
|
|
|
*/ |
72
|
|
|
private $regexp; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets the response regex |
76
|
|
|
* |
77
|
|
|
* @param string $regex |
78
|
|
|
*/ |
79
|
2 |
|
public function setResponseRegex($regex) |
80
|
|
|
{ |
81
|
2 |
|
$this->responseRegex = $regex; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the response code regex |
86
|
|
|
* |
87
|
|
|
* @param string $regex |
88
|
|
|
*/ |
89
|
1 |
|
public function setResponseCodeRegex($regex) |
90
|
|
|
{ |
91
|
1 |
|
$this->responseCodeRegex = $regex; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets whether to enable detailed logging |
96
|
|
|
* |
97
|
|
|
* @param boolean $verbose |
98
|
|
|
*/ |
99
|
|
|
public function setVerbose($verbose) |
100
|
|
|
{ |
101
|
|
|
$this->verbose = StringHelper::booleanValue($verbose); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The setter for the method |
106
|
|
|
* |
107
|
|
|
* @param $method |
108
|
|
|
*/ |
109
|
1 |
|
public function setMethod($method) |
110
|
|
|
{ |
111
|
1 |
|
$this->method = $method; |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Creates post body parameters for this request |
116
|
|
|
* |
117
|
|
|
* @return Parameter The created post parameter |
118
|
|
|
*/ |
119
|
1 |
|
public function createPostParameter() |
120
|
|
|
{ |
121
|
1 |
|
$num = array_push($this->postParameters, new Parameter()); |
122
|
|
|
|
123
|
1 |
|
return $this->postParameters[$num - 1]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Load the necessary environment for running this task. |
128
|
|
|
* |
129
|
|
|
* @throws BuildException |
130
|
|
|
*/ |
131
|
8 |
|
public function init() |
132
|
|
|
{ |
133
|
8 |
|
parent::init(); |
134
|
|
|
|
135
|
8 |
|
$this->regexp = new Regexp(); |
136
|
8 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Creates, configures, and sends a request |
140
|
|
|
* |
141
|
|
|
* @param array $options |
142
|
|
|
* @return ResponseInterface |
143
|
|
|
*/ |
144
|
7 |
|
protected function request($options = []) |
145
|
|
|
{ |
146
|
7 |
|
if ($this->method === 'POST') { |
147
|
1 |
|
$idx = ($this->isHeaderSet('content-type', 'application/json') ? 'json' : 'form_params'); |
148
|
1 |
|
$options[$idx] = array_reduce( |
149
|
1 |
|
$this->postParameters, |
150
|
1 |
|
function ($carry, Parameter $postParameter) { |
151
|
1 |
|
return $carry + [$postParameter->getName() => $postParameter->getValue()]; |
152
|
1 |
|
}, |
153
|
1 |
|
[] |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
if ($this->verbose) { |
158
|
|
|
self::getHandlerStack()->push(Middleware::log(new ConsoleLogger(new ConsoleOutput()), new \GuzzleHttp\MessageFormatter())); |
159
|
|
|
} |
160
|
|
|
|
161
|
7 |
|
return parent::request($options); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
private function isHeaderSet($headerName, $headerValue) |
165
|
|
|
{ |
166
|
1 |
|
$isSet = false; |
167
|
|
|
|
168
|
1 |
|
foreach ($this->headers as $header) { |
169
|
|
|
if ($header->getName() === $headerName && $header->getValue() === $headerValue) { |
170
|
|
|
$isSet = true; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $isSet; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Checks whether response body or status-code matches the given regexp |
179
|
|
|
* |
180
|
|
|
* @param ResponseInterface $response |
181
|
|
|
* @return void |
182
|
|
|
* @throws RegexpException |
183
|
|
|
*/ |
184
|
4 |
|
protected function processResponse(ResponseInterface $response) |
185
|
|
|
{ |
186
|
4 |
|
if ($this->responseRegex !== '') { |
187
|
2 |
|
$this->regexp->setPattern($this->responseRegex); |
188
|
|
|
|
189
|
2 |
|
if (!$this->regexp->matches($response->getBody())) { |
190
|
1 |
|
throw new BuildException('The received response body did not match the given regular expression'); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
$this->log('The response body matched the provided regex.'); |
194
|
|
|
} |
195
|
|
|
|
196
|
3 |
|
if ($this->responseCodeRegex !== '') { |
197
|
1 |
|
$this->regexp->setPattern($this->responseCodeRegex); |
198
|
|
|
|
199
|
1 |
|
if (!$this->regexp->matches($response->getStatusCode())) { |
200
|
|
|
throw new BuildException('The received response status-code did not match the given regular expression'); |
201
|
|
|
} |
202
|
|
|
|
203
|
1 |
|
$this->log('The response status-code matched the provided regex.'); |
204
|
|
|
} |
205
|
3 |
|
} |
206
|
|
|
} |
207
|
|
|
|