Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart static files #342

Merged
merged 3 commits into from
Oct 12, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
extensions: sockets, curl, zip, ffi
php-version: ${{ matrix.php }}
coverage: none
ini-values: ${{ matrix.operating-system == 'windows-latest' && 'opcache.enable=0 opcache.enable_cli=0' || '' }}

- name: Composer install
uses: ramsey/composer-install@v2
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@
"extra": {
"downloads": {
"pact-ffi-headers": {
"version": "0.4.8",
"version": "0.4.9",
"url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h",
"path": "bin/pact-ffi-headers/pact.h"
},
"pact-ffi-lib": {
"version": "0.4.8",
"version": "0.4.9",
"variables": {
"{$prefix}": "PHP_OS_FAMILY === 'Windows' ? 'pact_ffi' : 'libpact_ffi'",
"{$os}": "PHP_OS === 'Darwin' ? 'osx' : strtolower(PHP_OS_FAMILY)",
Expand Down
6 changes: 3 additions & 3 deletions example/binary/pacts/binaryConsumer-binaryProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.7",
"mockserver": "1.2.3",
"models": "1.1.9"
"ffi": "0.4.9",
"mockserver": "1.2.4",
"models": "1.1.11"
},
"pactSpecification": {
"version": "3.0.0"
Expand Down
6 changes: 3 additions & 3 deletions example/json/pacts/jsonConsumer-jsonProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.7",
"mockserver": "1.2.3",
"models": "1.1.9"
"ffi": "0.4.9",
"mockserver": "1.2.4",
"models": "1.1.11"
},
"pactSpecification": {
"version": "3.0.0"
Expand Down
4 changes: 2 additions & 2 deletions example/message/pacts/messageConsumer-messageProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.7",
"models": "1.1.9"
"ffi": "0.4.9",
"models": "1.1.11"
},
"pactSpecification": {
"version": "3.0.0"
Expand Down
7 changes: 0 additions & 7 deletions example/multipart/consumer/src/Service/HttpClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public function updateUserProfile(): string
'name' => 'full_name',
'contents' => 'Zoey Turcotte',
'filename' => 'full_name.txt',
'headers' => [
'Content-Type' => 'application/octet-stream',
],
],
[
'name' => 'profile_image',
Expand All @@ -40,10 +37,6 @@ public function updateUserProfile(): string
'name' => 'personal_note',
'contents' => 'testing',
'filename' => 'note.txt',
'headers' => [
'X-Foo' => 'this is a note',
'Content-Type' => 'application/octet-stream',
],
],
],
'headers' => [
Expand Down
21 changes: 2 additions & 19 deletions example/multipart/consumer/tests/Service/HttpClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function testUpdateUserProfile()
],
])
->setBody(new Multipart([
new Part($fullNameTempFile = $this->createTempFile($fullName), 'full_name', 'text/plain'),
new Part(__DIR__ . '/../_resource/full_name.txt', 'full_name', 'text/plain'),
new Part(__DIR__ . '/../_resource/image.jpg', 'profile_image', in_array(php_uname('m'), ['AMD64', 'arm64']) ? 'application/octet-stream' : 'image/jpeg'),
new Part($personalNoteTempFile = $this->createTempFile($personalNote), 'personal_note', 'text/plain'),
new Part(__DIR__ . '/../_resource/note.txt', 'personal_note', 'text/plain'),
]));

$response = new ProviderResponse();
Expand Down Expand Up @@ -68,28 +68,11 @@ public function testUpdateUserProfile()
$userProfileResponse = $service->updateUserProfile();
$verifyResult = $builder->verify();

unlink($fullNameTempFile);
unlink($personalNoteTempFile);

$this->assertTrue($verifyResult);
$this->assertEquals([
'full_name' => $fullName,
'profile_image' => $profileImageUrl,
'personal_note' => $personalNote,
], \json_decode($userProfileResponse, true, 512, JSON_THROW_ON_ERROR));
}

private function createTempFile(string $contents): string
{
$path = tempnam(sys_get_temp_dir(), 'pact');
//$newPath = "$path.txt";
//rename($path, $newPath);
$newPath = $path;

$handle = fopen($newPath, 'w');
fwrite($handle, $contents);
fclose($handle);

return $newPath;
}
}
1 change: 1 addition & 0 deletions example/multipart/consumer/tests/_resource/full_name.txt

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this file?

Copy link
Contributor Author

@tienvx tienvx Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pactffi_with_multipart_file_v2 require path to the part's file.

Instead of creating temporary files using code, I put the content into pre-created files, so no need those ugly code.

The purpose of those files:

  • full_name.txt
  • image.jpg
  • note.txt

are just for demo that the multipart request POST /user-profile has 3 parts: full_name, profile_image and personal_note. Nothing special about it really.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My oversight, I didn't fully absorb the full path name 😅 Makes complete sense!

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Colten Ziemann
1 change: 1 addition & 0 deletions example/multipart/consumer/tests/_resource/note.txt

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And similarly, this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing special about these file. It's just for testing that I can actually send multiple parts in a multipart request.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.8",
"ffi": "0.4.9",
"mockserver": "1.2.4",
"models": "1.1.11"
},
Expand Down