Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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));
}

}