Codebase list libatombus-perl / c6a0e0f
Merge pull request #1 from jonassmedegaard/master Adding resource for individual entries. Adding links. Thanks jonassmedegaard. Naveed Massjouni 12 years ago
3 changed file(s) with 60 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
1818 if (not $deployed++) {
1919 eval { schema->deploy }; # Fails gracefully if tables already exist.
2020 }
21 };
22
23 get '/feeds/:feed_title/entries/:entry_id' => sub {
24 my $entry_id = 'urn:uuid:' . params->{entry_id};
25 my $db_entry = schema->resultset('AtomBusEntry')->find({id => $entry_id});
26 return send_error("No such message exists", 404)
27 unless $db_entry;
28 my $if_none_match = request->header('If-None-Match');
29 #TODO: support revised entries (i.e. Atompub PUT)
30 # my $revision_id = $db_entry->revision_id || $db_entry->id;
31 my $revision_id = $db_entry->id;
32
33 # If ETag matches current revision id of entry
34 if (my $id = $if_none_match) {
35 $id =~ s/^"(.*)"$/$1/; # Remove surrounding quotes
36 if ($revision_id eq $id) {
37 status 304;
38 return '';
39 }
40 }
41
42 my $entry = _entry_from_db($db_entry);
43
44 _add_etag($revision_id);
45 header Vary => 'If-None-Match';
46
47 return $entry->as_xml;
2148 };
2249
2350 get '/feeds/:feed_title' => sub {
111138 updated => $updated,
112139 });
113140 $db_feed->update({updated => $updated});
114 _add_etag($db_entry->id);
115 return _entry_from_db($db_entry)->as_xml;
141 $entry = _entry_from_db($db_entry);
142 _add_etag($entry->id);
143 header Location => $entry->link->href;
144 content_type 'application/atom+xml;type=entry';
145 status 'created';
146 return $entry->as_xml;
116147 };
117148
118149 sub _gen_id { 'urn:uuid:' . create_UUID_as_string() }
150
151 sub _id_nss { $_ = shift; s/^urn:uuid://; return $_ }
119152
120153 sub _entry_from_db {
121154 my $row = shift;
124157 $entry->content($row->content);
125158 $entry->id($row->id);
126159 $entry->updated($row->updated);
160 my $self_link = XML::Atom::Link->new;
161 $self_link->rel('self');
162 $self_link->type('application/atom+xml');
163 $self_link->href( join( '/', uri_for('/feeds'), $row->feed_title->title, 'entries', _id_nss($row->id) ));
164 $entry->add_link($self_link);
127165 return $entry;
128166 }
129167
2929 };
3030
3131 my $res = dancer_response POST => "/feeds/$feed", { body => $xml1 };
32 is $res->{status} => 200, 'Got 200 for posting entry1.';
32 is $res->{status} => 201, 'Got 201 for posting entry1.';
3333
3434 is schema->resultset('AtomBusEntry')->count() => 1, '1 entries in db.';
3535 is schema->resultset('AtomBusFeed')->count() => 1, '1 feed in db.';
3939 ok $entry1, 'Found entry 1.';
4040
4141 $res = dancer_response POST => "/feeds/$feed", { body => $xml2 };
42 is $res->{status} => 200, 'Got 200 for posting entry2.';
42 is $res->{status} => 201, 'Got 201 for posting entry2.';
4343
4444 is schema->resultset('AtomBusEntry')->count() => 2, '2 entries in db.';
4545 is schema->resultset('AtomBusFeed')->count() => 1, '1 feed in db.';
0 use Test::More import => ['!pass'], tests => 17;
0 use Test::More import => ['!pass'], tests => 21;
11 use Dancer qw(:syntax);
22 use Dancer::Test;
33
4 use URI;
45 use XML::XPath;
56 use Dancer::Plugin::DBIC qw(schema);
67 use AtomBus;
2627 response_status_is [ GET => "/feeds/foo" ], 404;
2728 };
2829
29 foreach my $i (1 .. 10) {
30 # Create one entry and examine result.
31 my $res = dancer_response POST => "/feeds/foo", { body => sprintf($xml, 1, 1) };
32 is $res->{status}=> 201, 'Status was 201';
33 my $etag = new URI($res->{headers}->{etag}, 'urn');
34 my $location = new URI($res->{headers}->{location}, 'http');
35 my $id_nss = $location;
36 ($id_nss) =~ s,^.*/,,;
37 is $location->path => "/feeds/foo/entries/$id_nss", 'Location header is well-structured.';
38 is $etag->as_string => "urn:uuid:$id_nss", 'ETag was contained in location header.';
39 $res = dancer_response GET => $location->path;
40 is $res->{status}=> 200, 'Entry was GETtable with status 200';
41
42 # Create additional entries.
43 foreach my $i (2 .. 10) {
3044 dancer_response POST => "/feeds/foo", { body => sprintf($xml, $i, $i) };
3145 }
32 my $res = dancer_response GET => "/feeds/foo";
46
47 $res = dancer_response GET => "/feeds/foo";
3348 my $xp = XML::XPath->new(xml => $res->{content});
3449 my @entries = $xp->findnodes('/feed/entry');
3550 is $res->{status}=> 200, 'Status was 200';