Skip to content

Commit acea12b

Browse files
committed
add tests for handle
1 parent 670a77b commit acea12b

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

tests/HttpClient/HttpClientTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,59 @@ public function testClientMakesUncompressedRequestWhenCompressionDisabled(): voi
7575
$this->assertEquals(\strlen($request->getStringBody()), $serverOutput['headers']['Content-Length']);
7676
}
7777

78+
public function testClientMakesRequestWhenShareHandleDisabled(): void
79+
{
80+
$testServer = $this->startTestServer();
81+
82+
$options = new Options([
83+
'dsn' => "http://publicKey@{$testServer}/200",
84+
'http_enable_curl_share_handle' => false,
85+
]);
86+
87+
$request = new Request();
88+
$request->setStringBody('test');
89+
90+
$client = new HttpClient('sentry.php', 'testing');
91+
$response = $client->sendRequest($request, $options);
92+
93+
$serverOutput = $this->stopTestServer();
94+
95+
$this->assertTrue($response->isSuccess());
96+
$this->assertEquals(200, $response->getStatusCode());
97+
$this->assertEquals($response->getStatusCode(), $serverOutput['status']);
98+
$this->assertEquals($request->getStringBody(), $serverOutput['body']);
99+
$this->assertNull($this->getShareHandleFromClient($client));
100+
}
101+
102+
public function testShareHandleIsInitializedOnlyOncePerHttpClientInstance(): void
103+
{
104+
$testServer = $this->startTestServer();
105+
106+
$options = new Options([
107+
'dsn' => "http://publicKey@{$testServer}/200",
108+
'http_enable_curl_share_handle' => true,
109+
]);
110+
111+
$request = new Request();
112+
$request->setStringBody('test');
113+
114+
$client = new HttpClient('sentry.php', 'testing');
115+
116+
$firstResponse = $client->sendRequest($request, $options);
117+
$firstShareHandle = $this->getShareHandleFromClient($client);
118+
119+
$secondResponse = $client->sendRequest($request, $options);
120+
$secondShareHandle = $this->getShareHandleFromClient($client);
121+
122+
$this->stopTestServer();
123+
124+
$this->assertTrue($firstResponse->isSuccess());
125+
$this->assertTrue($secondResponse->isSuccess());
126+
$this->assertNotNull($firstShareHandle);
127+
$this->assertShareHandleHasExpectedType($firstShareHandle);
128+
$this->assertSame($firstShareHandle, $secondShareHandle);
129+
}
130+
78131
public function testClientReturnsBodyAsErrorOnNonSuccessStatusCode(): void
79132
{
80133
$testServer = $this->startTestServer();
@@ -118,4 +171,38 @@ public function testThrowsExceptionIfRequestDataIsEmpty(): void
118171
$client = new HttpClient('sentry.php', 'testing');
119172
$client->sendRequest(new Request(), $options);
120173
}
174+
175+
/**
176+
* @return object|resource|null
177+
*/
178+
private function getShareHandleFromClient(HttpClient $client)
179+
{
180+
$reflectionProperty = new \ReflectionProperty(HttpClient::class, 'shareHandle');
181+
if (\PHP_VERSION_ID < 80100) {
182+
$reflectionProperty->setAccessible(true);
183+
}
184+
185+
return $reflectionProperty->getValue($client);
186+
}
187+
188+
/**
189+
* @param object|resource $shareHandle
190+
*/
191+
private function assertShareHandleHasExpectedType($shareHandle): void
192+
{
193+
if (\PHP_VERSION_ID < 80000) {
194+
$this->assertTrue(\is_resource($shareHandle));
195+
196+
return;
197+
}
198+
199+
if (\PHP_VERSION_ID >= 80500) {
200+
$this->assertTrue(\class_exists('CurlSharePersistentHandle'));
201+
$this->assertInstanceOf(\CurlSharePersistentHandle::class, $shareHandle);
202+
203+
return;
204+
}
205+
206+
$this->assertInstanceOf(\CurlShareHandle::class, $shareHandle);
207+
}
121208
}

0 commit comments

Comments
 (0)