@@ -165,6 +165,31 @@ public function testInvalidPasswordCustomEndpoint()
165165 ->assertRaised ();
166166 }
167167
168+ public function testEndpointWithInvalidValueInReturn ()
169+ {
170+ $ returnValue = implode (
171+ "\r\n" ,
172+ [
173+ '36039744C253F9B2A4E90CBEDB02EBFB82D:5 ' ,
174+ 'This should not break the validator ' ,
175+ '3686792BBC66A72D40D928ED15621124CFE:7 ' ,
176+ '36EEC709091B810AA240179A44317ED415C:2 ' ,
177+ '' ,
178+ ]
179+ );
180+
181+ $ validator = new NotCompromisedPasswordValidator (
182+ $ this ->createHttpClientStub ($ returnValue ),
183+ 'UTF-8 ' ,
184+ true ,
185+ 'https://password-check.internal.example.com/range/%s '
186+ );
187+
188+ $ validator ->validate (self ::PASSWORD_NOT_LEAKED , new NotCompromisedPassword ());
189+
190+ $ this ->assertNoViolation ();
191+ }
192+
168193 public function testInvalidConstraint ()
169194 {
170195 $ this ->expectException (UnexpectedTypeException::class);
@@ -202,11 +227,11 @@ public function provideErrorSkippingConstraints(): iterable
202227 }
203228 }
204229
205- private function createHttpClientStub (): HttpClientInterface
230+ private function createHttpClientStub (? string $ returnValue = null ): HttpClientInterface
206231 {
207232 $ httpClientStub = $ this ->createMock (HttpClientInterface::class);
208233 $ httpClientStub ->method ('request ' )->willReturnCallback (
209- function (string $ method , string $ url ): ResponseInterface {
234+ function (string $ method , string $ url ) use ( $ returnValue ) : ResponseInterface {
210235 if (self ::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $ url ) {
211236 throw new class ('Problem contacting the Have I been Pwned API. ' ) extends \Exception implements ServerExceptionInterface {
212237 public function getResponse (): ResponseInterface
@@ -219,7 +244,7 @@ public function getResponse(): ResponseInterface
219244 $ responseStub = $ this ->createMock (ResponseInterface::class);
220245 $ responseStub
221246 ->method ('getContent ' )
222- ->willReturn (implode ("\r\n" , self ::RETURN ));
247+ ->willReturn ($ returnValue ?? implode ("\r\n" , self ::RETURN ));
223248
224249 return $ responseStub ;
225250 }
0 commit comments