-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support http request binary body
- Loading branch information
Showing
44 changed files
with
521 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Service; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
|
||
/** | ||
* Example HTTP Service | ||
*/ | ||
class HttpClientService | ||
{ | ||
private Client $httpClient; | ||
|
||
private string $baseUri; | ||
|
||
public function __construct(string $baseUri) | ||
{ | ||
$this->httpClient = new Client(); | ||
$this->baseUri = $baseUri; | ||
} | ||
|
||
public function getImageContent(): string | ||
{ | ||
$response = $this->httpClient->get(new Uri("{$this->baseUri}/image.jpg"), [ | ||
'headers' => ['Accept' => 'image/jpeg'] | ||
]); | ||
|
||
return $response->getBody(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
example/binary/consumer/tests/Service/HttpClientServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Tests\Service; | ||
|
||
use BinaryConsumer\Service\HttpClientService; | ||
use PhpPact\Consumer\InteractionBuilder; | ||
use PhpPact\Consumer\Model\Body\Binary; | ||
use PhpPact\Consumer\Model\ConsumerRequest; | ||
use PhpPact\Consumer\Model\ProviderResponse; | ||
use PhpPact\Standalone\MockService\MockServerConfig; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HttpClientServiceTest extends TestCase | ||
{ | ||
public function testGetImageContent() | ||
{ | ||
$imageContent = file_get_contents(__DIR__ . '/../_resource/image.jpg'); | ||
|
||
$request = new ConsumerRequest(); | ||
$request | ||
->setMethod('GET') | ||
->setPath('/image.jpg') | ||
->addHeader('Accept', 'image/jpeg'); | ||
|
||
$response = new ProviderResponse(); | ||
$response | ||
->setStatus(200) | ||
->addHeader('Content-Type', 'image/jpeg') | ||
->setBody(new Binary($imageContent, PHP_OS_FAMILY === 'Windows' ? 'application/octet-stream' : 'image/jpeg')); | ||
|
||
$config = new MockServerConfig(); | ||
$config | ||
->setConsumer('binaryConsumer') | ||
->setProvider('binaryProvider') | ||
->setPactDir(__DIR__.'/../../../pacts'); | ||
if ($logLevel = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($logLevel); | ||
} | ||
$builder = new InteractionBuilder($config); | ||
$builder | ||
->given('Image file image.jpg exists') | ||
->uponReceiving('A get request to /image.jpg') | ||
->with($request) | ||
->willRespondWith($response); | ||
|
||
$service = new HttpClientService($config->getBaseUri()); | ||
$imageContentResult = $service->getImageContent(); | ||
$verifyResult = $builder->verify(); | ||
|
||
$this->assertTrue($verifyResult); | ||
$this->assertEquals($imageContent, $imageContentResult); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"consumer": { | ||
"name": "binaryConsumer" | ||
}, | ||
"interactions": [ | ||
{ | ||
"description": "A get request to /image.jpg", | ||
"providerStates": [ | ||
{ | ||
"name": "Image file image.jpg exists" | ||
} | ||
], | ||
"request": { | ||
"headers": { | ||
"Accept": "image/jpeg" | ||
}, | ||
"method": "GET", | ||
"path": "/image.jpg" | ||
}, | ||
"response": { | ||
"body": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAPAA8BAREA/8QAFgABAQEAAAAAAAAAAAAAAAAAAgME/8QAIBAAAgIDAAIDAQAAAAAAAAAAAQIDBAUREhMhACIxUf/aAAgBAQAAPwBNlclNW6S0s7wbvv4K6iNI2HJcp9Rrnk+T0ByNofwZIjdnyYx0stWrYWAP5y03SBjt+GQ/YnmHbaUkfoA107Br40Wa+DrRLNXhjchECDwyjjlXPpdFSo3Gw5CbDFdmUVOrHcfEU4JYb2PWSJCzbVPUEmh7PQaOZHOyD0x/hHz/2Q==", | ||
"headers": { | ||
"Content-Type": "image/jpeg" | ||
}, | ||
"matchingRules": { | ||
"body": { | ||
"$": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "contentType", | ||
"value": "image/jpeg" | ||
} | ||
] | ||
} | ||
}, | ||
"header": {} | ||
}, | ||
"status": 200 | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"pactRust": { | ||
"ffi": "0.4.7", | ||
"mockserver": "1.2.3", | ||
"models": "1.1.9" | ||
}, | ||
"pactSpecification": { | ||
"version": "3.0.0" | ||
} | ||
}, | ||
"provider": { | ||
"name": "binaryProvider" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Tests; | ||
|
||
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig; | ||
use PhpPact\Standalone\ProviderVerifier\Verifier; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Process\Process; | ||
|
||
class PactVerifyTest extends TestCase | ||
{ | ||
private Process $process; | ||
|
||
/** | ||
* Run the PHP build-in web server. | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->process = new Process(['php', '-S', '127.0.0.1:7202', '-t', __DIR__ . '/../public/']); | ||
|
||
$this->process->start(); | ||
$this->process->waitUntil(function (): bool { | ||
$fp = @fsockopen('127.0.0.1', 7202); | ||
$isOpen = is_resource($fp); | ||
if ($isOpen) { | ||
fclose($fp); | ||
} | ||
|
||
return $isOpen; | ||
}); | ||
} | ||
|
||
/** | ||
* Stop the web server process once complete. | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
$this->process->stop(); | ||
} | ||
|
||
/** | ||
* This test will run after the web server is started. | ||
*/ | ||
public function testPactVerifyConsumer() | ||
{ | ||
$config = new VerifierConfig(); | ||
$config->getProviderInfo() | ||
->setName('binaryProvider') // Providers name to fetch. | ||
->setHost('localhost') | ||
->setPort(7202); | ||
if ($level = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($level); | ||
} | ||
|
||
$verifier = new Verifier($config); | ||
$verifier->addFile(__DIR__ . '/../../pacts/binaryConsumer-binaryProvider.json'); | ||
|
||
$verifyResult = $verifier->verify(); | ||
|
||
$this->assertTrue($verifyResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/PhpPact/Consumer/Exception/MessageContentsNotAddedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Exception; | ||
|
||
use Exception; | ||
|
||
class MessageContentsNotAddedException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Model\Body; | ||
|
||
use PhpPact\FFI\Model\StringData; | ||
|
||
class Binary | ||
{ | ||
use ContentTypeTrait; | ||
|
||
private StringData $contents; | ||
|
||
public function __construct(string $contents, string $contentType) | ||
{ | ||
$this->setContents(StringData::createFrom($contents, false)); | ||
$this->setContentType($contentType); | ||
} | ||
|
||
public function getContents(): StringData | ||
{ | ||
return $this->contents; | ||
} | ||
|
||
public function setContents(StringData $contents): self | ||
{ | ||
$this->contents = $contents; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Model\Body; | ||
|
||
trait ContentTypeTrait | ||
{ | ||
private string $contentType; | ||
|
||
public function getContentType(): string | ||
{ | ||
return $this->contentType; | ||
} | ||
|
||
public function setContentType(string $contentType): self | ||
{ | ||
$this->contentType = $contentType; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Model\Body; | ||
|
||
class Text | ||
{ | ||
use ContentTypeTrait; | ||
|
||
private string $contents; | ||
|
||
public function __construct(string $contents, string $contentType) | ||
{ | ||
$this->setContents($contents); | ||
$this->setContentType($contentType); | ||
} | ||
|
||
public function getContents(): string | ||
{ | ||
return $this->contents; | ||
} | ||
|
||
public function setContents(string $contents): self | ||
{ | ||
$this->contents = $contents; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.