4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2010, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
23 $shortoptions = 'n:p:';
24 $longoptions = array('nickname=', 'password=', 'dry-run');
26 $helptext = <<<END_OF_HELP
27 USAGE: atompub_test.php [options]
29 Runs some tests on the AtomPub interface for the site. You must provide
30 a user account to authenticate as; it will be used to make some test
34 -n<user> --nickname=<user> Nickname of account to post as
35 -p<pass> --password=<pass> Password for account
36 --dry-run Skip tests that modify the site (post, delete)
40 require_once INSTALLDIR.'/scripts/commandline.inc';
49 * @param string $url collection feed URL
50 * @param string $user auth username
51 * @param string $pass auth password
53 function __construct($url, $user, $pass)
61 * Set up an HTTPClient with auth for our resource.
63 * @param string $method
66 private function httpClient($method='GET')
68 $client = new HTTPClient($this->url);
69 $client->setMethod($method);
70 $client->setAuth($this->user, $this->pass);
76 $client = $this->httpClient('GET');
77 $response = $client->send();
78 if ($response->isOk()) {
79 return $response->getBody();
81 throw new Exception("Bogus return code: " . $response->getStatus() . ': ' . $response->getBody());
86 * Create a new resource by POSTing it to the collection.
87 * If successful, will return the URL representing the
88 * canonical location of the new resource. Neat!
91 * @param string $type defaults to Atom entry
92 * @return string URL to the created resource
94 * @throws exceptions on failure
96 function post($data, $type='application/atom+xml;type=entry')
98 $client = $this->httpClient('POST');
99 $client->setHeader('Content-Type', $type);
100 // optional Slug header not used in this case
101 $client->setBody($data);
102 $response = $client->send();
104 if ($response->getStatus() != '201') {
105 throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus() . ': ' . $response->getBody());
107 $loc = $response->getHeader('Location');
108 $contentLoc = $response->getHeader('Content-Location');
111 throw new Exception("AtomPub POST response missing Location header.");
113 if (!empty($contentLoc)) {
114 if ($loc != $contentLoc) {
115 throw new Exception("AtomPub POST response Location and Content-Location headers do not match.");
118 // If Content-Location and Location match, that means the response
119 // body is safe to interpret as the resource itself.
120 if ($type == 'application/atom+xml;type=entry') {
121 self::validateAtomEntry($response->getBody());
129 * Note that StatusNet currently doesn't allow PUT editing on notices.
131 * @param string $data
132 * @param string $type defaults to Atom entry
133 * @return true on success
135 * @throws exceptions on failure
137 function put($data, $type='application/atom+xml;type=entry')
139 $client = $this->httpClient('PUT');
140 $client->setHeader('Content-Type', $type);
141 $client->setBody($data);
142 $response = $client->send();
144 if ($response->getStatus() != '200' && $response->getStatus() != '204') {
145 throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus() . ': ' . $response->getBody());
152 * Delete the resource.
154 * @return true on success
156 * @throws exceptions on failure
160 $client = $this->httpClient('DELETE');
161 $client->setBody($data);
162 $response = $client->send();
164 if ($response->getStatus() != '200' && $response->getStatus() != '204') {
165 throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus() . ': ' . $response->getBody());
172 * Ensure that the given string is a parseable Atom entry.
176 * @throws Exception on invalid input
178 static function validateAtomEntry($str)
181 throw new Exception('Bad Atom entry: empty');
183 $dom = new DOMDocument;
184 if (!$dom->loadXML($str)) {
185 throw new Exception('Bad Atom entry: XML is not well formed.');
188 $activity = new Activity($dom->documentRoot);
194 $user = get_option_value('n', 'nickname');
195 $pass = get_option_value('p', 'password');
198 die("Must set a user: --nickname=<username>\n");
201 die("Must set a password: --password=<username>\n");
204 // discover the feed...
205 // @fixme will this actually work?
206 $url = common_local_url('ApiTimelineUser', array('format' => 'atom', 'id' => $user));
208 echo "Collection URL is: $url\n";
210 $collection = new AtomPubClient($url, $user, $pass);
212 // confirm the feed has edit links ..... ?
214 echo "Posting an empty message (should fail)... ";
216 $noticeUrl = $collection->post('');
217 die("FAILED, succeeded!\n");
218 } catch (Exception $e) {
222 echo "Posting an invalid XML message (should fail)... ";
224 $noticeUrl = $collection->post('<feed<entry>barf</yomomma>');
225 die("FAILED, succeeded!\n");
226 } catch (Exception $e) {
230 echo "Posting a valid XML but non-Atom message (should fail)... ";
232 $noticeUrl = $collection->post('<feed xmlns="http://notatom.com"><id>arf</id><entry><id>barf</id></entry></feed>');
233 die("FAILED, succeeded!\n");
234 } catch (Exception $e) {
239 $rand = mt_rand(0, 99999);
241 <entry xmlns="http://www.w3.org/2005/Atom">
242 <title>This is an AtomPub test post title ($rand)</title>
243 <content>This is an AtomPub test post content ($rand)</content>
247 echo "Posting a new message... ";
248 $noticeUrl = $collection->post($atom);
249 echo "ok, got $noticeUrl\n";
251 echo "Fetching the new notice... ";
252 $notice = new AtomPubClient($noticeUrl, $user, $pass);
253 $body = $notice->get();
254 AtomPubClient::validateAtomEntry($body);
257 echo "Confirming new entry looks right... ";
258 // confirm that it actually is what we expected
259 // confirm it has an edit URL that matches $target
262 echo "Refetching the collection... ";
263 $feed = $collection->get();
266 echo "Confirming new entry is in the feed... ";
267 // make sure the new entry is in there
268 // edit URL should match
271 echo "Editing notice (should fail)... ";
273 $notice->put($target, $atom2);
274 die("ERROR: editing a notice should have failed.\n");
275 } catch (Exception $e) {
276 echo "ok (failed as expected)\n";
279 echo "Deleting notice... ";
283 echo "Refetching deleted notice to confirm it's gone... ";
285 $body = $notice->get();
287 die("ERROR: notice should be gone now.\n");
288 } catch (Exception $e) {
292 echo "Refetching the collection.. ";
293 $feed = $collection->get();
296 echo "Confirming deleted notice is no longer in the feed... ";
299 // make subscriptions
301 // make sure the posts go through or not depending on the subs
302 // remove subscriptions
303 // test that they don't go through now
305 // group memberships too
310 // make sure we can't post to someone else's feed!
311 // make sure we can't delete someone else's messages
312 // make sure we can't create/delete someone else's subscriptions
313 // make sure we can't create/delete someone else's group memberships