Skip to content

All composer requires up-to-date, includes and closes PR #90 and resolves #87 #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests with PHPUnit

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0

sudo: false

dist: trusty
dist: bionic

before_script:
- composer self-update
Expand All @@ -18,4 +18,4 @@ script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- vendor/bin/php-coveralls -v
- vendor/bin/php-coveralls -v
20 changes: 12 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "jenssegers/proxy",
"description": "Proxy library that forwards requests to the desired url and returns the response.",
"keywords": ["proxy"],
"keywords": [
"proxy"
],
"homepage": "https://github.com/jenssegers/php-proxy",
"license": "MIT",
"authors": [
Expand All @@ -16,17 +18,16 @@
}
],
"require": {
"php": "^5.6 || ^7.0",
"psr/http-message": "^1.0",
"guzzlehttp/guzzle": "^6.0",
"php": "^7.3|^8.0",
"guzzlehttp/guzzle": "^7.0.1",
"laminas/laminas-diactoros": "^2.0",
"relay/relay": "^1.0",
"relay/relay": "~2.0",
"laminas/laminas-httphandlerrunner": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0|^7.0",
"phpunit/phpunit": "^9.3",
"php-coveralls/php-coveralls": "^2.0",
"mockery/mockery": "^1.1"
"mockery/mockery": "^1.4.2"
},
"autoload": {
"psr-4": {
Expand All @@ -37,5 +38,8 @@
"psr-4": {
"Proxy\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
}
2 changes: 1 addition & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface FilterInterface
* @param callable $next
* @return ResponseInterface
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next);
public function __invoke(RequestInterface $request, callable $next): ResponseInterface;
}
4 changes: 2 additions & 2 deletions src/Filter/RemoveEncodingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class RemoveEncodingFilter implements FilterInterface
/**
* @inheritdoc
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
$response = $next($request, $response);
$response = $next($request);

return $response
->withoutHeader(self::TRANSFER_ENCODING)
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/RemoveLocationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class RemoveLocationFilter implements FilterInterface
/**
* @inheritdoc
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
$response = $next($request, $response);
$response = $next($request);

if ($response->hasHeader(self::LOCATION)) {
$response = $response
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/RewriteLocationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class RewriteLocationFilter implements FilterInterface
/**
* @inheritdoc
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
$response = $next($request, $response);
$response = $next($request);

if ($response->hasHeader(self::LOCATION)) {
$location = $response->getHeader(self::LOCATION)[0];
Expand Down
17 changes: 9 additions & 8 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Proxy;

use GuzzleHttp\Exception\ClientException;
use Relay\RelayBuilder;
use Laminas\Diactoros\Uri;
use Laminas\Diactoros\Response;
use Proxy\Adapter\AdapterInterface;
use Proxy\Exception\UnexpectedValueException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Relay\RelayBuilder;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Uri;
use Psr\Http\Server\MiddlewareInterface;
use GuzzleHttp\Exception\ClientException;
use Proxy\Exception\UnexpectedValueException;

class Proxy
{
Expand Down Expand Up @@ -79,19 +80,19 @@ public function to($target)

$stack = $this->filters;

$stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) {
$stack[] = function (RequestInterface $request, callable $next) {
try {
$response = $this->adapter->send($request);
} catch (ClientException $ex) {
$response = $ex->getResponse();
}

return $next($request, $response);
return $response;
};

$relay = (new RelayBuilder)->newInstance($stack);

return $relay($request, new Response);
return $relay->handle($request);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Proxy/Adapter/Dummy/DummyAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DummyAdapterTest extends TestCase
*/
private $adapter;

