source: bookmarks/trunk/t/00-cgiapp.t @ 113

Last change on this file since 113 was 92, checked in by peter, 9 years ago

issue #10: added basic test of the web app and the Bookmark class

To make the application testable, BookmarksApp now does not require a
config_file to be set, and can just be configured via a config hash in the
constructor. In addition, authentication and reverse proxy rewriting are now
conditionally enabled based on whether there is an auth and proxy_ip key in
the config, respecitvely.

Added Plack 1.0036 to the cpanfile dependencies, since that is the version that
contains Plack::Test.

Also fixed the Bookmarks::create_tables() method to load the
bookmarks.sql file from the correct location.

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use Plack::Test;
5use HTTP::Request::Common;
6
7use Test::More;
8#use Test::WWW::Mechanize::CGIApp;
9use Test::JSON;
10use Digest::MD5 qw{md5_hex};
11
12# test fixture setup
13my $dbname = 't/apptest.db';
14unlink $dbname if -e $dbname;
15my $bookmarks = Bookmarks->new({ dbname => $dbname });
16$bookmarks->create_tables;
17my $username = 'apptest';
18my $password = 'apptest';
19
20# app setup
21use BookmarksApp;
22my $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
32my $test = Plack::Test->create($app);
33
34my $res = $test->request(GET "/");
35is($res->content_type, 'text/html');
36
37$res = $test->request(GET '/?format=json');
38is($res->content_type, 'application/json', 'JSON content type is application/json');
39is_valid_json $res->content;
40is_json $res->content, '{ "bookmarks": [] }';
41
42$res = $test->request(GET '/?format=xbel');
43is($res->content_type, 'application/xml', 'XBEL content type is application/xml');
44
45$res = $test->request(GET '/?format=text');
46is($res->content_type, 'text/uri-list', 'Text content type is text/uri-list');
47
48
49my $bookmark_uri;
50
51subtest '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
75subtest '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");
96my $id = $res->content;
97$res = $test->request(GET "$bookmark_uri/ctime");
98my $ctime = $res->content;
99$res = $test->request(GET "$bookmark_uri/mtime");
100my $mtime = $res->content;
101
102$res = $test->request(GET "$bookmark_uri?format=json");
103is($res->content_type, 'application/json');
104is_valid_json $res->content;
105is_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}
115END_JSON
116
117done_testing();
118
119# test fixture teardown
120unlink $dbname if -e $dbname;
Note: See TracBrowser for help on using the repository browser.