Skip to content

Commit

Permalink
Expose HttpStatusCode for urls when searching canonically
Browse files Browse the repository at this point in the history
  • Loading branch information
willwashburn committed Nov 19, 2019
1 parent f7960d6 commit 5f372b0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ Use composer

```composer require willwashburn/mushroom```

Alternatively, add ```"willwashburn/mushroom": "~2.10"``` to your composer.json
Alternatively, add ```"willwashburn/mushroom": "~2.11"``` to your composer.json

## Change Log
- v2.11.0 - Expose HttpStatusCode for urls when searching canonically
- v2.10.0 - Add method to add a domain to JS Redirect Domains array
- v2.9.1 - Fix bug where some html sources were not cached
- v2.9.0 - Expose HTML for urls when searching canonically
Expand Down
50 changes: 41 additions & 9 deletions src/mushroom/Mushroom.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class Mushroom
*/
private $html = [];

/**
* Some user-land use cases popped up that required the response_code of the
* eventual curl request we made when finding canonical urls. We expose a
* method to get this content in the event someone needs it.
*
* @var int[]
*/
private $httpStatusCode = [];

/**
* Mushroom constructor.
*
Expand Down Expand Up @@ -147,6 +156,22 @@ public function getCachedHtml($url)
return false;
}


/**
* We occasionally can find use in knowing the http status code
*
* @param $url
*
* @return bool
*/
public function getCachedHttpStatusCode($url)
{
if ($this->httpStatusCode[$url]) {
return $this->httpStatusCode[$url];
}
return false;
}

/**
* @param array $urls The urls to unfurl
* @param array $curlOptions Custom curl options that can be passed in
Expand Down Expand Up @@ -202,6 +227,10 @@ private function batchFollow($urls, $curlOptions, $followHttpRefresh, $findCanon
if ($url['html']) {
$this->html[$urls[$key]] = $url['html'];
}

if ($url['http_status_code']) {
$this->httpStatusCode[$urls[$key]] = $url['http_status_code'];
}
}

if ($followHttpRefresh && count($retries) > 0) {
Expand Down Expand Up @@ -342,9 +371,10 @@ private function getUrlFromHandle($ch, $followHttpRefresh, $findCanonical)
$url = $this->ensureSchemeAndHost($ch, $url);

return [
'refresh' => true,
'url' => $url,
'html' => $html,
'refresh' => true,
'url' => $url,
'html' => $html,
'http_status_code' => $httpStatusCode,
];
}
}
Expand All @@ -366,17 +396,19 @@ private function getUrlFromHandle($ch, $followHttpRefresh, $findCanonical)
$url = $this->ensureSchemeAndHost($ch, $url);

return [
'refresh' => false,
'url' => $url,
'html' => $html
'refresh' => false,
'url' => $url,
'html' => $html,
'http_status_code' => $httpStatusCode,
];
}
}

return [
'refresh' => false,
'url' => $effectiveUrl,
'html' => $html,
'refresh' => false,
'url' => $effectiveUrl,
'html' => $html,
'http_status_code' => $httpStatusCode,
];
}

Expand Down
16 changes: 9 additions & 7 deletions tests/ExpandLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public function test_setting_curl_options_works()
->shouldReceive('curl_getinfo')->getMock()
->shouldReceive('curl_setopt_array')
->with(M::any(), M::on(function ($arg) use ($expected_curl_opts) {
foreach ( array_keys($expected_curl_opts) as $key ) {
if ( $arg[$key] != $expected_curl_opts[$key] ) {
return false;
}
}
foreach (array_keys($expected_curl_opts) as $key) {
if ($arg[$key] != $expected_curl_opts[$key]) {
return false;
}
}

return true;
}))
Expand All @@ -151,7 +151,7 @@ public function test_without_follow_http_redirects()
];

$mushroom = new Mushroom();
foreach ( $links as list( $link, $expected_result ) ) {
foreach ($links as list( $link, $expected_result )) {
$result = $mushroom->expand($link, [], false);

$this->assertEquals($expected_result, $result);
Expand All @@ -164,15 +164,17 @@ public function test_get_html_works()
['http://bit.ly/1bdDlXc', 'https://www.google.com/'],
['http://www.tailwindapp.com', 'https://www.tailwindapp.com/'],
['https://diply.com/article/auntyacid/pinterest-diy-easy-solutions', 'https://diply.com/article/auntyacid/pinterest-diy-easy-solutions'],
['https://www.zazzle.com/cookie_monster_cookies_for_santa_dinner_plate-115773106232089655', 'https://www.zazzle.com/cookie_monster_cookies_for_santa_dinner_plate-115773106232089655'],
];

$mushroom = new Mushroom();
foreach ( $links as list( $link, $expected_result ) ) {
foreach ($links as list( $link, $expected_result )) {
$result = $mushroom->canonical($link);

$this->assertEquals($expected_result, $result);

$this->assertNotFalse($mushroom->getCachedHtml($link), $link);
$this->assertNotEmpty($mushroom->getCachedHttpStatusCode($link), $link);
}

$this->assertFalse($mushroom->getCachedHtml('https://www.tailwindapp.com'));
Expand Down

0 comments on commit 5f372b0

Please sign in to comment.