[92] | 1 | #!/usr/bin/perl -w |
---|
| 2 | use strict; |
---|
| 3 | |
---|
| 4 | use Plack::Test; |
---|
| 5 | use HTTP::Request::Common; |
---|
| 6 | |
---|
| 7 | use Test::More; |
---|
| 8 | #use Test::WWW::Mechanize::CGIApp; |
---|
| 9 | use Test::JSON; |
---|
| 10 | use Digest::MD5 qw{md5_hex}; |
---|
| 11 | |
---|
| 12 | # test fixture setup |
---|
| 13 | my $dbname = 't/apptest.db'; |
---|
| 14 | unlink $dbname if -e $dbname; |
---|
| 15 | my $bookmarks = Bookmarks->new({ dbname => $dbname }); |
---|
| 16 | $bookmarks->create_tables; |
---|
| 17 | my $username = 'apptest'; |
---|
| 18 | my $password = 'apptest'; |
---|
| 19 | |
---|
| 20 | # app setup |
---|
| 21 | use BookmarksApp; |
---|
| 22 | my $app = BookmarksApp->new({ |
---|
| 23 | config => { |
---|
| 24 | dbname => $dbname, |
---|
| 25 | #auth => 1, |
---|
| 26 | digest_key => 'test_secret', |
---|
| 27 | digest_password => md5_hex("$username:Bookmarks:$password"), |
---|
| 28 | #proxy_ip => '', |
---|
| 29 | } |
---|
| 30 | })->to_app; |
---|
| 31 | |
---|
| 32 | my $test = Plack::Test->create($app); |
---|
| 33 | |
---|
| 34 | my $res = $test->request(GET "/"); |
---|
| 35 | is($res->content_type, 'text/html'); |
---|
| 36 | |
---|
| 37 | $res = $test->request(GET '/?format=json'); |
---|
| 38 | is($res->content_type, 'application/json', 'JSON content type is application/json'); |
---|
| 39 | is_valid_json $res->content; |
---|
| 40 | is_json $res->content, '{ "bookmarks": [] }'; |
---|
| 41 | |
---|
| 42 | $res = $test->request(GET '/?format=xbel'); |
---|
| 43 | is($res->content_type, 'application/xml', 'XBEL content type is application/xml'); |
---|
| 44 | |
---|
| 45 | $res = $test->request(GET '/?format=text'); |
---|
| 46 | is($res->content_type, 'text/uri-list', 'Text content type is text/uri-list'); |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | my $bookmark_uri; |
---|
| 50 | |
---|
| 51 | subtest 'Created bookmark' => sub { |
---|
| 52 | # create a bookmark |
---|
| 53 | my $res = $test->request(POST |
---|
| 54 | '/', |
---|
| 55 | { |
---|
| 56 | uri => 'http://metacpan.org', |
---|
| 57 | title => 'My Bookmark', |
---|
| 58 | tags => 'test', |
---|
| 59 | }, |
---|
| 60 | ); |
---|
| 61 | |
---|
| 62 | $bookmark_uri = $res->header('Location'); #->as_string; |
---|
| 63 | |
---|
| 64 | $res = $test->request(GET "$bookmark_uri/title"); |
---|
| 65 | is($res->content_type, 'text/plain', 'Title is text/plain'); |
---|
| 66 | is($res->content, 'My Bookmark', 'Title has correct value'); |
---|
| 67 | $res = $test->request(GET "$bookmark_uri/uri"); |
---|
| 68 | is($res->content_type, 'text/plain', 'URI is text/plain'); |
---|
| 69 | is($res->content, 'http://metacpan.org', 'URI has correct value'); |
---|
| 70 | $res = $test->request(GET "$bookmark_uri/tags"); |
---|
| 71 | is($res->content_type, 'text/plain', 'Tags are text/plain'); |
---|
| 72 | is($res->content, 'test', 'Tags have correct value'); |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | subtest 'Updated bookmark' => sub { |
---|
| 76 | my $res = $test->request( |
---|
| 77 | POST $bookmark_uri, |
---|
| 78 | { |
---|
| 79 | uri => 'http://search.cpan.org', |
---|
| 80 | title => 'My Other Bookmark', |
---|
| 81 | tags => 'test one two', |
---|
| 82 | }, |
---|
| 83 | ); |
---|
| 84 | $res = $test->request(GET "$bookmark_uri/title"); |
---|
| 85 | is($res->content_type, 'text/plain', 'Title is text/plain'); |
---|
| 86 | is($res->content, 'My Other Bookmark', 'Title has correct value'); |
---|
| 87 | $res = $test->request(GET "$bookmark_uri/uri"); |
---|
| 88 | is($res->content_type, 'text/plain', 'URI is text/plain'); |
---|
| 89 | is($res->content, 'http://search.cpan.org', 'URI has correct value'); |
---|
| 90 | $res = $test->request(GET "$bookmark_uri/tags"); |
---|
| 91 | is($res->content_type, 'text/plain', 'Tags are text/plain'); |
---|
| 92 | is($res->content, 'one test two', 'Tags have correct value'); |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | $res = $test->request(GET "$bookmark_uri/id"); |
---|
| 96 | my $id = $res->content; |
---|
| 97 | $res = $test->request(GET "$bookmark_uri/ctime"); |
---|
| 98 | my $ctime = $res->content; |
---|
| 99 | $res = $test->request(GET "$bookmark_uri/mtime"); |
---|
| 100 | my $mtime = $res->content; |
---|
| 101 | |
---|
| 102 | $res = $test->request(GET "$bookmark_uri?format=json"); |
---|
| 103 | is($res->content_type, 'application/json'); |
---|
| 104 | is_valid_json $res->content; |
---|
| 105 | is_json $res->content, <<"END_JSON"; |
---|
| 106 | { |
---|
| 107 | "id": $id, |
---|
| 108 | "bookmark_uri": "$bookmark_uri", |
---|
| 109 | "ctime": $ctime, |
---|
| 110 | "mtime": $mtime, |
---|
| 111 | "uri": "http://search.cpan.org", |
---|
| 112 | "title": "My Other Bookmark", |
---|
| 113 | "tags": ["one", "test", "two"] |
---|
| 114 | } |
---|
| 115 | END_JSON |
---|
| 116 | |
---|
| 117 | done_testing(); |
---|
| 118 | |
---|
| 119 | # test fixture teardown |
---|
| 120 | unlink $dbname if -e $dbname; |
---|