Skip to content

Commit

Permalink
test(http): cover multipart base64 encoded and embedding id
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jun 14, 2024
1 parent eb03198 commit b073b6b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests export-ignore
.* export-ignore
box.json.dist export-ignore
composer.lock export-ignore
composer-require-* export-ignore
composer-require-* export-ignore
docker-compose.yaml export-ignore
Makefile export-ignore
phpunit.xml* export-ignore
Expand Down
11 changes: 7 additions & 4 deletions src/Traffic/Message/Multipart/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,23 @@ public function isEmbedded(): bool
*/
public function getEmbeddingId(): ?string
{
$matches = [];
$result = match (true) {
// Content-Disposition is inline and name is present
\str_starts_with($this->getHeaderLine('Content-Disposition'), 'inline') && \preg_match(
'/name=(?:\"([^\"]++)\"|\'([^\']++)\'|([^;,\\s]++))/',
'/(?:\\s|^|;|,)name=(?:\"([^\"]++)\"|\'([^\']++)\'|([^;,\\s]++))/',
$this->getHeaderLine('Content-Disposition'),
$matches,
) === 1 => $matches[1],
PREG_UNMATCHED_AS_NULL,
) === 1 => $matches[1] ?? $matches[2] ?? $matches[3],

// Content-Type is image/* and has name
\str_starts_with($this->getHeaderLine('Content-Type'), 'image/') && \preg_match(
'/name=(?:\"([^\"]++)\"|\'([^\']++)\'|([^;,\\s]++))/',
'/(?:\\s|^|;|,)name=(?:\"([^\"]++)\"|\'([^\']++)\'|([^;,\\s]++))/',
$this->getHeaderLine('Content-Type'),
$matches,
) === 1 => $matches[1],
PREG_UNMATCHED_AS_NULL,
) === 1 => $matches[1] ?? $matches[2] ?? $matches[3],
default => null,
};

Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Traffic/Message/Multipart/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buggregator\Trap\Support\StreamHelper;
use Buggregator\Trap\Traffic\Message\Multipart\File;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class FileTest extends TestCase
Expand All @@ -29,6 +30,29 @@ public function testWithHeader(): void
self::assertSame('baz', $new->getHeaderLine('foo'));
}

public static function provideEmbeddings(): iterable
{
yield [['Content-Type' => 'image/jpeg'], null];
yield [['Content-Type' => 'image/jpeg', 'Content-Disposition' => 'inline; filename="foo.jpg"'], null];
yield [['Content-Type' => 'image/jpeg', 'Content-Disposition' => 'inline; filename="foo.jpg"; id="bar"'], null];
yield [['Content-Type' => 'image/png; name="embedding-name"; id="bar"'], 'embedding-name'];
yield [['Content-Type' => 'image/png; a-name=test; name=embedding-name; b-name=test'], 'embedding-name'];
yield [['Content-Type' => 'image/png; name=\'embedding-name\''], 'embedding-name'];
yield [['Content-Disposition' => 'inline; name="embedding-name"'], 'embedding-name'];
yield [['Content-Disposition' => 'inline; ; a-name="a"; name=embedding; file-name=3'], 'embedding'];
yield [['Content-Disposition' => 'inline; name=\'embedding-1\''], 'embedding-1'];
}

#[DataProvider('provideEmbeddings')]
public function testEmbeddingId(array $headers, ?string $result): void
{
$field = File::fromArray([
'headers' => $headers,
]);

self::assertSame($result, $field->getEmbeddingId());
}

public function testFromArray(): void
{
$field = File::fromArray([
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Traffic/Parser/MultipartBodyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@ public function testWithFileAttach(): void
self::assertSame($file2, $file->getStream()->__toString());
}

public function testBase64Encoded(): void
{
$file1 = \file_get_contents(__DIR__ . '/../../../Stub/deburger.png');
$file2 = \file_get_contents(__DIR__ . '/../../../Stub/buggregator.png');

$encoded1 = \base64_encode($file1);
$encoded2 = \base64_encode($file2);
$body = $this->makeStream(
<<<BODY
--Asrf456BGe4h\r
Content-Type: image/png; name=4486bda9ad8b1f422deaf6a750194668@trap\r
Content-Transfer-Encoding: base64\r
Content-Disposition: inline; filename=logo-embeddable\r
\r
$encoded1\r
--Asrf456BGe4h\r
Content-Disposition: inline; name="AttachedFile2"; filename=logo-embeddable\r
Content-Transfer-Encoding: base64\r
Content-Type: image/png\r
\r
$encoded2\r
--Asrf456BGe4h--\r\n\r\n
BODY,
);

$result = $this->parse($body, 'Asrf456BGe4h');

self::assertCount(2, $result);
$file = $result[0];
// Uploaded files
self::assertInstanceOf(File::class, $file);
self::assertNull($file->getName());
self::assertSame('logo-embeddable', $file->getClientFilename());
self::assertSame('image/png', $file->getClientMediaType());
self::assertSame('4486bda9ad8b1f422deaf6a750194668@trap', $file->getEmbeddingId());
self::assertSame($file1, $file->getStream()->__toString());

$file = $result[1];
self::assertInstanceOf(File::class, $file);
self::assertSame('AttachedFile2', $file->getName());
self::assertSame('logo-embeddable', $file->getClientFilename());
self::assertSame('image/png', $file->getClientMediaType());
self::assertSame('AttachedFile2', $file->getEmbeddingId());
self::assertSame($file2, $file->getStream()->__toString());
}

private function makeStream(string $body): StreamInterface
{
$stream = Stream::create($body);
Expand Down

0 comments on commit b073b6b

Please sign in to comment.