|
17 | 17 | use ByJG\WebRequest\Exception\RequestException; |
18 | 18 | use ByJG\WebRequest\Psr7\MemoryStream; |
19 | 19 | use ByJG\WebRequest\Psr7\Request; |
| 20 | +use ByJG\XmlUtil\XmlDocument; |
20 | 21 | use Psr\Http\Message\RequestInterface; |
21 | 22 | use Psr\Http\Message\ResponseInterface; |
22 | 23 |
|
@@ -235,21 +236,28 @@ public function send(bool $matchQueryParams = true): ResponseInterface |
235 | 236 | $this->psr7Request = $this->psr7Request->withUri($uri); |
236 | 237 |
|
237 | 238 | // Prepare Body to Match Against Specification |
238 | | - $requestBody = $this->psr7Request->getBody()->getContents(); |
239 | | - if (!empty($requestBody)) { |
240 | | - $contentType = $this->psr7Request->getHeaderLine("content-type"); |
241 | | - if (empty($contentType) || str_contains($contentType, "application/json")) { |
242 | | - $requestBody = json_decode($requestBody, true); |
| 239 | + $rawBody = $this->psr7Request->getBody()->getContents(); |
| 240 | + $isXmlBody = false; |
| 241 | + $requestBody = null; |
| 242 | + $contentType = $this->psr7Request->getHeaderLine("content-type"); |
| 243 | + if (!empty($rawBody)) { |
| 244 | + if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { |
| 245 | + $isXmlBody = new XmlDocument($rawBody); |
| 246 | + } elseif (empty($contentType) || str_contains($contentType, "application/json")) { |
| 247 | + $requestBody = json_decode($rawBody, true); |
243 | 248 | } elseif (str_contains($contentType, "multipart/")) { |
244 | | - $requestBody = $this->parseMultiPartForm($contentType, $requestBody); |
| 249 | + $requestBody = $this->parseMultiPartForm($contentType, $rawBody); |
245 | 250 | } else { |
246 | 251 | throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); |
247 | 252 | } |
| 253 | + |
248 | 254 | } |
249 | 255 |
|
250 | 256 | // Check if the body is the expected before request |
251 | | - $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod(), $matchQueryParams ? $this->psr7Request->getUri()->getQuery() : null); |
252 | | - $bodyRequestDef->match($requestBody); |
| 257 | + if ($isXmlBody === false) { |
| 258 | + $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod(), $matchQueryParams ? $this->psr7Request->getUri()->getQuery() : null); |
| 259 | + $bodyRequestDef->match($requestBody); |
| 260 | + } |
253 | 261 |
|
254 | 262 | // Handle Request |
255 | 263 | $response = $this->handleRequest($this->psr7Request); |
|
0 commit comments