Browse Source

sends an Accept header when fetching posts

pull/78/head
Aaron Parecki 3 years ago
parent
commit
01cce9b823
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 108 additions and 3 deletions
  1. +24
    -3
      lib/XRay/Fetcher.php
  2. +40
    -0
      lib/XRay/MediaType.php
  3. +44
    -0
      tests/MediaTypeTest.php

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

@ -70,6 +70,9 @@ class Fetcher {
}
$headers = [];
$headers[] = 'Accept: text/html, application/json, application/xml, text/xml';
if(isset($opts['token']))
$headers[] = 'Authorization: Bearer ' . $opts['token'];
@ -84,16 +87,34 @@ class Fetcher {
];
}
// Show an error if the content type returned is not a recognized type
$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(!$format ||
!in_array($format, ['html', 'json', 'xml'])) {
return [
'error' => 'invalid_content',
'error_description' => 'The server did not return a recognized content type',
'content_type' => $result['headers']['Content-Type'],
'url' => $result['url'],
'code' => $result['code']
];
}
if(trim($result['body']) == '') {
if($result['code'] == 410) {
// 410 Gone responses are valid and should not return an error
return $this->respond($response, 200, [
return [
'data' => [
'type' => 'unknown'
'type' => 'deleted'
],
'url' => $result['url'],
'code' => $result['code']
]);
];
}
return [

+ 40
- 0
lib/XRay/MediaType.php View File

@ -0,0 +1,40 @@
<?php
namespace p3k\XRay;
class MediaType {
public $type;
public $subtype;
public $format;
public $charset;
// Parse a media type into component parts: type, subtype, format, charset
// e.g. "application/json" => type: application, subtype: json, format: json
// "application/ld+json" => type: application, subtype: "ld+json", format: json
public function __construct($string) {
if(strstr($string, ';')) {
list($type, $parameters) = explode(';', $string, 2);
$parameters = explode(';', $parameters);
foreach($parameters as $p) {
list($k, $v) = explode('=', trim($p));
if($k == 'charset')
$this->charset = $v;
}
} else {
$type = $string;
}
list($type, $subtype) = explode('/', $type);
$this->type = $type;
$this->subtype = $subtype;
$this->format = $subtype;
if(strstr($subtype, '+')) {
list($a, $b) = explode('+', $subtype, 2);
$this->format = $b;
}
}
}

+ 44
- 0
tests/MediaTypeTest.php View File

@ -0,0 +1,44 @@
<?php
class MediaTypeTest extends PHPUnit_Framework_TestCase {
public function testParseTextHtml() {
$type = new p3k\XRay\MediaType('text/html');
$this->assertEquals('text', $type->type);
$this->assertEquals('html', $type->subtype);
$this->assertEquals('html', $type->format);
$this->assertEquals(null, $type->charset);
}
public function testParseTextHtmlUtf8() {
$type = new p3k\XRay\MediaType('text/html; charset=UTF-8');
$this->assertEquals('text', $type->type);
$this->assertEquals('html', $type->subtype);
$this->assertEquals('html', $type->format);
$this->assertEquals('UTF-8', $type->charset);
}
public function testParseTextHtmlUtf8Extra() {
$type = new p3k\XRay\MediaType('text/html; hello=world; charset=UTF-8');
$this->assertEquals('text', $type->type);
$this->assertEquals('html', $type->subtype);
$this->assertEquals('html', $type->format);
$this->assertEquals('UTF-8', $type->charset);
}
public function testParseApplicationJson() {
$type = new p3k\XRay\MediaType('application/json');
$this->assertEquals('application', $type->type);
$this->assertEquals('json', $type->subtype);
$this->assertEquals('json', $type->format);
$this->assertEquals(null, $type->charset);
}
public function testParseApplicationJsonFeed() {
$type = new p3k\XRay\MediaType('application/feed+json');
$this->assertEquals('application', $type->type);
$this->assertEquals('feed+json', $type->subtype);
$this->assertEquals('json', $type->format);
$this->assertEquals(null, $type->charset);
}
}

Loading…
Cancel
Save