X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2Fatompub%2Fatompub_test.php;h=4259c0cdc701dde222477542b0e59448e3395142;hb=e8d1bb25469fe1ef0cbcb32c3022010997aca4b0;hp=389fd12c41fdf44bad0cf49e75b2ed80da8d1d8e;hpb=0bfeaa45594121fb89b88e4890fb2a1288a51ba6;p=quix0rs-gnu-social.git diff --git a/tests/atompub/atompub_test.php b/tests/atompub/atompub_test.php index 389fd12c41..4259c0cdc7 100644 --- a/tests/atompub/atompub_test.php +++ b/tests/atompub/atompub_test.php @@ -37,159 +37,7 @@ Options: END_OF_HELP; -require_once INSTALLDIR.'/scripts/commandline.inc'; - -class AtomPubClient -{ - public $url; - private $user, $pass; - - /** - * - * @param string $url collection feed URL - * @param string $user auth username - * @param string $pass auth password - */ - function __construct($url, $user, $pass) - { - $this->url = $url; - $this->user = $user; - $this->pass = $pass; - } - - /** - * Set up an HTTPClient with auth for our resource. - * - * @param string $method - * @return HTTPClient - */ - private function httpClient($method='GET') - { - $client = new HTTPClient($this->url); - $client->setMethod($method); - $client->setAuth($this->user, $this->pass); - return $client; - } - - function get() - { - $client = $this->httpClient('GET'); - $response = $client->send(); - if ($response->isOk()) { - return $response->getBody(); - } else { - throw new Exception("Bogus return code: " . $response->getStatus() . ': ' . $response->getBody()); - } - } - - /** - * Create a new resource by POSTing it to the collection. - * If successful, will return the URL representing the - * canonical location of the new resource. Neat! - * - * @param string $data - * @param string $type defaults to Atom entry - * @return string URL to the created resource - * - * @throws exceptions on failure - */ - function post($data, $type='application/atom+xml;type=entry') - { - $client = $this->httpClient('POST'); - $client->setHeader('Content-Type', $type); - // optional Slug header not used in this case - $client->setBody($data); - $response = $client->send(); - - if ($response->getStatus() != '201') { - throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus() . ': ' . $response->getBody()); - } - $loc = $response->getHeader('Location'); - $contentLoc = $response->getHeader('Content-Location'); - - if (empty($loc)) { - throw new Exception("AtomPub POST response missing Location header."); - } - if (!empty($contentLoc)) { - if ($loc != $contentLoc) { - throw new Exception("AtomPub POST response Location and Content-Location headers do not match."); - } - - // If Content-Location and Location match, that means the response - // body is safe to interpret as the resource itself. - if ($type == 'application/atom+xml;type=entry') { - self::validateAtomEntry($response->getBody()); - } - } - - return $loc; - } - - /** - * Note that StatusNet currently doesn't allow PUT editing on notices. - * - * @param string $data - * @param string $type defaults to Atom entry - * @return true on success - * - * @throws exceptions on failure - */ - function put($data, $type='application/atom+xml;type=entry') - { - $client = $this->httpClient('PUT'); - $client->setHeader('Content-Type', $type); - $client->setBody($data); - $response = $client->send(); - - if ($response->getStatus() != '200' && $response->getStatus() != '204') { - throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus() . ': ' . $response->getBody()); - } - - return true; - } - - /** - * Delete the resource. - * - * @return true on success - * - * @throws exceptions on failure - */ - function delete() - { - $client = $this->httpClient('GET'); - $client->setBody($data); - $response = $client->send(); - - if ($response->getStatus() != '200' && $response->getStatus() != '204') { - throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus() . ': ' . $response->getBody()); - } - - return true; - } - - /** - * Ensure that the given string is a parseable Atom entry. - * - * @param string $str - * @return boolean - * @throws Exception on invalid input - */ - static function validateAtomEntry($str) - { - if (empty($str)) { - throw new Exception('Bad Atom entry: empty'); - } - $dom = new DOMDocument; - if (!$dom->loadXML($str)) { - throw new Exception('Bad Atom entry: XML is not well formed.'); - } - - $activity = new Activity($dom->documentRoot); - return true; - } -} - +require_once INSTALLDIR.'/scripts/commandline.inc.php'; $user = get_option_value('n', 'nickname'); $pass = get_option_value('p', 'password'); @@ -254,19 +102,28 @@ $body = $notice->get(); AtomPubClient::validateAtomEntry($body); echo "ok\n"; -echo "Confirming new entry looks right... "; -// confirm that it actually is what we expected -// confirm it has an edit URL that matches $target -echo "NYI\n"; +echo "Getting the notice ID URI... "; +$noticeUri = AtomPubClient::entryId($body); +echo "ok: $noticeUri\n"; + +echo "Confirming new entry points to itself right... "; +$editUrl = AtomPubClient::entryEditURL($body); +if ($editUrl != $noticeUrl) { + die("Entry lists edit URL as $editUrl, no match!\n"); +} +echo "OK\n"; echo "Refetching the collection... "; $feed = $collection->get(); echo "ok\n"; echo "Confirming new entry is in the feed... "; -// make sure the new entry is in there +$entry = AtomPubClient::getEntryInFeed($feed, $noticeUri); +if (!$entry) { + die("missing!\n"); +} // edit URL should match -echo "NYI\n"; +echo "ok\n"; echo "Editing notice (should fail)... "; try { @@ -294,7 +151,11 @@ $feed = $collection->get(); echo "ok\n"; echo "Confirming deleted notice is no longer in the feed... "; -echo "NYI\n"; +$entry = AtomPubClient::getEntryInFeed($feed, $noticeUri); +if ($entry) { + die("still there!\n"); +} +echo "ok\n"; // make subscriptions // make some posts