public function setUp()
public function setUp(): Void
{
$this->adapter = new DummyAdapter();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GuzzleAdapterTest extends TestCase
*/
private $body = 'Totally awesome response body';

public function setUp()
public function setUp(): Void
{
$mock = new MockHandler([
$this->createResponse(),
Expand Down
8 changes: 4 additions & 4 deletions tests/Proxy/Filter/RemoveEncodingFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RemoveEncodingFilterTest extends TestCase
*/
private $filter;

public function setUp()
public function setUp(): Void
{
$this->filter = new RemoveEncodingFilter();
}
Expand All @@ -29,7 +29,7 @@ public function filter_removes_transfer_encoding()
return $response;
};

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

$this->assertFalse($response->hasHeader(RemoveEncodingFilter::TRANSFER_ENCODING));
}
Expand All @@ -41,11 +41,11 @@ public function filter_removes_content_encoding()
{
$request = new Request();
$response = new Response('php://memory', 200, [RemoveEncodingFilter::TRANSFER_ENCODING => 'foo']);
$next = function ($request, $response) {
$next = function () use ($response) {
return $response;
};

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

$this->assertFalse($response->hasHeader(RemoveEncodingFilter::CONTENT_ENCODING));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Proxy/Filter/RemoveLocationFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RemoveLocationFilterTest extends TestCase
*/
private $filter;

public function setUp()
public function setUp(): Void
{
$this->filter = new RemoveLocationFilter();
}
Expand All @@ -29,7 +29,7 @@ public function filter_removes_location()
return $response;
};

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

$this->assertFalse($response->hasHeader(RemoveLocationFilter::LOCATION));
}
Expand All @@ -45,7 +45,7 @@ public function filter_adds_location_as_xheader()
return $response;
};

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

$this->assertEquals('http://www.example.com', $response->getHeader('X-Proxy-Location')[0]);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Proxy/Filter/RewriteLocationFilterTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Proxy\Filter;
<?php

namespace Proxy\Filter;

use PHPUnit\Framework\TestCase;
use Laminas\Diactoros\Request;
Expand All @@ -11,7 +13,7 @@ class RewriteLocationFilterTest extends TestCase
*/
private $filter;

public function setUp()
public function setUp(): Void
{
$this->filter = new RewriteLocationFilter();
}
Expand All @@ -30,7 +32,7 @@ public function filter_rewrites_location()
return $response;
};

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

$this->assertTrue($response->hasHeader('X-Proxy-Location'));
$this->assertTrue($response->hasHeader(RewriteLocationFilter::LOCATION));
Expand Down
21 changes: 13 additions & 8 deletions tests/Proxy/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Proxy;

use Laminas\Diactoros\Request;
use Laminas\Diactoros\Response;
use PHPUnit\Framework\TestCase;
use Proxy\Adapter\Dummy\DummyAdapter;
use Proxy\Exception\UnexpectedValueException;
use Psr\Http\Message\RequestInterface;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Proxy\Exception\UnexpectedValueException;
use PHPUnit\Framework\InvalidArgumentException;

class ProxyTest extends TestCase
{
Expand All @@ -17,7 +18,7 @@ class ProxyTest extends TestCase
*/
private $proxy;

public function setUp()
public function setUp(): void
{
$this->proxy = new Proxy(new DummyAdapter());
}
Expand All @@ -28,6 +29,7 @@ public function setUp()
*/
public function to_throws_exception_if_no_request_is_given()
{
$this->expectException(UnexpectedValueException::class);
$this->proxy->to('http://www.example.com');
}

Expand All @@ -48,9 +50,12 @@ public function to_applies_filters()
{
$applied = false;

$this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $response) use (&$applied
$this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $next) use (
&$applied
) {
$applied = true;

return new Response('php://memory', 200);
})->to('http://www.example.com');

$this->assertTrue($applied);
Expand All @@ -61,7 +66,7 @@ public function to_applies_filters()
*/
public function to_sends_request()
{
$request = new Request('http://localhost/path?query=yes', 'GET');
$request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes');
$url = 'https://www.example.com';

$adapter = $this->getMockBuilder(DummyAdapter::class)
Expand All @@ -85,7 +90,7 @@ public function to_sends_request()
*/
public function to_sends_request_with_port()
{
$request = new Request('http://localhost/path?query=yes', 'GET');
$request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes');
$url = 'https://www.example.com:3000';

$adapter = $this->getMockBuilder(DummyAdapter::class)
Expand All @@ -109,7 +114,7 @@ public function to_sends_request_with_port()
*/
public function to_sends_request_with_subdirectory()
{
$request = new Request('http://localhost/path?query=yes', 'GET');
$request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes');
$url = 'https://www.example.com/proxy/';

$adapter = $this->getMockBuilder(DummyAdapter::class)
Expand Down