1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch\Context; |
4
|
|
|
|
5
|
|
|
use Behatch\HttpCall\Request; |
6
|
|
|
use Behat\Gherkin\Node\TableNode; |
7
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
8
|
|
|
|
9
|
|
|
class RestContext extends BaseContext |
10
|
|
|
{ |
11
|
|
|
protected $request; |
12
|
|
|
|
13
|
|
|
public function __construct(Request $request) |
14
|
|
|
{ |
15
|
|
|
$this->request = $request; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Sends a HTTP request |
20
|
|
|
* |
21
|
|
|
* @Given I send a :method request to :url |
22
|
|
|
*/ |
23
|
|
|
public function iSendARequestTo($method, $url, PyStringNode $body = null, $files = []) |
24
|
|
|
{ |
25
|
|
|
return $this->request->send( |
|
|
|
|
26
|
|
|
$method, |
27
|
|
|
$this->locatePath($url), |
28
|
|
|
[], |
29
|
|
|
$files, |
30
|
|
|
$body !== null ? $body->getRaw() : null |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sends a HTTP request with a some parameters |
36
|
|
|
* |
37
|
|
|
* @Given I send a :method request to :url with parameters: |
38
|
|
|
*/ |
39
|
|
|
public function iSendARequestToWithParameters($method, $url, TableNode $datas) |
40
|
|
|
{ |
41
|
|
|
$files = []; |
42
|
|
|
$parameters = []; |
43
|
|
|
|
44
|
|
|
foreach ($datas->getHash() as $row) { |
45
|
|
|
if (!isset($row['key']) || !isset($row['value'])) { |
46
|
|
|
throw new \Exception("You must provide a 'key' and 'value' column in your table node."); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (is_string($row['value']) && substr($row['value'], 0, 1) == '@') { |
50
|
|
|
$files[$row['key']] = rtrim($this->getMinkParameter('files_path'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.substr($row['value'],1); |
51
|
|
|
} |
52
|
|
|
else { |
53
|
|
|
$parameters[] = sprintf('%s=%s', $row['key'], $row['value']); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
parse_str(implode('&', $parameters), $parameters); |
58
|
|
|
|
59
|
|
|
return $this->request->send( |
|
|
|
|
60
|
|
|
$method, |
61
|
|
|
$this->locatePath($url), |
62
|
|
|
$parameters, |
63
|
|
|
$files |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sends a HTTP request with a body |
69
|
|
|
* |
70
|
|
|
* @Given I send a :method request to :url with body: |
71
|
|
|
*/ |
72
|
|
|
public function iSendARequestToWithBody($method, $url, PyStringNode $body) |
73
|
|
|
{ |
74
|
|
|
return $this->iSendARequestTo($method, $url, $body); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Checks, whether the response content is equal to given text |
79
|
|
|
* |
80
|
|
|
* @Then the response should be equal to |
81
|
|
|
*/ |
82
|
|
|
public function theResponseShouldBeEqualTo(PyStringNode $expected) |
83
|
|
|
{ |
84
|
|
|
$expected = str_replace('\\"', '"', $expected); |
85
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
86
|
|
|
$message = "The string '$expected' is not equal to the response of the current page"; |
87
|
|
|
$this->assertEquals($expected, $actual, $message); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks, whether the response content is null or empty string |
92
|
|
|
* |
93
|
|
|
* @Then the response should be empty |
94
|
|
|
*/ |
95
|
|
|
public function theResponseShouldBeEmpty() |
96
|
|
|
{ |
97
|
|
|
$actual = $this->request->getContent(); |
|
|
|
|
98
|
|
|
$message = 'The response of the current page is not empty'; |
99
|
|
|
$this->assertTrue(null === $actual || "" === $actual, $message); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks, whether the header name is equal to given text |
104
|
|
|
* |
105
|
|
|
* @Then the header :name should be equal to :value |
106
|
|
|
*/ |
107
|
|
|
public function theHeaderShouldBeEqualTo($name, $value) |
108
|
|
|
{ |
109
|
|
|
$actual = $this->request->getHttpHeader($name); |
|
|
|
|
110
|
|
|
$this->assertEquals(strtolower($value), strtolower($actual), |
111
|
|
|
"The header '$name' is equal to '$actual'" |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks, whether the header name contains the given text |
117
|
|
|
* |
118
|
|
|
* @Then the header :name should contain :value |
119
|
|
|
*/ |
120
|
|
|
public function theHeaderShouldBeContains($name, $value) |
121
|
|
|
{ |
122
|
|
|
$this->assertContains($value, $this->request->getHttpHeader($name), |
|
|
|
|
123
|
|
|
"The header '$name' doesn't contain '$value'" |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks, whether the header name doesn't contain the given text |
129
|
|
|
* |
130
|
|
|
* @Then the header :name should not contain :value |
131
|
|
|
*/ |
132
|
|
|
public function theHeaderShouldNotContain($name, $value) |
133
|
|
|
{ |
134
|
|
|
$this->assertNotContains($value, $this->request->getHttpHeader($name), |
|
|
|
|
135
|
|
|
"The header '$name' contains '$value'" |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks, whether the header not exist |
141
|
|
|
* |
142
|
|
|
* @Then the header :name should not exist |
143
|
|
|
*/ |
144
|
|
|
public function theHeaderShouldNotExist($name) |
145
|
|
|
{ |
146
|
|
|
$this->not(function () use($name) { |
147
|
|
|
$this->theHeaderShouldExist($name); |
148
|
|
|
}, "The header '$name' exists"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function theHeaderShouldExist($name) |
152
|
|
|
{ |
153
|
|
|
return $this->request->getHttpHeader($name); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Checks, that the response header expire is in the future |
158
|
|
|
* |
159
|
|
|
* @Then the response should expire in the future |
160
|
|
|
*/ |
161
|
|
|
public function theResponseShouldExpireInTheFuture() |
162
|
|
|
{ |
163
|
|
|
$date = new \DateTime($this->request->getHttpHeader('Date')); |
|
|
|
|
164
|
|
|
$expires = new \DateTime($this->request->getHttpHeader('Expires')); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$this->assertSame(1, $expires->diff($date)->invert, |
167
|
|
|
sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM)) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add an header element in a request |
173
|
|
|
* |
174
|
|
|
* @Then I add :name header equal to :value |
175
|
|
|
*/ |
176
|
|
|
public function iAddHeaderEqualTo($name, $value) |
177
|
|
|
{ |
178
|
|
|
$this->request->setHttpHeader($name, $value); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @Then the response should be encoded in :encoding |
183
|
|
|
*/ |
184
|
|
|
public function theResponseShouldBeEncodedIn($encoding) |
185
|
|
|
{ |
186
|
|
|
$content = $this->request->getContent(); |
|
|
|
|
187
|
|
|
if (!mb_check_encoding($content, $encoding)) { |
188
|
|
|
throw new \Exception("The response is not encoded in $encoding"); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->theHeaderShouldBeContains('Content-Type', "charset=$encoding"); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @Then print last response headers |
196
|
|
|
*/ |
197
|
|
|
public function printLastResponseHeaders() |
198
|
|
|
{ |
199
|
|
|
$text = ''; |
200
|
|
|
$headers = $this->request->getHttpHeaders(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
foreach ($headers as $name => $value) { |
203
|
|
|
$text .= $name . ': '. $this->request->getHttpHeader($name) . "\n"; |
|
|
|
|
204
|
|
|
} |
205
|
|
|
echo $text; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @Then print the corresponding curl command |
211
|
|
|
*/ |
212
|
|
|
public function printTheCorrespondingCurlCommand() |
213
|
|
|
{ |
214
|
|
|
$method = $this->request->getMethod(); |
|
|
|
|
215
|
|
|
$url = $this->request->getUri(); |
|
|
|
|
216
|
|
|
|
217
|
|
|
$headers = ''; |
218
|
|
|
foreach ($this->request->getServer() as $name => $value) { |
|
|
|
|
219
|
|
|
if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') { |
220
|
|
|
$headers .= " -H '$name: $value'"; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$data = ''; |
225
|
|
|
$params = $this->request->getParameters(); |
|
|
|
|
226
|
|
|
if (!empty($params)) { |
227
|
|
|
$query = http_build_query($params); |
228
|
|
|
$data = " --data '$query'" ; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
echo "curl -X $method$data$headers '$url'"; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
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: