summaryrefslogtreecommitdiffstats
path: root/tests/StreamTransport.php
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 14:58:55 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 14:58:55 -0300
commit64764fb9fac0227f8e0443e7c7519d73aec275b9 (patch)
treecb4fc56eeb6ecce53854f9c530609cf01ab3349f /tests/StreamTransport.php
downloadphp-1fd1470890518a68130b3b9058fe1bebddca9f07.tar.gz
Initial importv0.1.0
Diffstat (limited to 'tests/StreamTransport.php')
-rw-r--r--tests/StreamTransport.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/StreamTransport.php b/tests/StreamTransport.php
new file mode 100644
index 0000000..174e516
--- /dev/null
+++ b/tests/StreamTransport.php
@@ -0,0 +1,52 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Cepis\Tests;
+
+use Cepis\NetworkException;
+use Cepis\Response;
+use Cepis\Transport;
+
+/**
+ * A {@see Transport} over PHP streams rather than curl — roughly the shape a
+ * WordPress plugin's `wp_remote_get` implementation takes, and proof that the
+ * seam is implementable against a different HTTP stack in a few lines.
+ */
+final class StreamTransport implements Transport
+{
+ public function get(string $url, array $headers, float $timeout): Response
+ {
+ $lines = '';
+ foreach ($headers as $name => $value) {
+ $lines .= $name . ': ' . $value . "\r\n";
+ }
+
+ $context = stream_context_create(['http' => [
+ 'method' => 'GET',
+ 'header' => $lines,
+ 'timeout' => $timeout,
+ 'ignore_errors' => true,
+ ]]);
+
+ $body = @file_get_contents($url, false, $context);
+ if ($body === false) {
+ throw new NetworkException('could not reach ' . $url);
+ }
+
+ $status = 0;
+ $received = [];
+ foreach ($http_response_header ?? [] as $line) {
+ if (preg_match('#^HTTP/\S+\s+(\d{3})#', $line, $match) === 1) {
+ $status = (int) $match[1];
+ continue;
+ }
+ $parts = explode(':', $line, 2);
+ if (count($parts) === 2) {
+ $received[trim($parts[0])][] = trim($parts[1]);
+ }
+ }
+
+ return new Response($status, $received, $body);
+ }
+}