Skip to content

Commit

Permalink
Add default timeout to NotPwnedVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrenaud committed May 20, 2021
1 parent 31c58e5 commit cb5ffc6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Illuminate/Validation/NotPwnedVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ class NotPwnedVerifier implements UncompromisedVerifier
*/
protected $factory;

/**
* The number of seconds the request can run before timing out.
*
* @var int|null
*/
public $timeout;

/**
* Create a new uncompromised verifier.
*
* @param \Illuminate\Http\Client\Factory $factory
* @param int|null $timeout
* @return void
*/
public function __construct($factory)
public function __construct($factory, $timeout = null)
{
$this->factory = $factory;
$this->timeout = $timeout ?? 15;
}

/**
Expand Down Expand Up @@ -77,7 +86,7 @@ protected function search($hashPrefix)
try {
$response = $this->factory->withHeaders([
'Add-Padding' => true,
])->get(
])->timeout($this->timeout)->get(
'https://api.pwnedpasswords.com/range/'.$hashPrefix
);
} catch (Exception $e) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Validation/ValidationNotPwnedVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public function testApiResponseGoesWrong()
->with(['Add-Padding' => true])
->andReturn($httpFactory);

$httpFactory
->shouldReceive('timeout')
->once()
->with(15)
->andReturn($httpFactory);

$httpFactory->shouldReceive('get')
->once()
->andReturn($response);
Expand Down Expand Up @@ -77,6 +83,12 @@ public function testApiGoesDown()
->with(['Add-Padding' => true])
->andReturn($httpFactory);

$httpFactory
->shouldReceive('timeout')
->once()
->with(15)
->andReturn($httpFactory);

$httpFactory->shouldReceive('get')
->once()
->andReturn($response);
Expand Down Expand Up @@ -112,6 +124,12 @@ public function testDnsDown()
->with(['Add-Padding' => true])
->andReturn($httpFactory);

$httpFactory
->shouldReceive('timeout')
->once()
->with(15)
->andReturn($httpFactory);

$httpFactory
->shouldReceive('get')
->once()
Expand Down

0 comments on commit cb5ffc6

Please sign in to comment.