Changeset 1946

Show
Ignore:
Timestamp:
04/17/08 02:37:16 (22 months ago)
Author:
fumiakiy
Message:

Added more tests to Atom PP implementation.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-35/t/41-atom.t

    r1098 r1946  
    11use strict; 
     2use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib'; 
    23use POSIX; 
    34 
    4 use Test::More tests => 11; 
     5use MT; 
     6use MT::Atom; 
     7use XML::LibXML; # this test would not work without it 
     8use XML::XPath; 
     9use XML::Atom; 
     10use XML::Atom::Feed; 
     11use XML::Atom::Entry; 
     12 
     13use Test::More tests => 37; 
    514 
    615# To keep away from being under FastCGI 
    716$ENV{HTTP_HOST} = 'localhost'; 
    8  
    9 use MT::Atom; 
    10  
    11 use MT; 
    1217 
    1318use vars qw( $DB_DIR $T_CFG ); 
     
    1520isa_ok($mt, 'MT'); 
    1621 
    17 use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib'; 
    1822use MT::Test qw(:db :data); 
     23 
     24my %test_data; 
     25$test_data{'/mt-atom.cgi/weblog'} = <<XML1; 
     26<?xml version="1.0" encoding="utf-8"?> 
     27    <entry xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
     28    <title>Fight the Power</title> 
     29    <content>Elvis was a hero to most but he never meant shit to me</content> 
     30    <issued>2004-08-06T00:43:34+01:00</issued> 
     31    </entry> 
     32XML1 
     33$test_data{'/mt-atom.cgi/1.0'} = <<XML2; 
     34<?xml version="1.0" encoding="utf-8"?> 
     35<entry xmlns="http://www.w3.org/2005/Atom"> 
     36<title>Fight the Power</title> 
     37<content type="html">Elvis was a hero to most but he never meant shit to me</content> 
     38<published>2004-08-06T00:43:34+01:00</published> 
     39</entry> 
     40XML2 
     41 
     42my %feed_link = ( 
     43    '/mt-atom.cgi/weblog' => sub { 
     44        my ($resp) = @_; 
     45        my $feed = XML::Atom::Feed->new(\$resp->content()); 
     46        ok($feed, 'got feed'); 
     47        my ($sfeed) = grep { 
     48            $_->rel eq 'service.feed' 
     49        } $feed->links; 
     50        $sfeed->href; 
     51    }, 
     52    '/mt-atom.cgi/1.0' => sub { 
     53        my ($resp) = @_; 
     54        my $feed = XML::XPath->new(xml => $resp->content()); 
     55        ok($feed, 'got feed'); 
     56        my $col = $feed->getNodeText('/service/workspace[1]/collection/@href'); 
     57        $col; 
     58    } 
     59); 
    1960 
    2061my $username = 'Chuck D'; 
     
    4384my $ua = new LWP::UserAgent::Local({ ScriptAlias => '/' }); 
    4485 
     86foreach my $base_uri ( qw{/mt-atom.cgi/weblog /mt-atom.cgi/1.0 } ) { 
    4587{ 
    4688# # # # First try a req with baloney auth, make sure it fails 
     
    5092 
    5193    my $uri = new URI(); 
    52     $uri->path('/mt-atom.cgi/weblog/blog_id=1'); 
     94    $uri->path($base_uri . '/blog_id=1'); 
    5395    my $req = new HTTP::Request(GET => $uri); 
    5496    $req->header(Authentication => 'Atom'); 
     
    62104} 
    63105 
     106# test blog lists 
     107{ 
     108    my $wsse_header = make_wsse($chuck_token); 
     109    my $uri = new URI; 
     110    $uri->path($base_uri); 
     111    my $req = new HTTP::Request(GET => $uri); 
     112    $req->header('Authorization' => 'Atom'); 
     113    $req->header('X-WSSE' => $wsse_header); 
     114 
     115    print "# X-WSSE: $wsse_header\n"; 
     116 
     117    my $resp = $ua->request($req); 
     118    if (ok($resp->is_success)) { 
     119        my $blog_feed_url = $feed_link{$base_uri}->($resp); 
     120        my $uri = new URI($blog_feed_url); 
     121        is($uri->path, $base_uri . '/blog_id=1', 'blog feed url is correct'); 
     122    } 
     123    else { 
     124        die 'failed to retrieve blog feed'; 
     125    } 
     126} 
     127 
     128# test blog feed 
     129{ 
     130    my $wsse_header = make_wsse($chuck_token); 
     131    my $uri = new URI; 
     132    $uri->path($base_uri . "/blog_id=1"); 
     133    my $req = new HTTP::Request(GET => $uri); 
     134    $req->header('Authorization' => 'Atom'); 
     135    $req->header('X-WSSE' => $wsse_header); 
     136 
     137    print "# X-WSSE: $wsse_header\n"; 
     138 
     139    my $resp = $ua->request($req); 
     140    if (ok($resp->is_success)) { 
     141        my $feed = XML::Atom::Feed->new(\$resp->content()); 
     142        ok($feed, 'got feed'); 
     143        is($feed->title, 'none'); 
     144        my ($alternate) = grep { 
     145            $_->rel eq 'alternate' && $_->type eq 'text/html' 
     146        } $feed->links; 
     147        is($alternate->href, 'http://narnia.na/nana/', 'blog url is correct'); 
     148        my $entry_count = MT::Entry->count( 
     149            { blog_id => 1 }, 
     150            { limit => 21 }, 
     151        ); 
     152        my @entries = $feed->entries; 
     153        is($entry_count, scalar(@entries), 'number of entries is correct'); 
     154    } 
     155    else { 
     156        die 'failed to retrieve blog feed'; 
     157    } 
     158} 
    64159 
    65160my $entry_id; 
     
    78173 
    79174    my $uri = new URI; 
    80     $uri->path('/mt-atom.cgi/weblog/blog_id=1'); 
     175    $uri->path($base_uri . '/blog_id=1'); 
    81176    my $req = new HTTP::Request(POST => $uri); 
    82177 
     
    110205 
    111206    $uri = new URI; 
    112     $uri->path('/mt-atom.cgi/weblog/blog_id=1'); 
     207    $uri->path($base_uri . '/blog_id=1'); 
    113208    $req = new HTTP::Request(POST => $uri); 
    114209 
     
    116211    print "# sending: ", $req->header('X-Atom-Authorization'); 
    117212 
    118     $req->content(<<XML); 
    119 <?xml version="1.0" encoding="utf-8"?> 
    120     <entry xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
    121     <title>Fight the Power</title> 
    122     <content>Elvis was a hero to most but he never meant shit to me</content> 
    123     </entry> 
    124 XML 
     213    $req->content($test_data{$base_uri}); 
    125214 
    126215    $resp = $ua->simple_request($req); 
     
    170259 
    171260    my $uri = new URI; 
    172     $uri->path('/mt-atom.cgi/weblog/blog_id=1'); 
     261    $uri->path($base_uri . '/blog_id=1'); 
    173262    my $req = new HTTP::Request(POST => $uri); 
    174263    $req->header('Authorization' => 'Atom'); 
     
    177266                 . "Created=\"$timestamp\""); 
    178267 
    179     $req->content(<<XML); 
    180 <?xml version="1.0" encoding="utf-8"?> 
    181     <entry xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
    182     <title>Fight the Power</title> 
    183     <content>Elvis was a hero to most but he never meant shit to me</content> 
    184     <issued>2004-08-06T00:43:34+01:00</issued> 
    185     </entry> 
    186 XML 
     268    $req->content($test_data{$base_uri}); 
    187269 
    188270    my $resp = $ua->simple_request($req); 
     
    209291 
    210292    ok($entry_id); 
    211     require XML::Atom; 
    212293    my $atom_obj = XML::Atom::Entry->new(\$resp->content()); 
    213294    require Date::Parse; 
     
    220301    my $wsse_header = make_wsse($chuck_token); 
    221302    my $uri = new URI; 
    222     $uri->path("/mt-atom.cgi/weblog/blog_id=1/entry_id=$entry_id"); 
     303    $uri->path($base_uri . "/blog_id=1/entry_id=$entry_id"); 
    223304    my $req = new HTTP::Request(GET => $uri); 
    224305    $req->header('Authorization' => 'Atom'); 
    225306    $req->header('X-WSSE' => $wsse_header); 
    226307 
    227     print "# X-WSSSE: $wsse_header\n"; 
     308    print "# X-WSSE: $wsse_header\n"; 
    228309 
    229310    my $resp = $ua->request($req); 
    230311 
    231     require XML::LibXML; 
    232312    my $atom_entry = XML::Atom::Entry->new(Stream => \$resp->content()); 
    233313 
     
    239319    $wsse_header = make_wsse($chuck_token); 
    240320    $uri = new URI; 
    241     $uri->path("/mt-atom.cgi/weblog/blog_id=1/entry_id=$entry_id"); 
     321    $uri->path($base_uri . "/blog_id=1/entry_id=$entry_id"); 
    242322    $req = new HTTP::Request(PUT => $uri); 
    243323    $req->header('Authorization' => 'Atom'); 
     
    260340} 
    261341 
     342} #end foreach 
     343 
     344 
    262345END { 
    263346    #my $melody = MT::Author->load({ name => $username });