Browse Source

handle the case where the server returns multiple content-type headers

pull/78/head v1.6.7
Aaron Parecki 6 years ago
parent
commit
43db6098fc
3 changed files with 37 additions and 3 deletions
  1. +11
    -3
      lib/XRay/Fetcher.php
  2. +11
    -0
      tests/ParseTest.php
  3. +15
    -0
      tests/data/source.example.com/multiple-content-type

+ 11
- 3
lib/XRay/Fetcher.php View File

@ -89,9 +89,17 @@ class Fetcher {
// Show an error if the content type returned is not a recognized type // Show an error if the content type returned is not a recognized type
$format = null; $format = null;
if(isset($result['headers']['Content-Type']) && is_string($result['headers']['Content-Type'])) {
$type = new MediaType($result['headers']['Content-Type']);
$format = $type->format;
if(isset($result['headers']['Content-Type'])) {
$contentType = null;
if(is_array($result['headers']['Content-Type'])) {
$contentType = $result['headers']['Content-Type'][0];
} elseif(is_string($result['headers']['Content-Type'])) {
$contentType = $result['headers']['Content-Type'];
}
if($contentType) {
$type = new MediaType($contentType);
$format = $type->format;
}
} }
if(!$format || if(!$format ||

+ 11
- 0
tests/ParseTest.php View File

@ -940,4 +940,15 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('XRay should use this content since the JSON in the rel-alternate is invalid', $data['data']['content']['text']); $this->assertEquals('XRay should use this content since the JSON in the rel-alternate is invalid', $data['data']['content']['text']);
} }
public function testMultipleContentTypeHeaders() {
$url = 'http://source.example.com/multiple-content-type';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('mf2+html', $data['source-format']);
}
} }

+ 15
- 0
tests/data/source.example.com/multiple-content-type View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Content-Type: text/html
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="p-content">This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b> but is in a p-content element so is plaintext.</p>
</body>
</html>

Loading…
Cancel
Save