Skip to content

Commit

Permalink
Merge pull request #16 from dmason30/response-on-failure
Browse files Browse the repository at this point in the history
Set response field on request failure
  • Loading branch information
freekmurze authored Sep 4, 2019
2 parents 0686656 + ff691db commit 7974547
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/CallWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
Expand Down Expand Up @@ -78,6 +79,10 @@ public function handle()

return;
} catch (Exception $exception) {
if ($exception instanceof RequestException) {
$this->response = $exception->getResponse();
}

/** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */
$backoffStrategy = app($this->backoffStrategyClass);

Expand Down
14 changes: 14 additions & 0 deletions tests/CallWebhookJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ public function by_default_it_will_retry_3_times_with_the_exponential_backoff_st
$this->testClient->assertRequestCount(3);
}

/** @test */
public function it_sets_the_response_field_on_request_failure()
{
$this->testClient->throwRequestException();

$this->baseWebhook()->dispatch();

$this->artisan('queue:work --once');
Event::assertDispatched(WebhookCallFailedEvent::class, function (WebhookCallFailedEvent $event) {
$this->assertNotNull($event->response);
return true;
});
}

protected function baseWebhook(): WebhookCall
{
return WebhookCall::create()
Expand Down
17 changes: 17 additions & 0 deletions tests/TestClasses/TestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Spatie\WebhookServer\Tests\TestClasses;

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Assert;

Expand All @@ -11,10 +13,20 @@ class TestClient

private $useResponseCode = 200;

private $throwRequestException = false;

public function request(string $method, string $url, array $options)
{
$this->requests[] = compact('method', 'url', 'options');

if ($this->throwRequestException) {
throw new RequestException(
'Request failed exception',
new Request($method, $url),
new Response(500)
);
}

return new Response($this->useResponseCode);
}

Expand All @@ -40,4 +52,9 @@ public function letEveryRequestFail()
{
$this->useResponseCode = 500;
}

public function throwRequestException()
{
$this->throwRequestException = true;
}
}

0 comments on commit 7974547

Please sign in to comment.