From 506ebbf25ffd81c9681990a726ee498917ea3183 Mon Sep 17 00:00:00 2001 From: Oleksandr Gribiennikov Date: Mon, 29 Apr 2024 12:47:15 +0300 Subject: [PATCH 1/2] Update tests running script --- run_tests.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run_tests.sh b/run_tests.sh index 1700b56..2802539 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -6,12 +6,12 @@ if [ "$(docker ps -a -q -f name=$container)" ]; then docker stop $container docker rm $container fi -docker build -t $container -f $PWD/Dockerfile $PWD +docker build -t $container -f $PWD/Dockerfile --build-arg PHP_VER=7.4 $PWD docker run -d --name $container -v $PWD:/var/www -w /var/www $container -docker exec $container bash -c "composer i" +#docker exec $container bash -c "composer i" -#docker exec -it $container bash +docker exec -it $container bash docker exec $container bash -c "composer run phpunit" docker container stop $container #docker container rm --force $container From 08fd26a9ba6450980af3c5fd2d460f9475ba95f8 Mon Sep 17 00:00:00 2001 From: Oleksandr Gribiennikov Date: Tue, 30 Apr 2024 09:06:42 +0300 Subject: [PATCH 2/2] Update `WebClient` and its tests; update build --- WebToPay.php | 35 ++++++++++++++++---------------- run_tests.sh | 10 ++++----- src/WebToPay/WebClient.php | 35 ++++++++++++++++---------------- tests/WebToPay/WebClientTest.php | 34 ++++++++++++++++++++++++++----- 4 files changed, 69 insertions(+), 45 deletions(-) diff --git a/WebToPay.php b/WebToPay.php index 70bad53..c35248e 100644 --- a/WebToPay.php +++ b/WebToPay.php @@ -1150,37 +1150,38 @@ class WebToPay_WebClient * @param array $queryData * * @return string - * * @throws WebToPayException */ public function get(string $uri, array $queryData = []): string { - // Append query data to the URI if provided - if (!empty($queryData)) { - $uri .= (strpos($uri, '?') === false ? '?' : '&') - . http_build_query($queryData, '', '&'); + if (count($queryData) > 0) { + $uri .= strpos($uri, '?') === false ? '?' : '&'; + $uri .= http_build_query($queryData, '', '&'); } - - // Parse URL $url = parse_url($uri); - $scheme = $url['scheme'] ?? 'http'; - $host = $url['host'] ?? ''; - $port = $scheme === 'https' ? 443 : 80; - $path = $url['path'] ?? '/'; - $query = isset($url['query']) ? '?' . $url['query'] : ''; + if ('https' === ($url['scheme'] ?? '')) { + $host = 'ssl://' . ($url['host'] ?? ''); + $port = 443; + } else { + $host = $url['host'] ?? ''; + $port = 80; + } - // Open socket connection $fp = $this->openSocket($host, $port, $errno, $errstr, 30); if (!$fp) { throw new WebToPayException(sprintf('Cannot connect to %s', $uri), WebToPayException::E_INVALID); } - // Construct HTTP request - $out = "GET {$path}{$query} HTTP/1.1\r\n"; - $out .= "Host: {$host}\r\n"; + if(isset($url['query'])) { + $data = ($url['path'] ?? '') . '?' . $url['query']; + } else { + $data = ($url['path'] ?? ''); + } + + $out = "GET " . $data . " HTTP/1.0\r\n"; + $out .= "Host: " . ($url['host'] ?? '') . "\r\n"; $out .= "Connection: Close\r\n\r\n"; - // Send request and read response $content = $this->getContentFromSocket($fp, $out); // Separate header and content diff --git a/run_tests.sh b/run_tests.sh index 2802539..f1bf894 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -4,15 +4,13 @@ container='lib_webtopay_tests' if [ "$(docker ps -a -q -f name=$container)" ]; then docker stop $container - docker rm $container + docker container rm --force $container + docker image rm $container fi docker build -t $container -f $PWD/Dockerfile --build-arg PHP_VER=7.4 $PWD docker run -d --name $container -v $PWD:/var/www -w /var/www $container -#docker exec $container bash -c "composer i" - -docker exec -it $container bash +docker exec $container bash -c "composer i" +#docker exec -it $container bash docker exec $container bash -c "composer run phpunit" docker container stop $container -#docker container rm --force $container -#docker image rm $container diff --git a/src/WebToPay/WebClient.php b/src/WebToPay/WebClient.php index 159c7e0..68600c7 100644 --- a/src/WebToPay/WebClient.php +++ b/src/WebToPay/WebClient.php @@ -15,37 +15,38 @@ class WebToPay_WebClient * @param array $queryData * * @return string - * * @throws WebToPayException */ public function get(string $uri, array $queryData = []): string { - // Append query data to the URI if provided - if (!empty($queryData)) { - $uri .= (strpos($uri, '?') === false ? '?' : '&') - . http_build_query($queryData, '', '&'); + if (count($queryData) > 0) { + $uri .= strpos($uri, '?') === false ? '?' : '&'; + $uri .= http_build_query($queryData, '', '&'); } - - // Parse URL $url = parse_url($uri); - $scheme = $url['scheme'] ?? 'http'; - $host = $url['host'] ?? ''; - $port = $scheme === 'https' ? 443 : 80; - $path = $url['path'] ?? '/'; - $query = isset($url['query']) ? '?' . $url['query'] : ''; + if ('https' === ($url['scheme'] ?? '')) { + $host = 'ssl://' . ($url['host'] ?? ''); + $port = 443; + } else { + $host = $url['host'] ?? ''; + $port = 80; + } - // Open socket connection $fp = $this->openSocket($host, $port, $errno, $errstr, 30); if (!$fp) { throw new WebToPayException(sprintf('Cannot connect to %s', $uri), WebToPayException::E_INVALID); } - // Construct HTTP request - $out = "GET {$path}{$query} HTTP/1.1\r\n"; - $out .= "Host: {$host}\r\n"; + if(isset($url['query'])) { + $data = ($url['path'] ?? '') . '?' . $url['query']; + } else { + $data = ($url['path'] ?? ''); + } + + $out = "GET " . $data . " HTTP/1.0\r\n"; + $out .= "Host: " . ($url['host'] ?? '') . "\r\n"; $out .= "Connection: Close\r\n\r\n"; - // Send request and read response $content = $this->getContentFromSocket($fp, $out); // Separate header and content diff --git a/tests/WebToPay/WebClientTest.php b/tests/WebToPay/WebClientTest.php index 05b98c9..101c7c4 100644 --- a/tests/WebToPay/WebClientTest.php +++ b/tests/WebToPay/WebClientTest.php @@ -20,7 +20,7 @@ public function testGet_NoSocket() { $this->webClientMock->expects($this->once()) ->method('openSocket') - ->with('example.com', 443) + ->with('ssl://example.com', 443) ->willReturn(false); $this->expectException(WebToPayException::class); @@ -28,24 +28,48 @@ public function testGet_NoSocket() $this->webClientMock->get('https://example.com', ['param1' => 'value1', 'param2' => 'value2']); } - public function testGet() + public function getDataForTestingGet(): iterable + { + yield 'not secure' => [ + 'uri' => '', + 'host' => '', + 'host prefix' => '', + 'port' => 80, + 'scheme' => 'http', + ]; + + yield 'secure' => [ + 'uri' => 'https://example.com', + 'host' => 'example.com', + 'host prefix' => 'ssl://', + 'port' => 443, + 'scheme' => 'https', + ]; + } + + /** + * @dataProvider getDataForTestingGet + * + * @throws WebToPayException + */ + public function testGet(string $uri, string $host, string $hostPrefix, int $port, string $scheme) { $this->webClientMock->expects($this->once()) ->method('openSocket') - ->with('example.com', 443) + ->with($hostPrefix . $host, $port) ->willReturn('socket'); $this->webClientMock->expects($this->once()) ->method('getContentFromSocket') ->with( 'socket', - "GET /?param1=value1¶m2=value2 HTTP/1.1\r\nHost: example.com\r\nConnection: Close\r\n\r\n" + "GET ?param1=value1¶m2=value2 HTTP/1.0\r\nHost: {$host}\r\nConnection: Close\r\n\r\n" ) ->willReturn("HTTP/1.1 200 OK\r\n\r\nContent"); $this->assertEquals( 'Content', - $this->webClientMock->get('https://example.com', ['param1' => 'value1', 'param2' => 'value2']) + $this->webClientMock->get($uri, ['param1' => 'value1', 'param2' => 'value2']) ); } }