Skip to content

Commit

Permalink
fix(browser): allow save source as raw content
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Feb 21, 2023
1 parent 41a5316 commit 5ddaf68
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/Browser/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,28 +442,36 @@ 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');
}

/**
* @return static
*/
final public function assertXml(): self
{
return $this->assertHeaderContains('Content-Type', 'xml');
return $this->assertContentType('xml');
}

/**
* @return static
*/
final public function assertHtml(): self
{
return $this->assertHeaderContains('Content-Type', 'html');
return $this->assertContentType('html');
}

/**
Expand Down
31 changes: 22 additions & 9 deletions src/Browser/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,26 +96,38 @@ public function expectException($expectedException, ?string $expectedMessage = n

public function source(): string
{
$ret = "<!--\n";
$ret = '';

try {
$ret .= "URL: {$this->getCurrentUrl()} ({$this->getStatusCode()})\n\n";
$isTextContent = str_contains((string) $this->getResponseHeader('content-type'), 'text')
|| str_contains((string) $this->getResponseHeader('content-type'), 'json');
} catch (UnsupportedDriverActionException) {
$isTextContent = true;
}

if ($isTextContent) {
$ret .= "<!--\n";

try {
$ret .= "URL: {$this->getCurrentUrl()} ({$this->getStatusCode()})\n\n";

foreach ($this->getResponseHeaders() as $header => $values) {
foreach ((array) $values as $value) {
$ret .= "{$header}: {$value}\n";
foreach ($this->getResponseHeaders() as $header => $values) {
foreach ((array) $values as $value) {
$ret .= "{$header}: {$value}\n";
}
}
} catch (DriverException $e) {
$ret = "URL: {$this->getCurrentUrl()}\n";
}
} catch (DriverException $e) {
$ret = "URL: {$this->getCurrentUrl()}\n";

$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;
Expand Down
18 changes: 18 additions & 0 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,24 @@ public function can_save_source(): void
$this->assertStringContainsString('<h1>h1 title</h1>', $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
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
}
Binary file added tests/Fixture/files/attachment.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/KernelBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ public function assert_content_types(): void
->assertXml()
->get('/page1')
->assertHtml()
->get('/zip')
->assertContentType('zip')
;
}

Expand Down

0 comments on commit 5ddaf68

Please sign in to comment.