forked from modethirteen/HyperPlug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockRequestMatcher.php
277 lines (246 loc) · 7.27 KB
/
MockRequestMatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php declare(strict_types=1);
/**
* HyperPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace modethirteen\Http\Mock;
use InvalidArgumentException;
use modethirteen\Http\Content\IContent;
use modethirteen\Http\Headers;
use modethirteen\Http\IHeaders;
use modethirteen\Http\IMutableHeaders;
use modethirteen\Http\XUri;
/**
* Class MockRequest
*
* Object for configuring a MockPlug request to mock or verify; file uploads not supported
*
* @package modethirteen\Http\Mock
*/
class MockRequestMatcher {
/**
* @var string[]
*/
private static array $ignoredQueryParamNames = [];
/**
* @var string[]
*/
private static array $ignoredHeaderNames = [];
/**
* Set query param names to ignore during matching
*
* @param string[] $names
* @return void
*/
public static function setIgnoredQueryParamNames(array $names) : void {
self::$ignoredQueryParamNames = $names;
}
/**
* Set HTTP header names to ignore during matching
*
* @param string[] $names
* @return void
*/
public static function setIgnoredHeaderNames(array $names) : void {
self::$ignoredHeaderNames = $names;
}
/**
* @var string
*/
private string $method;
/**
* @var XUri
*/
private XUri $uri;
/**
* @var IMutableHeaders
*/
private IMutableHeaders $headers;
/**
* @var string|null
*/
private ?string $body = null;
/**
* @param string $method
* @param XUri $uri
*/
public function __construct(string $method, XUri $uri) {
$this->method = $method;
$this->uri = $uri;
$this->headers = new Headers();
}
public function __clone() {
// deep copy internal data objects and arrays
$this->uri = unserialize(serialize($this->uri));
$this->headers = unserialize(serialize($this->headers));
}
/**
* Retrieve HTTP method
*
* @return string
*/
public function getMethod() : string {
return $this->method;
}
/**
* Retrieve denormalized matcher uri
*
* @return XUri
*/
public function getUri() : XUri {
return $this->uri;
}
/**
* Retrieve HTTP headers
*
* @return IHeaders
*/
public function getHeaders() : IHeaders {
return $this->headers;
}
/**
* Retrieve HTTP message body
*
* @return string|null
*/
public function getBody() : ?string {
return $this->body;
}
/**
* Retrieve id to match mock results to matcher
*
* @return string
*/
public function getMatcherId() : string {
$uri = $this->newNormalizedUriString();
$headers = $this->newNormalizedHeaderStrings();
return md5(serialize($headers) . "{$this->method}{$uri}{$this->body}");
}
/**
* Return an instance with the specified HTTP headers.
*
* @param IHeaders $headers
* @return MockRequestMatcher
*/
public function withHeaders(IHeaders $headers) : MockRequestMatcher {
$request = clone $this;
$request->headers = $headers->toMutableHeaders();
return $request;
}
/**
* Return an instance with the specified HTTP headers added to existing mocked HTTP headers.
*
* @param IHeaders $headers
* @return MockRequestMatcher
*/
public function withAddedHeaders(IHeaders $headers) : MockRequestMatcher {
$request = clone $this;
$request->headers->addHeaders($headers);
return $request;
}
/**
* Return an instance with the specified body string
*
* @param string|string[]|null $body - array body is assumed to be form fields and will be encoded to a string
* @return MockRequestMatcher
*/
public function withBody($body) : MockRequestMatcher {
if(is_string($body) || is_array($body) || $body == null) {
if(is_array($body)) {
$body = http_build_query($body);
}
$request = clone $this;
$request->body = $body;
return $request;
}
throw new InvalidArgumentException('Body value must be string, array, or null');
}
/**
* Return an instance with the specified content. Method will set a body and content-type depending on
* the value of the content object
*
* @param IContent $content
* @return MockRequestMatcher
*/
public function withContent(IContent $content) : MockRequestMatcher {
$request = clone $this;
$contentType = $content->getContentType();
$request->headers->setHeader(Headers::HEADER_CONTENT_TYPE, $contentType !== null ? $contentType->toString() : null);
$request->body = $content->toString();
return $request;
}
/**
* @return array
*/
public function toArray() : array {
return [
'method' => $this->method,
'uri' => $this->uri->toString(),
'headers' => $this->headers->toFlattenedArray(),
'body' => $this->body !== null ? $this->body : ''
];
}
/**
* @return array
*/
public function toNormalizedArray() : array {
return [
'method' => $this->method,
'uri' => $this->newNormalizedUriString(),
'headers' => $this->newNormalizedHeaderStrings(),
'body' => $this->body !== null ? $this->body : ''
];
}
/**
* @return string
*/
private function newNormalizedUriString() : string {
$params = [];
$href = $this->uri->toString();
// parse uri into components
$data = parse_url($href);
if(!is_array($data) || !isset($data['scheme']) || !isset($data['host'])) {
// if for some outstanding reason, the uri is malformed, at least match on something
return $href;
}
if(isset($data['query'])) {
parse_str($data['query'], $params);
}
// filter parameters applied by Plug
$params = array_diff_key($params, array_flip(self::$ignoredQueryParamNames));
// rebuild uri
$uri = $data['scheme'] . '://' . $data['host'];
if(isset($data['port'])) {
$uri .= ':' . $data['port'];
}
if(isset($data['path'])) {
$uri .= $data['path'];
}
asort($params);
if(!empty($params)) {
$uri .= '?' . http_build_query($params);
}
return $uri;
}
/**
* @return string[]
*/
private function newNormalizedHeaderStrings() : array {
$headers = $this->headers->toFlattenedArray();
$headers = array_diff_key($headers, array_flip(self::$ignoredHeaderNames));
// rebuild headers
ksort($headers);
return $headers;
}
}