Skip to content

Commit b5463b8

Browse files
committed
Merge branches '2.4-develop' and 'patch-1' of https://github.com/pmzandbergen/magento2 into patch-1
� Conflicts: � lib/internal/Magento/Framework/HTTP/Client/Curl.php
2 parents 730c495 + 552e0c1 commit b5463b8

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

lib/internal/Magento/Framework/HTTP/Client/Curl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ protected function parseHeaders($ch, $data)
453453
}
454454

455455
if (strlen($name)) {
456-
if ("set-cookie" === strtolower($name)) {
456+
if ('set-cookie' === strtolower($name)) {
457457
if (!isset($this->_responseHeaders['Set-Cookie'])) {
458458
$this->_responseHeaders['Set-Cookie'] = [];
459459
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\HTTP\Test\Unit\Client;
10+
11+
use Magento\Framework\HTTP\Client\CurlMock;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test HTTP client based on cUrl (mocked curl_exec).
16+
*/
17+
class CurlMockTest extends TestCase
18+
{
19+
/**
20+
* @var CurlMock
21+
*/
22+
protected $model;
23+
24+
/**
25+
* @var \Closure
26+
*/
27+
public static $curlExectClosure;
28+
29+
protected function setUp(): void
30+
{
31+
require_once __DIR__ . '/_files/curl_exec_mock.php';
32+
$this->model = new CurlMock();
33+
}
34+
35+
/**
36+
* Handle Curl response
37+
*
38+
* @param string $response
39+
* @return string
40+
*/
41+
private function handleResponse(string $response): string
42+
{
43+
// Make sure we use valid newlines
44+
$response = explode("\r\n\r\n", str_replace("\n", "\r\n", $response), 2);
45+
46+
// Parse headers
47+
$headers = explode("\r\n", $response[0]);
48+
foreach ($headers as $header) {
49+
call_user_func([$this->model, 'parseHeaders'], $this->model->getResource(), $header);
50+
}
51+
52+
// Return body
53+
return $response[1] ?? '';
54+
}
55+
56+
/**
57+
* Check that HTTP client parses cookies.
58+
*
59+
* @param string $response
60+
* @dataProvider cookiesDataProvider
61+
*/
62+
public function testCookies($response)
63+
{
64+
self::$curlExectClosure = function () use ($response) {
65+
$this->handleResponse($response);
66+
};
67+
$this->model->get('http://127.0.0.1/test');
68+
$cookies = $this->model->getCookies();
69+
$this->assertIsArray($cookies);
70+
$this->assertEquals([
71+
'Normal' => 'OK',
72+
'Uppercase' => 'OK',
73+
'Lowercase' => 'OK',
74+
], $cookies);
75+
}
76+
77+
/**
78+
* @return array
79+
*/
80+
public function cookiesDataProvider()
81+
{
82+
return [
83+
[file_get_contents(__DIR__ . '/_files/curl_response_cookies.txt')],
84+
];
85+
}
86+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\HTTP\Client;
9+
10+
use Magento\Framework\HTTP\Test\Unit\Client\CurlMockTest;
11+
12+
/**
13+
* Override global PHP function
14+
*
15+
* @SuppressWarnings("unused")
16+
* @param mixed $resource
17+
* @return string
18+
*/
19+
function curl_exec($resource)
20+
{
21+
return call_user_func(CurlMockTest::$curlExectClosure);
22+
}
23+
24+
/**
25+
* Extended Curl class with modifications for testing
26+
*/
27+
class CurlMock extends Curl
28+
{
29+
/**
30+
* Unfortunately, it is necessary for the tests to set this function public.
31+
*
32+
* @param resource $ch curl handle, not needed
33+
* @param string $data
34+
* @return int
35+
* @throws \Exception
36+
*/
37+
public function parseHeaders($ch, $data)
38+
{
39+
return parent::parseHeaders($ch, $data);
40+
}
41+
42+
/**
43+
* Return Curl resource, only used for testing.
44+
*
45+
* @return resource
46+
*/
47+
public function getResource()
48+
{
49+
return $this->_ch;
50+
}
51+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
X-Frame-Options: SAMEORIGIN
4+
Strict-Transport-Security: max-age=14400
5+
Strict-Transport-Security: max-age=14400
6+
Content-Type: text/html; charset=UTF-8
7+
Date: Mon, 22 Apr 2013 09:52:36 GMT
8+
Content-Length: 8
9+
Connection: keep-alive
10+
Set-Cookie: Normal=OK
11+
SET-COOKIE: Uppercase=OK
12+
set-cookie: Lowercase=OK
13+
14+
VERIFIED

0 commit comments

Comments
 (0)