1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rezzza\RestApiBehatExtension; |
4
|
|
|
|
5
|
|
|
use mageekguy\atoum\asserter; |
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
9
|
|
|
use Behat\Gherkin\Node\TableNode; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser; |
13
|
|
|
|
14
|
|
|
class RestApiContext implements Context, SnippetAcceptingContext |
15
|
|
|
{ |
16
|
|
|
private $asserter; |
17
|
|
|
|
18
|
|
|
private $restApiBrowser; |
19
|
|
|
|
20
|
|
|
public function __construct(RestApiBrowser $restApiBrowser) |
21
|
|
|
{ |
22
|
|
|
$this->restApiBrowser = $restApiBrowser; |
23
|
|
|
$this->asserter = new asserter\generator; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $method request method |
28
|
|
|
* @param string $url relative url |
29
|
|
|
* |
30
|
|
|
* @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/ |
31
|
|
|
*/ |
32
|
|
|
public function iSendARequest($method, $url) |
33
|
|
|
{ |
34
|
|
|
$this->restApiBrowser->sendRequest($method, $url); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sends HTTP request to specific URL with raw body from PyString. |
39
|
|
|
* |
40
|
|
|
* @param string $method request method |
41
|
|
|
* @param string $url relative url |
42
|
|
|
* @param PyStringNode $body |
43
|
|
|
* |
44
|
|
|
* @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/ |
45
|
|
|
*/ |
46
|
|
|
public function iSendARequestWithBody($method, $url, PyStringNode $body) |
47
|
|
|
{ |
48
|
|
|
$this->restApiBrowser->sendRequest($method, $url, (string) $body); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sends HTTP request to specific URL with POST parameters. |
53
|
|
|
* |
54
|
|
|
* @When I send a :method request to :url with form data: |
55
|
|
|
*/ |
56
|
|
|
public function iSendAPostRequestToWithFormData($method, $url, TableNode $formData) |
57
|
|
|
{ |
58
|
|
|
$this->restApiBrowser->sendRequest($method, $url, $formData->getRowsHash()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $code status code |
63
|
|
|
* |
64
|
|
|
* @Then /^(?:the )?response status code should be (\d+)$/ |
65
|
|
|
*/ |
66
|
|
|
public function theResponseCodeShouldBe($code) |
67
|
|
|
{ |
68
|
|
|
$expected = intval($code); |
69
|
|
|
$actual = intval($this->getResponse()->getStatusCode()); |
70
|
|
|
$this->asserter->variable($actual)->isEqualTo($expected); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ResponseInterface |
75
|
|
|
*/ |
76
|
|
|
private function getResponse() |
77
|
|
|
{ |
78
|
|
|
return $this->restApiBrowser->getResponse(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Given /^I set "([^"]*)" header equal to "([^"]*)"$/ |
83
|
|
|
*/ |
84
|
|
|
public function iSetHeaderEqualTo($headerName, $headerValue) |
85
|
|
|
{ |
86
|
|
|
$this->restApiBrowser->setRequestHeader($headerName, $headerValue); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @Given /^I add "([^"]*)" header equal to "([^"]*)"$/ |
91
|
|
|
*/ |
92
|
|
|
public function iAddHeaderEqualTo($headerName, $headerValue) |
93
|
|
|
{ |
94
|
|
|
$this->restApiBrowser->addRequestHeader($headerName, $headerValue); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set login / password for next HTTP authentication |
99
|
|
|
* |
100
|
|
|
* @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/ |
101
|
|
|
*/ |
102
|
|
|
public function iSetBasicAuthenticationWithAnd($username, $password) |
103
|
|
|
{ |
104
|
|
|
$authorization = base64_encode($username . ':' . $password); |
105
|
|
|
$this->restApiBrowser->setRequestHeader('Authorization', 'Basic ' . $authorization); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Then print request and response |
110
|
|
|
*/ |
111
|
|
|
public function printRequestAndResponse() |
112
|
|
|
{ |
113
|
|
|
echo "REQUEST:\n"; |
114
|
|
|
$this->printRequest(); |
115
|
|
|
echo "\nRESPONSE:\n"; |
116
|
|
|
$this->printResponse(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @Then print request |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function printRequest() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$request = $this->getRequest(); |
125
|
|
|
echo sprintf( |
126
|
|
|
"%s %s :\n%s%s\n", |
127
|
|
|
$request->getMethod(), |
128
|
|
|
$request->getUri(), |
129
|
|
|
$this->getRawHeaders($request->getHeaders()), |
130
|
|
|
$request->getBody() |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return RequestInterface |
136
|
|
|
*/ |
137
|
|
|
private function getRequest() |
138
|
|
|
{ |
139
|
|
|
return $this->restApiBrowser->getRequest(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $headers |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
private function getRawHeaders(array $headers) |
147
|
|
|
{ |
148
|
|
|
$rawHeaders = ''; |
149
|
|
|
foreach ($headers as $key => $value) { |
150
|
|
|
$rawHeaders .= sprintf("%s: %s\n", $key, is_array($value) ? implode(", ", $value) : $value); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
$rawHeaders .= "\n"; |
154
|
|
|
return $rawHeaders; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @Then print response |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function printResponse() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$request = $this->getRequest(); |
163
|
|
|
$response = $this->getResponse(); |
164
|
|
|
|
165
|
|
|
echo sprintf( |
166
|
|
|
"%s %s :\n%s%s\n", |
167
|
|
|
$request->getMethod(), |
168
|
|
|
$request->getUri()->__toString(), |
169
|
|
|
$this->getRawHeaders($response->getHeaders()), |
170
|
|
|
$response->getBody() |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.