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

Class ExternalHttpClientTest

tests/Security/ExternalHttpClientTest.php:16–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected