Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"require": {
"php": ">=8.1 <8.4",
"ext-json": "*",
"byjg/webrequest": "^5.0"
"byjg/webrequest": "^5.0",
"byjg/xmlutil": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
Expand Down
24 changes: 16 additions & 8 deletions src/AbstractRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ByJG\WebRequest\Exception\RequestException;
use ByJG\WebRequest\Psr7\MemoryStream;
use ByJG\WebRequest\Psr7\Request;
use ByJG\XmlUtil\XmlDocument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

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

// Prepare Body to Match Against Specification
$requestBody = $this->psr7Request->getBody()->getContents();
if (!empty($requestBody)) {
$contentType = $this->psr7Request->getHeaderLine("content-type");
if (empty($contentType) || str_contains($contentType, "application/json")) {
$requestBody = json_decode($requestBody, true);
$rawBody = $this->psr7Request->getBody()->getContents();
$isXmlBody = false;
$requestBody = null;
$contentType = $this->psr7Request->getHeaderLine("content-type");
if (!empty($rawBody)) {
if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) {
$isXmlBody = new XmlDocument($rawBody);
} elseif (empty($contentType) || str_contains($contentType, "application/json")) {
$requestBody = json_decode($rawBody, true);
} elseif (str_contains($contentType, "multipart/")) {
$requestBody = $this->parseMultiPartForm($contentType, $requestBody);
$requestBody = $this->parseMultiPartForm($contentType, $rawBody);
} else {
throw new InvalidRequestException("Cannot handle Content Type '$contentType'");
}

}

// Check if the body is the expected before request
$bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod(), $matchQueryParams ? $this->psr7Request->getUri()->getQuery() : null);
$bodyRequestDef->match($requestBody);
if ($isXmlBody === false) {
$bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod(), $matchQueryParams ? $this->psr7Request->getUri()->getQuery() : null);
$bodyRequestDef->match($requestBody);
}

// Handle Request
$response = $this->handleRequest($this->psr7Request);
Expand Down
39 changes: 39 additions & 0 deletions tests/AbstractRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,45 @@ public function testValidateAssertBodyNotContains()
$this->assertRequest($request);
}

/**
* @throws DefinitionNotFoundException
* @throws GenericSwaggerException
* @throws HttpMethodNotFoundException
* @throws InvalidDefinitionException
* @throws InvalidRequestException
* @throws NotMatchedException
* @throws PathNotFoundException
* @throws RequiredArgumentNotFound
* @throws StatusCodeNotMatchedException
* @throws MessageException
* @throws RequestException
*/
public function testPostPetWithXmlBody()
{
$xmlBody =
'<Pet id="99">' .
'<name>Garfield XML</name>' .
'<status>available</status>' .
'<photoUrls>' .
'<photoUrl>http://example.com/garfield.png</photoUrl>' .
'</photoUrls>' .
'<category id="1">' .
'<name>Cats</name>' .
'</category>' .
'</Pet>';

$expectedResponse = Response::getInstance(200);

$request = new MockRequester($expectedResponse);
$request
->withMethod('POST')
->withPath('/pet')
->withRequestHeader(['Content-Type' => 'application/xml'])
->withRequestBody($xmlBody);

$this->assertRequest($request);
}

public function testMatchParameterInQueryAssert()
{
$expectedResponse = Response::getInstance(200)
Expand Down