Skip to content

Commit

Permalink
Merge pull request #10 from curiousyigit/handle-unreachable-hosts
Browse files Browse the repository at this point in the history
Handle unreachable hosts
  • Loading branch information
huzaifaarain authored Jul 2, 2024
2 parents b6bec57 + 4fe7c2d commit cc0925b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/TelescopeGuzzleRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class TelescopeGuzzleRecorder

private Request $request;

private Response $response;
private ?Response $response;

public function __construct(TransferStats $transferStats)
{
$this->transferStats = $transferStats;
$this->request = new Request($transferStats->getRequest());
$this->response = new Response($transferStats->getResponse());

$transferStatsResponse = $transferStats->getResponse();
$this->response = $transferStatsResponse ? new Response($transferStatsResponse) : null;
}

public static function recordGuzzleRequest(TransferStats $transferStats)
Expand All @@ -44,9 +46,9 @@ private function recordRequest()
['query_string' => $this->request->queryString()],
['payload' => $this->payload($this->input($this->request))]
),
'response_status' => $this->response->status(),
'response_headers' => $this->headers($this->response->headers()),
'response' => $this->response($this->response),
'response_status' => $this->response ? $this->response->status() : null,
'response_headers' => $this->response ? $this->headers($this->response->headers()) : null,
'response' => $this->response ? $this->response($this->response) : null,
'duration' => $this->duration(),
]);

Expand Down
23 changes: 23 additions & 0 deletions tests/Watchers/TelescopeGuzzleWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MuhammadHuzaifa\TelescopeGuzzleWatcher\Tests\Watchers;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use Laravel\Telescope\EntryType;
use MuhammadHuzaifa\TelescopeGuzzleWatcher\Tests\TestCase;
use MuhammadHuzaifa\TelescopeGuzzleWatcher\Watchers\TelescopeGuzzleWatcher;
Expand Down Expand Up @@ -41,4 +42,26 @@ public function it_should_intercept_and_log_request()
$this->assertSame('GET', $entry->content['method']);
$this->assertSame('https://www.google.com', $entry->content['uri']);
}

/**
* @test
*/
public function it_should_handle_unreachable_hosts()
{
$client = app(Client::class);
try {
$client->get('http://host.does.not.exist');
} catch (\Throwable $th) {
if (! $th instanceof ConnectException) {
report($th);
}
}

$entry = $this->loadTelescopeEntries()->first();

$this->assertNotNull($entry);
$this->assertSame(EntryType::CLIENT_REQUEST, $entry->type);
$this->assertSame('GET', $entry->content['method']);
$this->assertSame('http://host.does.not.exist', $entry->content['uri']);
}
}

0 comments on commit cc0925b

Please sign in to comment.