root/lang/perl/Catalyst-Controller-Atompub/tags/0.3.1/samples/MyBlog/t/01.t @ 3073

Revision 3073, 6.0 kB (checked in by takemaru, 6 years ago)

lang/perl/Catalyst-Controller-Atompub: 0.3.1 released. fix a bug in error handling

Line 
1use strict;
2use warnings;
3use Data::Dumper; $Data::Dumper::Indent = 1;
4use Test::More tests => 93;
5
6use Atompub::Client;
7use Atompub::DateTime qw( datetime );
8use Atompub::MediaType qw( media_type );
9use File::Slurp;
10use FindBin;
11use HTTP::Status;
12use XML::Atom::Entry;
13
14#system "sqlite3 $FindBin::Bin/../test.db < $FindBin::Bin/../init.sql" || die;
15
16
17my $client = Atompub::Client->new;
18
19$client->username('foo');
20$client->password('foo');
21
22
23# Service
24
25my $serv = $client->getService('http://localhost:3000/service');
26isa_ok $serv, 'XML::Atom::Service';
27
28my @work = $serv->workspaces;
29is @work, 1;
30
31is $work[0]->title, 'My Blog';
32
33my @coll = $work[0]->collections;
34is @coll, 2;
35
36is $coll[0]->title, 'Diary';
37is $coll[0]->href, 'http://localhost:3000/entrycollection';
38
39is $coll[1]->title, 'Photo';
40is $coll[1]->href, 'http://localhost:3000/mediacollection';
41
42
43# Create Entry Resource
44
45my $entry = XML::Atom::Entry->new;
46$entry->title('Entry 1');
47$entry->content('This is the 1st entry');
48
49my $category = XML::Atom::Category->new;
50$category->term('animal');
51$category->scheme('http://example.com/dogs/big3');
52$entry->add_category($category);
53
54ok ! $client->createEntry( $coll[0]->href, $entry, 'Entry 1' );
55like $client->errstr, qr{Forbidden category}i;
56
57$category->scheme('http://example.com/cats/big3');
58
59ok my $uri = $client->createEntry( $coll[0]->href, $entry, 'Entry 1' );
60is $uri, 'http://localhost:3000/entrycollection/entry_1.atom';
61
62is $client->res->code, RC_CREATED;
63is $client->res->location, 'http://localhost:3000/entrycollection/entry_1.atom';
64ok $client->res->etag;
65#ok $client->res->last_modified;
66ok media_type( $client->res->content_type )->is_a('entry');
67
68$entry = $client->rc;
69is $entry->title, 'Entry 1';
70is $entry->id, 'http://localhost:3000/entrycollection/entry_1.atom';
71is $entry->link->href, 'http://localhost:3000/entrycollection/entry_1.atom';
72ok $entry->edited;
73ok $entry->updated;
74is $entry->category->term, 'animal';
75like $entry->content->body, qr{This is the 1st entry};
76
77ok $client->createEntry( $coll[0]->href, $entry, 'Entry 1' ); # same slug
78
79
80# List Entry Resources
81
82ok my $feed = $client->getFeed( $coll[0]->href );
83
84is $client->res->code, RC_OK;
85ok media_type( $client->res->content_type )->is_a('feed');
86
87is $feed->title, 'Diary';
88ok $feed->updated;
89is $feed->id, 'http://localhost:3000/entrycollection';
90
91is $feed->self_link, 'http://localhost:3000/entrycollection';
92is $feed->first_link, 'http://localhost:3000/entrycollection';
93is $feed->next_link, undef;
94
95my @entries = $feed->entries;
96is @entries, 2;
97is $entries[0]->title, 'Entry 1';
98
99
100# Read Entry Resource
101
102ok $entry = $client->getEntry( $uri );
103
104is $client->res->code, RC_NOT_MODIFIED;
105
106
107# Update Entry Resource
108
109$entry->title('Entry 1, ver.2');
110
111ok $client->updateEntry( $uri, $entry );
112
113is $client->res->code, RC_OK;
114ok $client->res->etag;
115#ok $client->res->last_modified;
116ok media_type( $client->res->content_type )->is_a('entry');
117
118$entry = $client->rc;
119is $entry->title, 'Entry 1, ver.2';
120
121
122# Delete Entry Resource
123
124ok $client->deleteEntry( $uri );
125
126ok $feed = $client->getFeed( $coll[0]->href );
127is $feed->entries, 1;
128
129
130# Create Media Resource
131
132ok ! $client->createMedia( $coll[1]->href, 't/samples/media1.gif', 'text/plain', 'Media 1' );
133like $client->errstr, qr{unsupported media type}i;
134
135ok $uri = $client->createMedia( $coll[1]->href, 't/samples/media1.gif', 'image/gif', 'Media 1' );
136is $uri, 'http://localhost:3000/mediacollection/media_1.atom';
137
138is $client->res->code, RC_CREATED;
139is $client->res->location, 'http://localhost:3000/mediacollection/media_1.atom';
140ok $client->res->etag;
141#ok $client->res->last_modified;
142ok media_type( $client->res->content_type )->is_a('entry');
143
144$entry = $client->rc;
145is $entry->title, 'Media 1';
146ok $entry->edited;
147ok $entry->updated;
148is $entry->id, 'http://localhost:3000/mediacollection/media_1.atom';
149
150is $entry->edit_link, 'http://localhost:3000/mediacollection/media_1.atom';
151is my $media_uri = $entry->edit_media_link, 'http://localhost:3000/mediacollection/media_1.gif';
152
153is $entry->content->src, 'http://localhost:3000/mediacollection/media_1.gif';
154
155ok $client->createMedia( $coll[1]->href, 't/samples/media1.gif', 'image/gif',
156                         'Media 1' ); # same slug
157
158
159# List Media Link Entries
160
161ok $feed = $client->getFeed( $coll[1]->href );
162
163is $client->res->code, RC_OK;
164ok media_type( $client->res->content_type )->is_a('feed');
165
166is $feed->title, 'Photo';
167ok $feed->updated;
168is $feed->id, 'http://localhost:3000/mediacollection';
169
170is $feed->self_link, 'http://localhost:3000/mediacollection';
171is $feed->first_link, 'http://localhost:3000/mediacollection';
172is $feed->next_link, undef;
173
174@entries = $feed->entries;
175is @entries, 2;
176is $entries[0]->title, 'Media 1';
177
178
179# Read Media Link Entry
180
181ok $entry = $client->getEntry( $uri );
182
183is $client->res->code, RC_NOT_MODIFIED;
184
185
186# Update Media Link Entry
187
188$entry->title('Media 1, ver.2');
189
190ok $client->updateEntry( $uri, $entry );
191
192is $client->res->code, RC_OK;
193ok $client->res->etag;
194#ok $client->res->last_modified;
195ok media_type( $client->res->content_type )->is_a('entry');
196
197$entry = $client->rc;
198is $entry->title, 'Media 1, ver.2';
199
200
201# Read Media Resource
202
203ok my $media = $client->getMedia( $media_uri );
204
205is $client->res->code, RC_OK;
206ok $client->res->etag;
207#ok $client->res->last_modified;
208
209is $media, read_file( 't/samples/media1.gif', binmode => ':raw' );
210
211ok $client->getMedia( $media_uri );
212is $client->res->code, RC_NOT_MODIFIED;
213
214
215# Update Media Resource
216
217# XXX Entry can not have app:edited, you SHOULD get it from Feed generally
218my $prev_edited = $entry->edited;
219sleep 1;
220
221ok $client->updateMedia( $media_uri, 't/samples/media2.png', 'image/png' );
222
223is $client->res->code, RC_OK;
224ok $client->res->etag;
225#ok $client->res->last_modified;
226
227is $client->rc, read_file( 't/samples/media2.png', binmode => ':raw' );
228
229# XXX Entry can not have app:edited, you SHOULD get it from Feed generally
230ok datetime( $client->getEntry( $uri )->edited ) > datetime( $prev_edited );
231
232
233# Delete Entry Resource
234
235ok $client->deleteMedia( $media_uri );
236
237ok $feed = $client->getFeed( $coll[1]->href );
238is $feed->entries, 1;
Note: See TracBrowser for help on using the browser.