Skip to content

Commit 6274abb

Browse files
committed
test: Response::redirected
1 parent ab3505d commit 6274abb

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/Response.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ class Response implements ResponseInterface {
3434
/** @var null|callable */
3535
private $exitCallback;
3636
private Deferred $deferred;
37-
private CurlInterface $curl;
3837

3938
public function __construct(
4039
private ?int $statusCode = null,
4140
?ResponseHeaders $headers = null,
4241
private readonly ?Request $request = null,
42+
private readonly ?CurlInterface $curl = null,
4343
) {
4444
$this->headers = $headers ?? new ResponseHeaders();
4545
$this->stream = new Stream();
@@ -322,6 +322,7 @@ public function awaitJson(int $depth = 512, int $options = 0):JsonObject {
322322
return $jsonObject;
323323
}
324324

325+
/** @param int<1, max> $depth */
325326
private function jsonFromResponseText(string $responseText, int $depth = 512, int $options = 0):JsonObject {
326327
$builder = new JsonObjectBuilder($depth, $options);
327328
return $builder->fromJsonString($responseText);
@@ -361,23 +362,4 @@ private function getPromise():Promise {
361362

362363
return $this->deferred->getPromise();
363364
}
364-
365-
private function checkIntegrity(?string $integrity, string $contents):void {
366-
if(is_null($integrity)) {
367-
return;
368-
}
369-
370-
[$algo, $hash] = explode("-", $integrity);
371-
372-
$availableAlgos = hash_algos();
373-
if(!in_array($algo, $availableAlgos)) {
374-
throw new InvalidIntegrityAlgorithmException($algo);
375-
}
376-
377-
$hashedContents = hash($algo, $contents);
378-
379-
if($hashedContents !== $hash) {
380-
throw new IntegrityMismatchException();
381-
}
382-
}
383365
}

test/phpunit/ResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gt\Http\Test;
33

4+
use Gt\Curl\Curl;
45
use Gt\Http\ArrayBuffer;
56
use Gt\Http\Blob;
67
use Gt\Http\FormData;
@@ -294,4 +295,29 @@ public function testReload_sendsFileLineDebug():void {
294295
self::assertSame("./", $actualLocation);
295296
self::assertSame("$expectedFile:$expectedLine", $actualDebugLocation);
296297
}
298+
299+
public function testPropGetRedirected_noCurl():void {
300+
$sut = new Response();
301+
self::assertFalse($sut->redirected);
302+
}
303+
304+
public function testPropGetRedirected_redirect():void {
305+
$curl = self::createMock(Curl::class);
306+
$curl->expects(self::exactly(1))
307+
->method("getInfo")
308+
->with(CURLINFO_REDIRECT_COUNT)
309+
->willReturn(5);
310+
$sut = new Response(200, curl: $curl);
311+
self::assertTrue($sut->redirected);
312+
}
313+
314+
public function testPropGetRedirected_noRedirect():void {
315+
$curl = self::createMock(Curl::class);
316+
$curl->expects(self::exactly(1))
317+
->method("getInfo")
318+
->with(CURLINFO_REDIRECT_COUNT)
319+
->willReturn(0);
320+
$sut = new Response(200, curl: $curl);
321+
self::assertFalse($sut->redirected);
322+
}
297323
}

0 commit comments

Comments
 (0)