MCPcopy Create free account
hub / github.com/Rello/analytics / ExternalHttpClientTest

Class ExternalHttpClientTest

tests/Security/ExternalHttpClientTest.php:14–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12use PHPUnit\Framework\TestCase;
13
14class ExternalHttpClientTest extends TestCase {
15 public function testRequestUsesSecurityOptionsAndReturnsBody(): void {
16 $captured = [];
17 $response = new class() {
18 public function getStatusCode(): int {
19 return 200;
20 }
21
22 public function getBody(): string {
23 return '{"ok":true}';
24 }
25 };
26 $client = new class($response, $captured) {
27 public array $captured;
28 private object $response;
29
30 public function __construct(object $response, array &$captured) {
31 $this->response = $response;
32 $this->captured =& $captured;
33 }
34
35 public function request(string $method, string $url, array $options): object {
36 $this->captured = compact('method', 'url', 'options');
37 return $this->response;
38 }
39 };
40 $service = new class($client) implements IClientService {
41 public function __construct(private object $client) {
42 }
43
44 public function newClient(): object {
45 return $this->client;
46 }
47 };
48
49 $urlValidator = $this->createMock(ExternalUrlValidator::class);
50 $urlValidator->method('validate')->willReturn(null);
51
52 $result = (new ExternalHttpClient($service, $urlValidator))->request('https://93.184.216.34/data');
53
54 $this->assertSame(200, $result['status']);
55 $this->assertSame('{"ok":true}', $result['body']);
56 $this->assertNull($result['error']);
57 $this->assertFalse($captured['options']['allow_redirects']);
58 $this->assertTrue($captured['options']['verify']);
59 $this->assertSame(10, $captured['options']['connect_timeout']);
60 $this->assertSame(60, $captured['options']['timeout']);
61 $this->assertArrayNotHasKey('stream', $captured['options']);
62 }
63}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected