|
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
|
|
|
* @param string $code status code |
|
53
|
|
|
* |
|
54
|
|
|
* @Then /^(?:the )?response status code should be (\d+)$/ |
|
55
|
|
|
*/ |
|
56
|
|
|
public function theResponseCodeShouldBe($code) |
|
57
|
|
|
{ |
|
58
|
|
|
$expected = intval($code); |
|
59
|
|
|
$actual = intval($this->getResponse()->getStatusCode()); |
|
60
|
|
|
$this->asserter->variable($actual)->isEqualTo($expected); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return ResponseInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getResponse() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->restApiBrowser->getResponse(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @Given /^I set "([^"]*)" header equal to "([^"]*)"$/ |
|
73
|
|
|
*/ |
|
74
|
|
|
public function iSetHeaderEqualTo($headerName, $headerValue) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->restApiBrowser->setRequestHeader($headerName, $headerValue); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @Given /^I add "([^"]*)" header equal to "([^"]*)"$/ |
|
81
|
|
|
*/ |
|
82
|
|
|
public function iAddHeaderEqualTo($headerName, $headerValue) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->restApiBrowser->addRequestHeader($headerName, $headerValue); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set login / password for next HTTP authentication |
|
89
|
|
|
* |
|
90
|
|
|
* @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/ |
|
91
|
|
|
*/ |
|
92
|
|
|
public function iSetBasicAuthenticationWithAnd($username, $password) |
|
93
|
|
|
{ |
|
94
|
|
|
$authorization = base64_encode($username . ':' . $password); |
|
95
|
|
|
$this->restApiBrowser->setRequestHeader('Authorization', 'Basic ' . $authorization); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @Then print request and response |
|
100
|
|
|
*/ |
|
101
|
|
|
public function printRequestAndResponse() |
|
102
|
|
|
{ |
|
103
|
|
|
echo "REQUEST:\n"; |
|
104
|
|
|
$this->printRequest(); |
|
105
|
|
|
echo "\nRESPONSE:\n"; |
|
106
|
|
|
$this->printResponse(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @Then print request |
|
111
|
|
|
*/ |
|
112
|
|
|
public function printRequest() |
|
113
|
|
|
{ |
|
114
|
|
|
$request = $this->getRequest(); |
|
115
|
|
|
echo sprintf( |
|
116
|
|
|
"%s %s :\n%s%s\n", |
|
117
|
|
|
$request->getMethod(), |
|
118
|
|
|
$request->getUri(), |
|
119
|
|
|
$this->getRawHeaders($request->getHeaders()), |
|
120
|
|
|
$request->getBody() |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return RequestInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getRequest() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->restApiBrowser->getRequest(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $headers |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getRawHeaders(array $headers) |
|
137
|
|
|
{ |
|
138
|
|
|
$rawHeaders = ''; |
|
139
|
|
|
foreach ($headers as $key => $value) { |
|
140
|
|
|
$rawHeaders .= sprintf("%s: %s\n", $key, is_array($value) ? implode(", ", $value) : $value); |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
$rawHeaders .= "\n"; |
|
144
|
|
|
return $rawHeaders; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @Then print response |
|
149
|
|
|
*/ |
|
150
|
|
|
public function printResponse() |
|
151
|
|
|
{ |
|
152
|
|
|
$request = $this->getRequest(); |
|
153
|
|
|
$response = $this->getResponse(); |
|
154
|
|
|
|
|
155
|
|
|
echo sprintf( |
|
156
|
|
|
"%s %s :\n%s %s\n%s%s\n", |
|
157
|
|
|
$request->getMethod(), |
|
158
|
|
|
$request->getUri()->__toString(), |
|
159
|
|
|
$response->getStatusCode(), |
|
160
|
|
|
$response->getReasonPhrase(), |
|
161
|
|
|
$this->getRawHeaders($response->getHeaders()), |
|
162
|
|
|
$response->getBody() |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|