forked from cachethq/cachet
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set HTTP timeouts and error handling on latest release lookup #6
- Loading branch information
Showing
5 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace CachetHQ\Tests\Cachet\Integrations\GitHub\Releases; | ||
|
||
use CachetHQ\Tests\Cachet\AbstractTestCase; | ||
use Illuminate\Support\Facades\Cache; | ||
use CachetHQ\Cachet\Integrations\GitHub\Releases; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
|
||
class ReleasesTest extends AbstractTestCase | ||
{ | ||
/** | ||
* Creates Mock Guzzle client | ||
* | ||
* @param array $mock_responses list of fake response to return to HTTP requests | ||
*/ | ||
private function createMockClient(array $mock_responses) | ||
{ | ||
$mock = new MockHandler($mock_responses); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handlerStack]); | ||
$this->app->instance(Client::class, $client); | ||
} | ||
|
||
public function test_latest_parses_response_from_github() | ||
{ | ||
$file = dirname(__FILE__) . '/../payloads/github_api.json'; | ||
$payload = file_get_contents($file); | ||
|
||
$mock_responses = [ | ||
new Response(200, [], $payload), | ||
]; | ||
$this->createMockClient($mock_responses); | ||
|
||
Cache::tags('release.latest')->flush(); | ||
$latest = $this->app->make(Releases::class)->latest(); | ||
$this->assertEquals('v2.3.18', $latest['tag_name']); | ||
} | ||
|
||
public function test_latest_returns_empty_on_timeout() | ||
{ | ||
$mock_responses = [ | ||
new ConnectException('cURL error 28: Connection timed out after 5000 milliseconds', new Request('GET', 'repos/cachethq/cachet/releases/latest')), | ||
]; | ||
$this->createMockClient($mock_responses); | ||
|
||
Cache::tags('release.latest')->flush(); | ||
$latest = $this->app->make(Releases::class)->latest(); | ||
$this->assertEquals(0, count($latest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"url": "https://api.github.com/repos/CachetHQ/Cachet/releases/17514684", | ||
"assets_url": "https://api.github.com/repos/CachetHQ/Cachet/releases/17514684/assets", | ||
"upload_url": "https://uploads.github.com/repos/CachetHQ/Cachet/releases/17514684/assets{?name,label}", | ||
"html_url": "https://github.com/CachetHQ/Cachet/releases/tag/v2.3.18", | ||
"id": 17514684, | ||
"author": { | ||
"login": "GrahamCampbell", | ||
"id": 2829600, | ||
"node_id": "MDQ6VXNlcjI4Mjk2MDA=", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/2829600?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/GrahamCampbell", | ||
"html_url": "https://github.com/GrahamCampbell", | ||
"followers_url": "https://api.github.com/users/GrahamCampbell/followers", | ||
"following_url": "https://api.github.com/users/GrahamCampbell/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/GrahamCampbell/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/GrahamCampbell/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/GrahamCampbell/subscriptions", | ||
"organizations_url": "https://api.github.com/users/GrahamCampbell/orgs", | ||
"repos_url": "https://api.github.com/users/GrahamCampbell/repos", | ||
"events_url": "https://api.github.com/users/GrahamCampbell/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/GrahamCampbell/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"node_id": "MDc6UmVsZWFzZTE3NTE0Njg0", | ||
"tag_name": "v2.3.18", | ||
"target_commitish": "2.4", | ||
"name": "v2.3.18", | ||
"draft": false, | ||
"prerelease": false, | ||
"created_at": "2019-05-14T12:27:19Z", | ||
"published_at": "2019-05-22T12:43:04Z", | ||
"assets": [ | ||
|
||
], | ||
"tarball_url": "https://api.github.com/repos/CachetHQ/Cachet/tarball/v2.3.18", | ||
"zipball_url": "https://api.github.com/repos/CachetHQ/Cachet/zipball/v2.3.18", | ||
"body": "" | ||
} |