Skip to content
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

Update for newer PHP and dependencies #82

Open
wants to merge 10 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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

sudo: false

dist: trusty
dist: bionic

before_script:
- composer self-update
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

This is a HTTP/HTTPS proxy script that forwards requests to a different server and returns the response. The Proxy class uses PSR7 request/response objects as input/output, and uses Guzzle to do the actual HTTP request.


## Installation

Install using composer:
Expand Down Expand Up @@ -34,11 +35,16 @@ $proxy = new Proxy(new GuzzleAdapter($guzzle));
// Add a response filter that removes the encoding headers.
$proxy->filter(new RemoveEncodingFilter());

// Forward the request and get the response.
$response = $proxy->forward($request)->to('http://example.com');
try {
// Forward the request and get the response.
$response = $proxy->forward($request)->to('http://example.com');

// Output response to the browser.
(new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($response);
// Output response to the browser.
(new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($response);
} catch(\GuzzleHttp\Exception\BadResponseException $e) {
// Correct way to handle bad responses
(new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($e->getResponse());
}
```

## Filters
Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
}
],
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.3 || ^8.0",
"psr/http-message": "^1.0",
"guzzlehttp/guzzle": "^6.0",
"laminas/laminas-diactoros": "^2.0",
"guzzlehttp/guzzle": "^7.1",
"laminas/laminas-diactoros": "^2.5",
"relay/relay": "^1.0",
"laminas/laminas-httphandlerrunner": "^1.1"
"laminas/laminas-httphandlerrunner": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0|^7.0",
"php-coveralls/php-coveralls": "^2.0",
"mockery/mockery": "^1.1"
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^9.5",
"php-coveralls/php-coveralls": "^2.4",
"mockery/mockery": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 21 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="PHPProxy Test Suite">
<directory>tests/Proxy/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="PHPProxy Test Suite">
<directory>tests/Proxy/</directory>
</testsuite>
</testsuites>
</phpunit>
7 changes: 6 additions & 1 deletion src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Proxy;

use GuzzleHttp\Exception\ClientException;
use Proxy\Adapter\AdapterInterface;
use Proxy\Exception\UnexpectedValueException;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -79,7 +80,11 @@ public function to($target)
$stack = $this->filters;

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

return $next($request, $response);
};
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
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion tests/Proxy/Filter/RewriteLocationFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RewriteLocationFilterTest extends TestCase
*/
private $filter;

public function setUp()
public function setUp(): void
{
$this->filter = new RewriteLocationFilter();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Proxy/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class ProxyTest extends TestCase
*/
private $proxy;

public function setUp()
public function setUp(): void
{
$this->proxy = new Proxy(new DummyAdapter());
}

/**
* @test
* @expectedException UnexpectedValueException
*/
public function to_throws_exception_if_no_request_is_given()
{
$this->expectException('UnexpectedValueException');
$this->proxy->to('http://www.example.com');
}

Expand Down