Skip to content

Commit

Permalink
Merge e40a6bd into 8ded271
Browse files Browse the repository at this point in the history
  • Loading branch information
jmortlock committed Aug 3, 2019
2 parents 8ded271 + e40a6bd commit 35f5fc2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -9,6 +9,8 @@ php:

sudo: false

dist: trusty

before_script:
- composer self-update
- travis_retry composer install --no-interaction --prefer-source
Expand Down
6 changes: 3 additions & 3 deletions src/Filter/RewriteLocationFilter.php
Expand Up @@ -17,7 +17,8 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
$response = $next($request, $response);

if ($response->hasHeader(self::LOCATION)) {
$original = parse_url($response->getHeader(self::LOCATION));
$location = $response->getHeader(self::LOCATION)[0];
$original = parse_url($location);

$target = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']), '/');

Expand All @@ -30,10 +31,9 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
}

$response = $response
->withHeader('X-Proxy-Location', $response->getHeader(self::LOCATION))
->withHeader('X-Proxy-Location', $location)
->withHeader(self::LOCATION, $target);
}

return $response;
}
}
36 changes: 36 additions & 0 deletions tests/Proxy/Filter/RewriteLocationFilterTest.php
@@ -0,0 +1,36 @@
<?php namespace Proxy\Filter;

use Zend\Diactoros\Request;
use Zend\Diactoros\Response;

class RewriteLocationFilterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RewriteLocationFilter
*/
private $filter;

public function setUp()
{
$this->filter = new RewriteLocationFilter();
}

/**
* @test
*/
public function filter_rewrites_location()
{
$_SERVER['SCRIPT_NAME'] = "";
$redirect_url = 'http://www.example.com/path?arg1=123&arg2=456';
$request = new Request;
$response = new Response('php://memory', 200, [RewriteLocationFilter::LOCATION => $redirect_url]);
$next = function () use ($response) { return $response; };

$response = call_user_func($this->filter, $request, $response, $next);

$this->assertTrue($response->hasHeader('X-Proxy-Location'));
$this->assertTrue($response->hasHeader(RewriteLocationFilter::LOCATION));
$this->assertEquals('/path?arg1=123&arg2=456', $response->getHeaderLine(RewriteLocationFilter::LOCATION));
}

}

0 comments on commit 35f5fc2

Please sign in to comment.