Browse Source

Last tests for HTTP links

master
J. King 4 years ago
parent
commit
d13cee90ee
  1. 15
      lib/Parser/HTTP/Message.php
  2. 2
      tests/cases/AbstractParserTestCase.php
  3. 26
      tests/cases/HTTP/http.yaml

15
lib/Parser/HTTP/Message.php

@ -149,17 +149,17 @@ class Message {
public function getLinks(): LinkCollection {
$out = new LinkCollection;
foreach ($this->parseHeader("Link", self::LINK_PATTERN, true) ?? [] as $h) {
if ($p = $this->parseParams($h[2])) {
$l = new Link;
$l->url = Url::fromString($h[1], $this->url);
$l = new Link;
$l->url = Url::fromString($h[1], $this->url);
if ($l->url && $p = $this->parseParams($h[2])) {
// normalize and deduplicate relations
$relations = ['f' => $this->normalizeRelations($p['rel'] ?? ""), 'r' => $this->normalizeRelations($p['rev'] ?? "")];
if (!$l->url || (!$relations['f'] && !$relations['r'])) {
// if there are no relations or the URL is invalid, skip this link
if (!$relations['f'] && !$relations['r']) {
// if there are no relations, skip this link
continue;
}
// build the link object with everything except the relation
$l->anchor = Url::fromString($p['anchor'] ?? "", $this->url);
$l->anchor = isset($p['anchor']) ? Url::fromString($p['anchor'], $this->url) : null;
$l->type = MimeType::parse($p['type'] ?? "");
foreach (['title' => "title", 'media' => "media", 'hreflang' => "lang"] as $src => $dst) {
$l->$dst = isset($p[$src]) ? $p[$src] : null;
@ -180,7 +180,8 @@ class Message {
$i->rel = $r;
$i->rev = true;
$out[] = $i;
}
}
// TODO: deduplicate results, maybe
}
}
return $out;

2
tests/cases/AbstractParserTestCase.php

@ -225,6 +225,8 @@ class AbstractParserTestCase extends \PHPUnit\Framework\TestCase {
foreach ((array) $link as $k => $v) {
if ($k === "type") {
$m->$k = MimeType::parse($v);
} elseif ($k === "attr") {
$m->$k = (array) $v;
} elseif (in_array($k, ["url", "anchor"])) {
$m->$k = $this->makeUrl($v);
} else {

26
tests/cases/HTTP/http.yaml

@ -124,16 +124,36 @@ Links:
doc_url: 'http://example.org/blah'
input:
head:
Link: '<http://example.com/>; rel="alternate"; type="appplication/atom+xml; charset=\"UTF-8\";", </>; rel=alternate; type="application/rss+xml"'
Link:
- '<http://example.com/>; rel="alternate"; type="appplication/atom+xml; charset=\"UTF-8\";", </>; rel=alternate; type="application/rss+xml"'
- '</favicon.ico>; rel="shortcut icon"'
- '<http://example.net/>;rev=help;rel=up'
- '<http://exampple.biz/>; rel="http://example.com/help" ; anchor="#ook" ; hreflang=en ; media=print ; title="Title!" ; title*="multilingual titles" ; ext="Extended Attribute"'
- '<http://[/>; rel="invalid URL"'
- '<>; rel= ; x="no relation"'
output:
meta:
url: 'http://example.org/blah'
links:
- url: 'http://example.com/'
anchor: 'http://example.org/blah'
rel: alternate
type: 'appplication/atom+xml;charset=UTF-8'
- url: ['/', 'http://example.org/blah']
anchor: 'http://example.org/blah'
rel: alternate
type: 'application/rss+xml'
- url: ['/favicon.ico', 'http://example.org/blah']
rel: shortcut
- url: ['/favicon.ico', 'http://example.org/blah']
rel: icon
- url: 'http://example.net/'
rel: up
- url: 'http://example.net/'
rel: help
rev: true
- url: 'http://exampple.biz/'
rel: 'http://example.com/help'
anchor: ['#ook', 'http://example.org/blah']
lang: en
media: print
title: 'Title!'
attr: {title*: 'multilingual titles', ext: 'Extended Attribute'}

Loading…
Cancel
Save