1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
6
|
|
|
use Behat\Gherkin\Node\TableNode; |
7
|
|
|
use Behat\Mink\Exception\ExpectationException; |
8
|
|
|
use Behatch\HttpCall\Request; |
9
|
|
|
|
10
|
|
|
class RestContext extends BaseContext |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Request |
14
|
|
|
*/ |
15
|
|
|
protected $request; |
16
|
|
|
|
17
|
|
|
public function __construct(Request $request) |
18
|
|
|
{ |
19
|
|
|
$this->request = $request; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sends a HTTP request |
24
|
|
|
* |
25
|
|
|
* @Given I send a :method request to :url |
26
|
|
|
*/ |
27
|
|
|
public function iSendARequestTo($method, $url, PyStringNode $body = null, $files = []) |
28
|
|
|
{ |
29
|
|
|
return $this->request->send( |
|
|
|
|
30
|
|
|
$method, |
31
|
|
|
$this->locatePath($url), |
32
|
|
|
[], |
33
|
|
|
$files, |
34
|
|
|
$body !== null ? $body->getRaw() : null |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sends a HTTP request with a some parameters |
40
|
|
|
* |
41
|
|
|
* @Given I send a :method request to :url with parameters: |
42
|
|
|
*/ |
43
|
|
|
public function iSendARequestToWithParameters($method, $url, TableNode $data) |
44
|
|
|
{ |
45
|
|
|
$files = []; |
46
|
|
|
$parameters = []; |
47
|
|
|
|
48
|
|
|
foreach ($data->getHash() as $row) { |
49
|
|
|
if (!isset($row['key']) || !isset($row['value'])) { |
50
|
|
|
throw new \Exception("You must provide a 'key' and 'value' column in your table node."); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (is_string($row['value']) && substr($row['value'], 0, 1) == '@') { |
54
|
|
|
$files[$row['key']] = rtrim($this->getMinkParameter('files_path'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.substr($row['value'],1); |
55
|
|
|
} |
56
|
|
|
else { |
57
|
|
|
$parameters[$row['key']] = $row['value']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->request->send( |
|
|
|
|
62
|
|
|
$method, |
63
|
|
|
$this->locatePath($url), |
64
|
|
|
$parameters, |
65
|
|
|
$files |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sends a HTTP request with a body |
71
|
|
|
* |
72
|
|
|
* @Given I send a :method request to :url with body: |
73
|
|
|
*/ |
74
|
|
|
public function iSendARequestToWithBody($method, $url, PyStringNode $body) |
75
|
|
|
{ |
76
|
|
|
return $this->iSendARequestTo($method, $url, $body); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks, whether the response content is equal to given text |
81
|
|
|
* |
82
|
|
|
* @Then the response should be equal to |
83
|
|
|
* @Then the response should be equal to: |
84
|
|
|
*/ |
85
|
|
|
public function theResponseShouldBeEqualTo(PyStringNode $expected) |
86
|
|
|
{ |
87
|
|
|
$expected = str_replace('\\"', '"', $expected); |
88
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
89
|
|
|
$message = "Actual response is '$actual', but expected '$expected'"; |
90
|
|
|
$this->assertEquals($expected, $actual, $message); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Checks, whether the response content is null or empty string |
95
|
|
|
* |
96
|
|
|
* @Then the response should be empty |
97
|
|
|
*/ |
98
|
|
|
public function theResponseShouldBeEmpty() |
99
|
|
|
{ |
100
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
101
|
|
|
$message = "The response of the current page is not empty, it is: $actual"; |
102
|
|
|
$this->assertTrue(null === $actual || "" === $actual, $message); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks, whether the header name is equal to given text |
107
|
|
|
* |
108
|
|
|
* @Then the header :name should be equal to :value |
109
|
|
|
*/ |
110
|
|
|
public function theHeaderShouldBeEqualTo($name, $value) |
111
|
|
|
{ |
112
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
113
|
|
|
$this->assertEquals(strtolower($value), strtolower($actual), |
114
|
|
|
"The header '$name' should be equal to '$value', but it is: '$actual'" |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks, whether the header name is not equal to given text |
120
|
|
|
* |
121
|
|
|
* @Then the header :name should not be equal to :value |
122
|
|
|
*/ |
123
|
|
|
public function theHeaderShouldNotBeEqualTo($name, $value) { |
124
|
|
|
$actual = $this->getSession()->getResponseHeader($name); |
125
|
|
|
if (strtolower($value) == strtolower($actual)) { |
126
|
|
|
throw new ExpectationException( |
127
|
|
|
"The header '$name' is equal to '$actual'", |
128
|
|
|
$this->getSession()->getDriver() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function theHeaderShouldBeContains($name, $value) |
134
|
|
|
{ |
135
|
|
|
trigger_error( |
136
|
|
|
sprintf('The %s function is deprecated since version 3.1 and will be removed in 4.0. Use the %s::theHeaderShouldContain function instead.', __METHOD__, __CLASS__), |
137
|
|
|
E_USER_DEPRECATED |
138
|
|
|
); |
139
|
|
|
$this->theHeaderShouldContain($name, $value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Checks, whether the header name contains the given text |
144
|
|
|
* |
145
|
|
|
* @Then the header :name should contain :value |
146
|
|
|
*/ |
147
|
|
|
public function theHeaderShouldContain($name, $value) |
148
|
|
|
{ |
149
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
150
|
|
|
$this->assertContains($value, $actual, |
151
|
|
|
"The header '$name' should contain value '$value', but actual value is '$actual'" |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Checks, whether the header name doesn't contain the given text |
157
|
|
|
* |
158
|
|
|
* @Then the header :name should not contain :value |
159
|
|
|
*/ |
160
|
|
|
public function theHeaderShouldNotContain($name, $value) |
161
|
|
|
{ |
162
|
|
|
$this->assertNotContains($value, $this->request->getHttpHeader($name), |
|
|
|
|
163
|
|
|
"The header '$name' contains '$value'" |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Checks, whether the header not exist |
169
|
|
|
* |
170
|
|
|
* @Then the header :name should not exist |
171
|
|
|
*/ |
172
|
|
|
public function theHeaderShouldNotExist($name) |
173
|
|
|
{ |
174
|
|
|
$this->not(function () use($name) { |
175
|
|
|
$this->theHeaderShouldExist($name); |
176
|
|
|
}, "The header '$name' exists"); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function theHeaderShouldExist($name) |
180
|
|
|
{ |
181
|
|
|
return $this->request->getHttpHeader($name); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @Then the header :name should match :regex |
186
|
|
|
*/ |
187
|
|
|
public function theHeaderShouldMatch($name, $regex) |
188
|
|
|
{ |
189
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$this->assertEquals( |
192
|
|
|
1, |
193
|
|
|
preg_match($regex, $actual), |
194
|
|
|
"The header '$name' should match '$regex', but it is: '$actual'" |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @Then the header :name should not match :regex |
200
|
|
|
*/ |
201
|
|
|
public function theHeaderShouldNotMatch($name, $regex) |
202
|
|
|
{ |
203
|
|
|
$this->not( |
204
|
|
|
function () use ($name, $regex) { |
205
|
|
|
$this->theHeaderShouldMatch($name, $regex); |
206
|
|
|
}, |
207
|
|
|
"The header '$name' should not match '$regex'" |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Checks, that the response header expire is in the future |
213
|
|
|
* |
214
|
|
|
* @Then the response should expire in the future |
215
|
|
|
*/ |
216
|
|
|
public function theResponseShouldExpireInTheFuture() |
217
|
|
|
{ |
218
|
|
|
$date = new \DateTime($this->request->getHttpRawHeader('Date')[0]); |
|
|
|
|
219
|
|
|
$expires = new \DateTime($this->request->getHttpRawHeader('Expires')[0]); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$this->assertSame(1, $expires->diff($date)->invert, |
222
|
|
|
sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM)) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Add an header element in a request |
228
|
|
|
* |
229
|
|
|
* @Then I add :name header equal to :value |
230
|
|
|
*/ |
231
|
|
|
public function iAddHeaderEqualTo($name, $value) |
232
|
|
|
{ |
233
|
|
|
$this->request->setHttpHeader($name, $value); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @Then the response should be encoded in :encoding |
238
|
|
|
*/ |
239
|
|
|
public function theResponseShouldBeEncodedIn($encoding) |
240
|
|
|
{ |
241
|
|
|
$content = $this->request->getContent(); |
|
|
|
|
242
|
|
|
if (!mb_check_encoding($content, $encoding)) { |
243
|
|
|
throw new \Exception("The response is not encoded in $encoding"); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$this->theHeaderShouldContain('Content-Type', "charset=$encoding"); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @Then print last response headers |
251
|
|
|
*/ |
252
|
|
|
public function printLastResponseHeaders() |
253
|
|
|
{ |
254
|
|
|
$text = ''; |
255
|
|
|
$headers = $this->request->getHttpHeaders(); |
|
|
|
|
256
|
|
|
|
257
|
|
|
foreach ($headers as $name => $value) { |
258
|
|
|
$text .= $name . ': '. $this->request->getHttpHeader($name) . "\n"; |
|
|
|
|
259
|
|
|
} |
260
|
|
|
echo $text; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @Then print the corresponding curl command |
266
|
|
|
*/ |
267
|
|
|
public function printTheCorrespondingCurlCommand() |
268
|
|
|
{ |
269
|
|
|
$method = $this->request->getMethod(); |
|
|
|
|
270
|
|
|
$url = $this->request->getUri(); |
|
|
|
|
271
|
|
|
|
272
|
|
|
$headers = ''; |
273
|
|
|
foreach ($this->request->getServer() as $name => $value) { |
|
|
|
|
274
|
|
|
if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') { |
275
|
|
|
$headers .= " -H '$name: $value'"; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$data = ''; |
280
|
|
|
$params = $this->request->getParameters(); |
|
|
|
|
281
|
|
|
if (!empty($params)) { |
282
|
|
|
$query = http_build_query($params); |
283
|
|
|
$data = " --data '$query'" ; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
echo "curl -X $method$data$headers '$url'"; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: