99use Typesense \Exceptions \RequestMalformed ;
1010use Http \Client \Exception \HttpException ;
1111use Http \Client \Exception \TransferException ;
12+ use JsonException ;
1213use Psr \Http \Client \ClientInterface ;
1314use Psr \Http \Message \ResponseInterface ;
1415use Psr \Http \Message \StreamInterface ;
1516use Psr \Http \Message \RequestInterface ;
17+ use Typesense \Exceptions \TypesenseClientError ;
1618
1719class ApiCallRetryTest extends TestCase
1820{
@@ -464,4 +466,38 @@ public function testDoesNotSleepOnFinalRetryAttempt(): void
464466 "Execution was too fast ( {$ actualDuration }s), suggesting sleep intervals were skipped "
465467 );
466468 }
467- }
469+
470+ public function testThrowsTypesenseClientErrorWhenSuccessResponseContainsInvalidJson (): void
471+ {
472+ $ httpClient = $ this ->createMock (ClientInterface::class);
473+ $ httpClient ->method ('sendRequest ' )
474+ ->willReturnCallback (function () {
475+ $ response = $ this ->createMock (ResponseInterface::class);
476+ $ response ->method ('getStatusCode ' )->willReturn (200 );
477+ $ stream = $ this ->createMock (StreamInterface::class);
478+ $ stream ->method ('getContents ' )->willReturn ('{invalid json ' );
479+ $ response ->method ('getBody ' )->willReturn ($ stream );
480+ return $ response ;
481+ });
482+
483+ $ config = new Configuration ([
484+ 'api_key ' => 'test-key ' ,
485+ 'nodes ' => [
486+ ['host ' => 'node1 ' , 'port ' => 8108 , 'protocol ' => 'http ' ]
487+ ],
488+ 'num_retries ' => 0 ,
489+ 'client ' => $ httpClient
490+ ]);
491+
492+ $ apiCall = new ApiCall ($ config );
493+
494+ try {
495+ $ apiCall ->get ('/test ' , []);
496+ $ this ->fail ('Expected TypesenseClientError to be thrown ' );
497+ } catch (TypesenseClientError $ exception ) {
498+ $ this ->assertStringContainsString ('HTTP 200 response is not valid JSON: ' , $ exception ->getMessage ());
499+ $ this ->assertStringContainsString ('{invalid json ' , $ exception ->getMessage ());
500+ $ this ->assertInstanceOf (JsonException::class, $ exception ->getPrevious ());
501+ }
502+ }
503+ }
0 commit comments