Skip to content

Commit 9cfff66

Browse files
authored
Merge pull request #85 from leonardomunsa/feature/xml
XML body requests
2 parents a21e9a0 + f8b3668 commit 9cfff66

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"require": {
88
"php": ">=8.1 <8.4",
99
"ext-json": "*",
10-
"byjg/webrequest": "^5.0"
10+
"byjg/webrequest": "^5.0",
11+
"byjg/xmlutil": "^5.0"
1112
},
1213
"require-dev": {
1314
"phpunit/phpunit": "^9.6",

src/AbstractRequester.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ByJG\WebRequest\Exception\RequestException;
1818
use ByJG\WebRequest\Psr7\MemoryStream;
1919
use ByJG\WebRequest\Psr7\Request;
20+
use ByJG\XmlUtil\XmlDocument;
2021
use Psr\Http\Message\RequestInterface;
2122
use Psr\Http\Message\ResponseInterface;
2223

@@ -235,21 +236,28 @@ public function send(bool $matchQueryParams = true): ResponseInterface
235236
$this->psr7Request = $this->psr7Request->withUri($uri);
236237

237238
// 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);
243248
} elseif (str_contains($contentType, "multipart/")) {
244-
$requestBody = $this->parseMultiPartForm($contentType, $requestBody);
249+
$requestBody = $this->parseMultiPartForm($contentType, $rawBody);
245250
} else {
246251
throw new InvalidRequestException("Cannot handle Content Type '$contentType'");
247252
}
253+
248254
}
249255

250256
// 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+
}
253261

254262
// Handle Request
255263
$response = $this->handleRequest($this->psr7Request);

tests/AbstractRequesterTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,45 @@ public function testValidateAssertBodyNotContains()
339339
$this->assertRequest($request);
340340
}
341341

342+
/**
343+
* @throws DefinitionNotFoundException
344+
* @throws GenericSwaggerException
345+
* @throws HttpMethodNotFoundException
346+
* @throws InvalidDefinitionException
347+
* @throws InvalidRequestException
348+
* @throws NotMatchedException
349+
* @throws PathNotFoundException
350+
* @throws RequiredArgumentNotFound
351+
* @throws StatusCodeNotMatchedException
352+
* @throws MessageException
353+
* @throws RequestException
354+
*/
355+
public function testPostPetWithXmlBody()
356+
{
357+
$xmlBody =
358+
'<Pet id="99">' .
359+
'<name>Garfield XML</name>' .
360+
'<status>available</status>' .
361+
'<photoUrls>' .
362+
'<photoUrl>http://example.com/garfield.png</photoUrl>' .
363+
'</photoUrls>' .
364+
'<category id="1">' .
365+
'<name>Cats</name>' .
366+
'</category>' .
367+
'</Pet>';
368+
369+
$expectedResponse = Response::getInstance(200);
370+
371+
$request = new MockRequester($expectedResponse);
372+
$request
373+
->withMethod('POST')
374+
->withPath('/pet')
375+
->withRequestHeader(['Content-Type' => 'application/xml'])
376+
->withRequestBody($xmlBody);
377+
378+
$this->assertRequest($request);
379+
}
380+
342381
public function testMatchParameterInQueryAssert()
343382
{
344383
$expectedResponse = Response::getInstance(200)

0 commit comments

Comments
 (0)