|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright (c) KUBO Atsuhiro <[email protected]>, |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This file is part of PHPMentorsProxyURLRewriteBundle. |
| 7 | + * |
| 8 | + * This program and the accompanying materials are made available under |
| 9 | + * the terms of the BSD 2-Clause License which accompanies this |
| 10 | + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PHPMentors\ProxyURLRewriteBundle\Functional; |
| 14 | + |
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | +use Symfony\Component\Filesystem\Filesystem; |
| 19 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @since Class available since Release 1.2.0 |
| 23 | + */ |
| 24 | +class HostFilterTest extends WebTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + protected function setUp() |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $_SERVER['KERNEL_DIR'] = __DIR__.'/app'; |
| 34 | + $_SERVER['SYMFONY__SECRET'] = hash('sha1', uniqid(mt_rand())); |
| 35 | + |
| 36 | + $this->removeCacheDir(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + protected function tearDown() |
| 43 | + { |
| 44 | + parent::tearDown(); |
| 45 | + |
| 46 | + $this->removeCacheDir(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritdoc} |
| 51 | + */ |
| 52 | + protected static function createKernel(array $options = array()) |
| 53 | + { |
| 54 | + $kernel = KernelTestCase::createKernel($options); |
| 55 | + if (array_key_exists('config', $options)) { |
| 56 | + $kernel->setConfig($options['config']); |
| 57 | + } |
| 58 | + |
| 59 | + return $kernel; |
| 60 | + } |
| 61 | + |
| 62 | + protected function removeCacheDir() |
| 63 | + { |
| 64 | + $fileSystem = new Filesystem(); |
| 65 | + $fileSystem->remove($_SERVER['KERNEL_DIR'].'/cache/test'); |
| 66 | + } |
| 67 | + |
| 68 | + public function filterHostData() |
| 69 | + { |
| 70 | + return array( |
| 71 | + array('/foo/bar/', true, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'), |
| 72 | + array('/foo/bar/', false, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'), |
| 73 | + array('//example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'), |
| 74 | + array('//example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'), |
| 75 | + array('http://example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'), |
| 76 | + array('http://example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'), |
| 77 | + array('https://example.com/foo/bar/', true, 'https://baz.example.com/foo/bar/url-rewriting-in-controllers/'), |
| 78 | + array('https://example.com/foo/bar/', false, 'https://example.com/foo/bar/url-rewriting-in-controllers/'), |
| 79 | + array('http://example.com:8180/foo/bar/', true, 'http://baz.example.com:8180/foo/bar/url-rewriting-in-controllers/'), |
| 80 | + array('http://example.com:8180/foo/bar/', false, 'http://example.com:8180/foo/bar/url-rewriting-in-controllers/'), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @test |
| 86 | + * @dataProvider filterHostData |
| 87 | + * |
| 88 | + * @param string $proxyUrl |
| 89 | + * @param bool $hostFilterService |
| 90 | + * @param string $rewroteUrl |
| 91 | + */ |
| 92 | + public function filterHost($proxyUrl, $hostFilterService, $rewroteUrl) |
| 93 | + { |
| 94 | + $client = $this->createClient(array('config' => function (ContainerBuilder $container) use ($proxyUrl, $hostFilterService) { |
| 95 | + $config = array( |
| 96 | + 'path' => '!^.*!', |
| 97 | + 'proxy_url' => $proxyUrl, |
| 98 | + ); |
| 99 | + if ($hostFilterService) { |
| 100 | + $config['host_filter_service'] = 'phpmentors_proxy_url_rewrite_test.host_filter'; |
| 101 | + } |
| 102 | + |
| 103 | + $container->loadFromExtension('phpmentors_proxy_url_rewrite', array( |
| 104 | + 'proxy_urls' => array( |
| 105 | + 'foo' => $config, |
| 106 | + ), )); |
| 107 | + })); |
| 108 | + |
| 109 | + $client->request('GET', sprintf('http://backend1.example.com:8080/url-rewriting-in-controllers/?referenceType=%s', UrlGeneratorInterface::ABSOLUTE_URL)); |
| 110 | + |
| 111 | + $this->assertThat($client->getResponse()->getStatusCode(), $this->equalTo(200), $client->getResponse()->getContent()); |
| 112 | + $this->assertThat($client->getCrawler()->filterXpath("//*[@id='generateUrl']")->text(), $this->equalTo($rewroteUrl)); |
| 113 | + } |
| 114 | +} |
0 commit comments