Skip to content

Commit

Permalink
Merge branches '2.4-develop' and 'patch-1' of https://github.com/pmza…
Browse files Browse the repository at this point in the history
…ndbergen/magento2 into patch-1

� Conflicts:
�	lib/internal/Magento/Framework/HTTP/Client/Curl.php
  • Loading branch information
pmzandbergen committed Aug 2, 2020
2 parents 730c495 + 552e0c1 commit b5463b8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/HTTP/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ protected function parseHeaders($ch, $data)
}

if (strlen($name)) {
if ("set-cookie" === strtolower($name)) {
if ('set-cookie' === strtolower($name)) {
if (!isset($this->_responseHeaders['Set-Cookie'])) {
$this->_responseHeaders['Set-Cookie'] = [];
}
Expand Down
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')],
];
}
}
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;
}
}
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

0 comments on commit b5463b8

Please sign in to comment.