|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Behatch\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
6
|
|
|
use Behatch\HttpCall\Request; |
|
7
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
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[] = sprintf('%s=%s', $row['key'], $row['value']); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
parse_str(implode('&', $parameters), $parameters); |
|
62
|
|
|
|
|
63
|
|
|
return $this->request->send( |
|
|
|
|
|
|
64
|
|
|
$method, |
|
65
|
|
|
$this->locatePath($url), |
|
66
|
|
|
$parameters, |
|
67
|
|
|
$files |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sends a HTTP request with a body |
|
73
|
|
|
* |
|
74
|
|
|
* @Given I send a :method request to :url with body: |
|
75
|
|
|
*/ |
|
76
|
|
|
public function iSendARequestToWithBody($method, $url, PyStringNode $body) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->iSendARequestTo($method, $url, $body); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Checks, whether the response content is equal to given text |
|
83
|
|
|
* |
|
84
|
|
|
* @Then the response should be equal to |
|
85
|
|
|
*/ |
|
86
|
|
|
public function theResponseShouldBeEqualTo(PyStringNode $expected) |
|
87
|
|
|
{ |
|
88
|
|
|
$expected = str_replace('\\"', '"', $expected); |
|
89
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
|
|
90
|
|
|
$message = "Actual response is '$actual', but expected '$expected'"; |
|
91
|
|
|
$this->assertEquals($expected, $actual, $message); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks, whether the response content is null or empty string |
|
96
|
|
|
* |
|
97
|
|
|
* @Then the response should be empty |
|
98
|
|
|
*/ |
|
99
|
|
|
public function theResponseShouldBeEmpty() |
|
100
|
|
|
{ |
|
101
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
|
|
102
|
|
|
$message = "The response of the current page is not empty, it is: $actual"; |
|
103
|
|
|
$this->assertTrue(null === $actual || "" === $actual, $message); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Checks, whether the header name is equal to given text |
|
108
|
|
|
* |
|
109
|
|
|
* @Then the header :name should be equal to :value |
|
110
|
|
|
*/ |
|
111
|
|
|
public function theHeaderShouldBeEqualTo($name, $value) |
|
112
|
|
|
{ |
|
113
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
|
|
114
|
|
|
$this->assertEquals(strtolower($value), strtolower($actual), |
|
115
|
|
|
"The header '$name' should be equal to '$value', but it is: '$actual'" |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Checks, whether the header name is not equal to given text |
|
121
|
|
|
* |
|
122
|
|
|
* @Then the header :name should not be equal to :value |
|
123
|
|
|
*/ |
|
124
|
|
|
public function theHeaderShouldNotBeEqualTo($name, $value) { |
|
125
|
|
|
$actual = $this->getSession()->getResponseHeader($name); |
|
126
|
|
|
if (strtolower($value) == strtolower($actual)) { |
|
127
|
|
|
throw new ExpectationException( |
|
128
|
|
|
"The header '$name' is equal to '$actual'", |
|
129
|
|
|
$this->getSession()->getDriver() |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function theHeaderShouldBeContains($name, $value) |
|
135
|
|
|
{ |
|
136
|
|
|
trigger_error( |
|
137
|
|
|
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__), |
|
138
|
|
|
E_USER_DEPRECATED |
|
139
|
|
|
); |
|
140
|
|
|
$this->theHeaderShouldContain($name, $value); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Checks, whether the header name contains the given text |
|
145
|
|
|
* |
|
146
|
|
|
* @Then the header :name should contain :value |
|
147
|
|
|
*/ |
|
148
|
|
|
public function theHeaderShouldContain($name, $value) |
|
149
|
|
|
{ |
|
150
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
|
|
151
|
|
|
$this->assertContains($value, $actual, |
|
152
|
|
|
"The header '$name' should contain value '$value', but actual value is '$actual'" |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Checks, whether the header name doesn't contain the given text |
|
158
|
|
|
* |
|
159
|
|
|
* @Then the header :name should not contain :value |
|
160
|
|
|
*/ |
|
161
|
|
|
public function theHeaderShouldNotContain($name, $value) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->assertNotContains($value, $this->request->getHttpHeader($name), |
|
|
|
|
|
|
164
|
|
|
"The header '$name' contains '$value'" |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Checks, whether the header not exist |
|
170
|
|
|
* |
|
171
|
|
|
* @Then the header :name should not exist |
|
172
|
|
|
*/ |
|
173
|
|
|
public function theHeaderShouldNotExist($name) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->not(function () use($name) { |
|
176
|
|
|
$this->theHeaderShouldExist($name); |
|
177
|
|
|
}, "The header '$name' exists"); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
protected function theHeaderShouldExist($name) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->request->getHttpHeader($name); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Checks, that the response header expire is in the future |
|
187
|
|
|
* |
|
188
|
|
|
* @Then the response should expire in the future |
|
189
|
|
|
*/ |
|
190
|
|
|
public function theResponseShouldExpireInTheFuture() |
|
191
|
|
|
{ |
|
192
|
|
|
$date = new \DateTime($this->request->getHttpRawHeader('Date')[0]); |
|
|
|
|
|
|
193
|
|
|
$expires = new \DateTime($this->request->getHttpRawHeader('Expires')[0]); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
$this->assertSame(1, $expires->diff($date)->invert, |
|
196
|
|
|
sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM)) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Add an header element in a request |
|
202
|
|
|
* |
|
203
|
|
|
* @Then I add :name header equal to :value |
|
204
|
|
|
*/ |
|
205
|
|
|
public function iAddHeaderEqualTo($name, $value) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->request->setHttpHeader($name, $value); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @Then the response should be encoded in :encoding |
|
212
|
|
|
*/ |
|
213
|
|
|
public function theResponseShouldBeEncodedIn($encoding) |
|
214
|
|
|
{ |
|
215
|
|
|
$content = $this->request->getContent(); |
|
|
|
|
|
|
216
|
|
|
if (!mb_check_encoding($content, $encoding)) { |
|
217
|
|
|
throw new \Exception("The response is not encoded in $encoding"); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$this->theHeaderShouldContain('Content-Type', "charset=$encoding"); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @Then print last response headers |
|
225
|
|
|
*/ |
|
226
|
|
|
public function printLastResponseHeaders() |
|
227
|
|
|
{ |
|
228
|
|
|
$text = ''; |
|
229
|
|
|
$headers = $this->request->getHttpHeaders(); |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
foreach ($headers as $name => $value) { |
|
232
|
|
|
$text .= $name . ': '. $this->request->getHttpHeader($name) . "\n"; |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
echo $text; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @Then print the corresponding curl command |
|
240
|
|
|
*/ |
|
241
|
|
|
public function printTheCorrespondingCurlCommand() |
|
242
|
|
|
{ |
|
243
|
|
|
$method = $this->request->getMethod(); |
|
|
|
|
|
|
244
|
|
|
$url = $this->request->getUri(); |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
$headers = ''; |
|
247
|
|
|
foreach ($this->request->getServer() as $name => $value) { |
|
|
|
|
|
|
248
|
|
|
if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') { |
|
249
|
|
|
$headers .= " -H '$name: $value'"; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$data = ''; |
|
254
|
|
|
$params = $this->request->getParameters(); |
|
|
|
|
|
|
255
|
|
|
if (!empty($params)) { |
|
256
|
|
|
$query = http_build_query($params); |
|
257
|
|
|
$data = " --data '$query'" ; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
echo "curl -X $method$data$headers '$url'"; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: