This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joan Fabrégat
committed
May 3, 2018
1 parent
7e2e9eb
commit b11cc73
Showing
3 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 03/05/2018 | ||
// Time: 11:43 | ||
// Project: Psr7Responses | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\Psr7Responses; | ||
use CodeInc\Psr7Responses\Tests\HttpProxyResponseTest; | ||
use GuzzleHttp\Psr7\Response; | ||
use function GuzzleHttp\Psr7\stream_for; | ||
|
||
|
||
/** | ||
* Class HttpProxyResponse | ||
* | ||
* @see HttpProxyResponseTest | ||
* @package CodeInc\Psr7Responses | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
class HttpProxyResponse extends Response | ||
{ | ||
/** | ||
* ProxyResponse constructor. | ||
* | ||
* @param string $remoteUrl | ||
* @param int $status | ||
* @param array $headers | ||
* @param string $version | ||
* @param null|string $reason | ||
* @throws ResponseException | ||
*/ | ||
public function __construct(string $remoteUrl, int $status = 200, array $headers = [], | ||
string $version = '1.1', ?string $reason = null) | ||
{ | ||
// checking the URL | ||
if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) { | ||
throw new ResponseException( | ||
sprintf("%s is not a valid URL", $remoteUrl), | ||
$this | ||
); | ||
} | ||
|
||
// downloading | ||
$context = stream_context_create(['http' => ['method' => 'GET']]); | ||
if (($f = fopen($remoteUrl, 'r', false, $context)) === false) { | ||
throw new ResponseException( | ||
sprintf("Unable to open the URL %s", $remoteUrl), | ||
$this | ||
); | ||
} | ||
|
||
// importing the headers | ||
foreach ($http_response_header as $header) { | ||
if (preg_match('/^([\\w-]+): +(.+)$/ui', $header, $matches)) { | ||
if (in_array(strtolower($matches[1]), ['content-type', 'last-modified', 'content-length', 'date'])) { | ||
$headers[$matches[1]] = $matches[2]; | ||
} | ||
} | ||
} | ||
|
||
parent::__construct($status, $headers, stream_for($f), $version, $reason); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 03/05/2018 | ||
// Time: 11:43 | ||
// Project: Psr7Responses | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\Psr7Responses\Tests; | ||
use CodeInc\Psr7Responses\HttpProxyResponse; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
|
||
/** | ||
* Class HttpProxyResponseTest | ||
* | ||
* @uses HttpProxyResponse | ||
* @package CodeInc\Psr7Responses\Tests | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
final class HttpProxyResponseTest extends TestCase | ||
{ | ||
private const TEST_TXT_URL = 'https://www.sample-videos.com/text/Sample-text-file-10kb.txt'; | ||
private const TEST_IMG_URL = 'https://www.sample-videos.com/img/Sample-jpg-image-50kb.jpg'; | ||
|
||
/** | ||
* @throws \CodeInc\Psr7Responses\ResponseException | ||
*/ | ||
public function testTxtResponse():void | ||
{ | ||
$response = new HttpProxyResponse(self::TEST_IMG_URL); | ||
self::assertInstanceOf(ResponseInterface::class, $response); | ||
self::assertResponse($response); | ||
} | ||
/** | ||
* @throws \CodeInc\Psr7Responses\ResponseException | ||
*/ | ||
public function testImgResponse():void | ||
{ | ||
$response = new HttpProxyResponse(self::TEST_TXT_URL); | ||
self::assertInstanceOf(ResponseInterface::class, $response); | ||
self::assertResponse($response); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
*/ | ||
public static function assertResponse(ResponseInterface $response):void | ||
{ | ||
// detching the stream | ||
$bodyStream = $response->getBody()->detach(); | ||
self::assertInternalType('resource', $bodyStream); | ||
|
||
// downloading the body | ||
$body = stream_get_contents($bodyStream); | ||
self::assertNotEmpty($body); | ||
self::assertEquals(strlen($body), $response->getHeaderLine('Content-Length')); | ||
|
||
fclose($bodyStream); | ||
} | ||
} |