-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branches '2.4-develop' and 'patch-1' of https://github.com/pmza…
…ndbergen/magento2 into patch-1 � Conflicts: � lib/internal/Magento/Framework/HTTP/Client/Curl.php
- Loading branch information
Showing
4 changed files
with
152 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
86 changes: 86 additions & 0 deletions
86
lib/internal/Magento/Framework/HTTP/Test/Unit/Client/CurlMockTest.php
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,86 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\HTTP\Test\Unit\Client; | ||
|
||
use Magento\Framework\HTTP\Client\CurlMock; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test HTTP client based on cUrl (mocked curl_exec). | ||
*/ | ||
class CurlMockTest extends TestCase | ||
{ | ||
/** | ||
* @var CurlMock | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* @var \Closure | ||
*/ | ||
public static $curlExectClosure; | ||
|
||
protected function setUp(): void | ||
{ | ||
require_once __DIR__ . '/_files/curl_exec_mock.php'; | ||
$this->model = new CurlMock(); | ||
} | ||
|
||
/** | ||
* Handle Curl response | ||
* | ||
* @param string $response | ||
* @return string | ||
*/ | ||
private function handleResponse(string $response): string | ||
{ | ||
// Make sure we use valid newlines | ||
$response = explode("\r\n\r\n", str_replace("\n", "\r\n", $response), 2); | ||
|
||
// Parse headers | ||
$headers = explode("\r\n", $response[0]); | ||
foreach ($headers as $header) { | ||
call_user_func([$this->model, 'parseHeaders'], $this->model->getResource(), $header); | ||
} | ||
|
||
// Return body | ||
return $response[1] ?? ''; | ||
} | ||
|
||
/** | ||
* Check that HTTP client parses cookies. | ||
* | ||
* @param string $response | ||
* @dataProvider cookiesDataProvider | ||
*/ | ||
public function testCookies($response) | ||
{ | ||
self::$curlExectClosure = function () use ($response) { | ||
$this->handleResponse($response); | ||
}; | ||
$this->model->get('http://127.0.0.1/test'); | ||
$cookies = $this->model->getCookies(); | ||
$this->assertIsArray($cookies); | ||
$this->assertEquals([ | ||
'Normal' => 'OK', | ||
'Uppercase' => 'OK', | ||
'Lowercase' => 'OK', | ||
], $cookies); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function cookiesDataProvider() | ||
{ | ||
return [ | ||
[file_get_contents(__DIR__ . '/_files/curl_response_cookies.txt')], | ||
]; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
lib/internal/Magento/Framework/HTTP/Test/Unit/Client/_files/curl_exec_mock.php
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,51 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\HTTP\Client; | ||
|
||
use Magento\Framework\HTTP\Test\Unit\Client\CurlMockTest; | ||
|
||
/** | ||
* Override global PHP function | ||
* | ||
* @SuppressWarnings("unused") | ||
* @param mixed $resource | ||
* @return string | ||
*/ | ||
function curl_exec($resource) | ||
{ | ||
return call_user_func(CurlMockTest::$curlExectClosure); | ||
} | ||
|
||
/** | ||
* Extended Curl class with modifications for testing | ||
*/ | ||
class CurlMock extends Curl | ||
{ | ||
/** | ||
* Unfortunately, it is necessary for the tests to set this function public. | ||
* | ||
* @param resource $ch curl handle, not needed | ||
* @param string $data | ||
* @return int | ||
* @throws \Exception | ||
*/ | ||
public function parseHeaders($ch, $data) | ||
{ | ||
return parent::parseHeaders($ch, $data); | ||
} | ||
|
||
/** | ||
* Return Curl resource, only used for testing. | ||
* | ||
* @return resource | ||
*/ | ||
public function getResource() | ||
{ | ||
return $this->_ch; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
lib/internal/Magento/Framework/HTTP/Test/Unit/Client/_files/curl_response_cookies.txt
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,14 @@ | ||
HTTP/1.1 200 OK | ||
Server: Apache | ||
X-Frame-Options: SAMEORIGIN | ||
Strict-Transport-Security: max-age=14400 | ||
Strict-Transport-Security: max-age=14400 | ||
Content-Type: text/html; charset=UTF-8 | ||
Date: Mon, 22 Apr 2013 09:52:36 GMT | ||
Content-Length: 8 | ||
Connection: keep-alive | ||
Set-Cookie: Normal=OK | ||
SET-COOKIE: Uppercase=OK | ||
set-cookie: Lowercase=OK | ||
|
||
VERIFIED |