From 5ddaf68140b7bd8d209ee5f87469a79b77f23388 Mon Sep 17 00:00:00 2001 From: Mathieu Santostefano Date: Mon, 20 Feb 2023 17:20:16 +0100 Subject: [PATCH] fix(browser): allow save source as raw content --- src/Browser/KernelBrowser.php | 14 ++++++++++--- src/Browser/Session.php | 31 ++++++++++++++++++++--------- tests/BrowserTests.php | 18 +++++++++++++++++ tests/Fixture/Kernel.php | 10 ++++++++++ tests/Fixture/files/attachment.zip | Bin 0 -> 182 bytes tests/KernelBrowserTests.php | 2 ++ 6 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 tests/Fixture/files/attachment.zip diff --git a/src/Browser/KernelBrowser.php b/src/Browser/KernelBrowser.php index 610b15e..e1ef3ba 100644 --- a/src/Browser/KernelBrowser.php +++ b/src/Browser/KernelBrowser.php @@ -442,12 +442,20 @@ final public function assertHeaderContains(string $header, string $expected): se return $this; } + /** + * @return static + */ + final public function assertContentType(string $contentType): self + { + return $this->assertHeaderContains('Content-Type', $contentType); + } + /** * @return static */ final public function assertJson(): self { - return $this->assertHeaderContains('Content-Type', 'json'); + return $this->assertContentType('json'); } /** @@ -455,7 +463,7 @@ final public function assertJson(): self */ final public function assertXml(): self { - return $this->assertHeaderContains('Content-Type', 'xml'); + return $this->assertContentType('xml'); } /** @@ -463,7 +471,7 @@ final public function assertXml(): self */ final public function assertHtml(): self { - return $this->assertHeaderContains('Content-Type', 'html'); + return $this->assertContentType('html'); } /** diff --git a/src/Browser/Session.php b/src/Browser/Session.php index c416235..79eefe0 100644 --- a/src/Browser/Session.php +++ b/src/Browser/Session.php @@ -13,6 +13,7 @@ use Behat\Mink\Element\DocumentElement; use Behat\Mink\Exception\DriverException; +use Behat\Mink\Exception\UnsupportedDriverActionException; use Behat\Mink\Session as MinkSession; use Behat\Mink\WebAssert; use Symfony\Component\BrowserKit\AbstractBrowser; @@ -95,26 +96,38 @@ public function expectException($expectedException, ?string $expectedMessage = n public function source(): string { - $ret = "\n"; } - $ret .= "-->\n"; try { $ret .= $this->json(); } catch (DriverException $e) { - $ret .= \trim($this->getDriver()->getContent()); + $ret .= $isTextContent ? \trim($this->getDriver()->getContent()) : $this->getDriver()->getContent(); } return $ret; diff --git a/tests/BrowserTests.php b/tests/BrowserTests.php index a284ea3..f1af991 100644 --- a/tests/BrowserTests.php +++ b/tests/BrowserTests.php @@ -255,6 +255,24 @@ public function can_save_source(): void $this->assertStringContainsString('

h1 title

', $contents); } + /** + * @test + */ + public function can_save_source_as_zip(): void + { + $contents = self::catchFileContents(__DIR__.'/../var/browser/source/attachment.zip', function() { + $this->browser() + ->visit('/zip') + ->saveSource('attachment.zip') + ; + }); + + $this->assertEquals( + file_get_contents(__DIR__.'/../var/browser/source/attachment.zip'), + $contents + ); + } + /** * @test */ diff --git a/tests/Fixture/Kernel.php b/tests/Fixture/Kernel.php index e59d6e8..ca3dfad 100644 --- a/tests/Fixture/Kernel.php +++ b/tests/Fixture/Kernel.php @@ -18,6 +18,7 @@ use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -157,6 +158,14 @@ public function logout(): Response return new Response(); } + public function zip(): Response + { + return new Response(\file_get_contents(__DIR__.'/files/attachment.zip'), 200, [ + 'Content-Disposition' => HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, 'attachment.zip'), + 'Content-Type' => 'application/zip', + ]); + } + public function registerBundles(): iterable { yield new FrameworkBundle(); @@ -213,5 +222,6 @@ private function configureRoutes(RoutingConfigurator $routes): void $routes->add('user', '/user')->controller('kernel::user'); $routes->add('login', '/login')->controller('kernel::login'); $routes->add('logout', '/logout')->controller('kernel::logout'); + $routes->add('zip', '/zip')->controller('kernel::zip'); } } diff --git a/tests/Fixture/files/attachment.zip b/tests/Fixture/files/attachment.zip new file mode 100644 index 0000000000000000000000000000000000000000..e6ebe532758411c348ef9485ecec1bb071e64f6e GIT binary patch literal 182 zcmWIWW@h1HW&nYXkTC5jGkQgUOc3S+;>41Y#N>?J)VvbCl8O=pPa!!!uLLMs%oX6x z$Yjrq%LEmm86cq0u%r=0BTN99$Hkxk5@cXxkN}b!lU!VHGBW5sN(R$VetassertXml() ->get('/page1') ->assertHtml() + ->get('/zip') + ->assertContentType('zip') ; }