Changeset 2137

Show
Ignore:
Timestamp:
01/23/07 06:15:48
Author:
bricas
Message:

reworked to use URI::Template and a Request object as a context with a WWW::OpenSearch?::Url and params hash

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • WWW-OpenSearch/trunk/Changes

    r2116 r2137  
    11Revision history for Perl extension WWW::OpenSearch 
     2 
     30.10_01 Mon Jan 22 2007 
     4        - use URI::Template for parsing OSD uri templates 
     5        - added opensearch-specific Request and Agent classes 
     6        - un-break get_best_url() 
     7        - you can now pass a WWW::OpenSearch::Url to WWW::OpenSearch's 
     8          search() method 
     9        - added ns() (namespace) field to Url.pm 
     10        - re-worked paging to use the new Request object 
     11 
     12        [ THINGS THAT MAY BREAK YOUR CODE ] 
     13        - using URI::Template means some methods are now proxied to 
     14          that class 
     15        - removed agent argument in new() in OpenSearch.pm 
     16        - handling of POST requests in prepare_query() in Url.pm now  
     17          returns data suitable for passing to HTTP::Request 
     18        - un-link Response.pm and the parent WWW::OpenSearch object -- 
     19          a Response is now in the context of whatever 
     20          WWW::OpenSearch::Url was used 
    221 
    3220.09    Thu Dec 07 2006 
  • WWW-OpenSearch/trunk/Makefile.PL

    r1877 r2137  
    44    'VERSION_FROM' => 'lib/WWW/OpenSearch.pm', 
    55    'PREREQ_PM'    => { 
    6         'Data::Page'  => 2.00, 
    7         'Encode'      => 0, 
    8         'LWP'         => 5.60, 
    9         'Test::More'  => 0.32, 
    10         'URI'         => 0, 
    11         'XML::Feed'   => 0.08, 
    12         'XML::LibXML' => 1.58 
     6        'Data::Page'    => 2.00, 
     7        'Encode'        => 0, 
     8        'LWP'           => 5.60, 
     9        'Test::More'    => 0.32, 
     10        'URI'           => 0, 
     11        'XML::Feed'     => 0.08, 
     12        'XML::LibXML'   => 1.58, 
     13        'URI::Template' => 0, 
    1314    }, 
    1415); 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch.pm

    r2115 r2137  
    77 
    88use Carp; 
    9 use WWW::OpenSearch::Response; 
     9use WWW::OpenSearch::Agent; 
     10use WWW::OpenSearch::Request; 
    1011use WWW::OpenSearch::Description; 
    11 use Encode qw( _utf8_off );  
     12 
     13use Encode ();  
    1214 
    1315__PACKAGE__->mk_accessors( qw( description_url agent description ) ); 
    1416 
    15 our $VERSION = '0.09'; 
     17our $VERSION = '0.10_01'; 
    1618 
    1719=head1 NAME 
     
    4345=head1 DESCRIPTION 
    4446 
    45 WWW::OpenSearch is a module to search A9's OpenSearch compatible search engines. See http://opensearch.a9.com/ for details. 
     47WWW::OpenSearch is a module to search A9's OpenSearch compatible search 
     48engines. See http://opensearch.a9.com/ for details. 
    4649 
    4750=head1 CONSTRUCTOR 
    4851 
    49 =head2 new( $url [, $useragent]
     52=head2 new( $url
    5053 
    5154Constructs a new instance of WWW::OpenSearch using the given 
    5255URL as the location of the engine's OpenSearch Description 
    53 document (retrievable via the description_url accessor). Pass any 
    54 LWP::UserAgent compatible object if you wish to override the default 
    55 agent. 
     56document (retrievable via the description_url accessor). 
    5657 
    5758=head1 METHODS 
     
    107108=head1 COPYRIGHT AND LICENSE 
    108109 
    109 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     110Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    110111 
    111112This library is free software; you can redistribute it and/or modify 
     
    115116 
    116117sub new { 
    117     my( $class, $url, $agent ) = @_; 
     118    my( $class, $url ) = @_; 
    118119     
    119120    croak( "No OpenSearch Description url provided" ) unless $url; 
     
    121122    my $self = $class->SUPER::new; 
    122123 
    123     unless( $agent ) { 
    124         require LWP::UserAgent; 
    125         $agent = LWP::UserAgent->new( agent => join( '/', ref $self, $VERSION ) ); 
    126     } 
    127  
    128124    $self->description_url( $url ); 
    129     $self->agent( $agent ); 
     125    $self->agent( WWW::OpenSearch::Agent->new() ); 
    130126 
    131127    $self->fetch_description; 
     
    148144 
    149145sub search { 
    150     my( $self, $query, $params ) = @_; 
     146    my( $self, $query, $params, $url ) = @_; 
    151147 
    152148    $params ||= { }; 
    153149    $params->{ searchTerms } = $query; 
    154     _utf8_off( $params->{ searchTerms } );  
     150    Encode::_utf8_off( $params->{ searchTerms } );  
    155151     
    156     my $url = $self->description->get_best_url; 
    157     return $self->do_search( $url->prepare_query( $params ), $url->method ); 
    158 
    159  
    160 sub do_search { 
    161     my( $self, $url, $method ) = @_; 
    162      
    163     $method = lc( $method ) || 'get'; 
    164      
    165     my $response; 
    166     if( $method eq 'post' ) { 
    167         $response = $self->agent->post( @$url ); 
    168     } 
    169     else { 
    170         $response = $self->agent->$method( $url ); 
    171     } 
    172      
    173     return WWW::OpenSearch::Response->new( $self, $response );     
     152    $url ||= $self->description->get_best_url; 
     153    return $self->agent->search( WWW::OpenSearch::Request->new( $url, $params ) ); 
    174154} 
    175155 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch/Description.pm

    r1947 r2137  
    126126=head1 COPYRIGHT AND LICENSE 
    127127 
    128 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     128Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    129129 
    130130This library is free software; you can redistribute it and/or modify 
     
    166166    my $ns = $element->getNamespace->value; 
    167167    my $version; 
    168     if( $ns eq 'http://a9.com/-/spec/opensearch/1.1/' ) { 
     168    if( $ns eq 'http://a9.com/-/spec/opensearchdescription/1.0/' ) { 
     169        $self->ns( 'http://a9.com/-/spec/opensearchrss/1.0/' ); 
     170        $version = '1.0'; 
     171    } 
     172    else { 
    169173        $self->ns( $ns ); 
    170         $version = '1.1'; 
    171     } 
    172     else { 
    173         $version = '1.0'; 
     174        ( $version ) = $ns =~ m{([^/]+)/?$}; 
    174175    } 
    175176    $self->version( $version ); 
     
    179180        if( $column eq 'Url' ) { 
    180181            if( $version eq '1.0' ) { 
    181                 $self->Url( [ WWW::OpenSearch::Url->new( template => $node->string_value, type => 'application/rss+xml' ) ] ); 
     182                $self->Url( [ WWW::OpenSearch::Url->new( template => $node->string_value, type => 'application/rss+xml', ns => $self->ns ) ] ); 
    182183                next; 
    183184            } 
     
    199200                } 
    200201 
    201                 push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params ); 
     202                push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params, ns => $self->ns ); 
    202203            } 
    203204            $self->Url( \@url ); 
     
    230231            $self->image( $images ); 
    231232        } 
    232         elsif( $version eq '1.0' and $column eq 'Format' ) { 
    233             $self->Format( $node->string_value ); 
    234             $self->ns( $self->Format ); 
    235         } 
    236233        else { 
    237234            $self->$column( $node->string_value ); 
     
    253250    my $type = shift; 
    254251     
    255     my $template; 
    256252    for( $self->urls ) { 
    257         $template = $_ if $_->type eq $type; 
    258         last; 
     253        return $_ if $_->type eq $type; 
    259254    }; 
    260255     
    261     return $template
     256    return
    262257} 
    263258 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch/Image.pm

    r1938 r2137  
    4646=head1 COPYRIGHT AND LICENSE 
    4747 
    48 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     48Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    4949 
    5050This library is free software; you can redistribute it and/or modify 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch/Query.pm

    r1938 r2137  
    4242=head1 COPYRIGHT AND LICENSE 
    4343 
    44 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     44Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    4545 
    4646This library is free software; you can redistribute it and/or modify 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch/Response.pm

    r2114 r2137  
    77 
    88use XML::Feed; 
    9 use URI; 
    109use Data::Page; 
    11  
    12 __PACKAGE__->mk_accessors( qw( feed pager parent ) ); 
     10use WWW::OpenSearch::Agent; 
     11use WWW::OpenSearch::Request; 
     12 
     13__PACKAGE__->mk_accessors( qw( feed pager ) ); 
    1314 
    1415=head1 NAME 
     
    4445=head1 CONSTRUCTOR 
    4546 
    46 =head2 new( $parent, $response ) 
    47  
    48 Constructs a new instance of WWW::OpenSearch::Response. Arguments 
    49 include the WWW::OpenSearch object which initiated the search (parent) 
    50 and the HTTP::Response returned by the search request. 
     47=head2 new( $response ) 
     48 
     49Constructs a new instance of WWW::OpenSearch::Response from the 
     50WWWW::OpenSearch:Response returned by the search request. 
    5151 
    5252=head1 METHODS 
     
    8383=head2 pager( ) 
    8484 
    85 =head2 parent( ) 
    86  
    8785=head1 AUTHOR 
    8886 
     
    9795=head1 COPYRIGHT AND LICENSE 
    9896 
    99 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     97Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    10098 
    10199This library is free software; you can redistribute it and/or modify 
     
    106104sub new { 
    107105    my $class    = shift; 
    108     my $parent   = shift; 
    109106    my $response = shift; 
    110107     
    111108    my $self = bless $response, $class; 
    112109 
    113     $self->parent( $parent ); 
    114110    return $self unless $self->is_success; 
    115111     
     
    137133    my $feed   = $self->feed; 
    138134    my $format = $feed->format; 
    139     my $ns     = $self->parent->description->ns; 
     135    my $ns     = $self->request->opensearch_url->ns; 
    140136     
    141137    # TODO 
     
    179175    my $page        = $pager->$pagermethod; 
    180176    return unless $page; 
    181      
    182     my $request = $self->request; 
    183     my $method  = lc $request->method; 
    184  
    185     if( $method ne 'post' ) { # force query build on POST 
    186         my $link = $self->_get_link( $direction ); 
    187         return $self->parent->do_search( $link, $method ) if $link; 
    188     } 
    189      
    190     my $template = $self->parent->description->get_best_url; 
    191     my( $param, $query ); 
    192     if( $method eq 'post' ) { 
    193         my $uri = URI->new( 'http://foo.com/?' . $request->content ); 
    194         $query = { $uri->query_form }; 
    195     } 
    196     else { 
    197         $query = { $self->request->uri->query_form }; 
    198     } 
    199  
    200     if( $param = $template->macros->{ startPage } ) { 
    201         $query->{ $param } = $pager->$pagermethod 
    202     } 
    203     elsif( $param = $template->macros->{ startIndex } ) { 
    204         if( $query->{ $param } ) { 
    205             $query->{ $param } = $direction eq 'previous' 
    206                 ? $query->{ $param } -= $pager->entries_per_page 
    207                 : $query->{ $param } += $pager->entries_per_page; 
     177 
     178    my $params; 
     179    my $osu = $self->request->opensearch_url; 
     180 
     181#    this code is too fragile -- deparse depends on the order of query 
     182#    params and the like. best just to use the last query params and 
     183#    do the paging from there. 
     184
     185#    if( lc $osu->method ne 'post' ) { # force query build on POST 
     186#        my $link = $self->_get_link( $direction ); 
     187#        if( $link ) { 
     188#            $params = $osu->deparse( $link ); 
     189#        } 
     190#    } 
     191 
     192    # rebuild the query 
     193    if( !$params ) { 
     194        $params = $self->request->opensearch_params; 
     195 
     196        # handle paging via a page # 
     197        $params->{ startPage } = $page; 
     198 
     199        # handle paging via an index 
     200        if( exists $params->{ startIndex } ) { 
     201            # start index is pre-existing 
     202            if( $params->{ startIndex } ) { 
     203                if( $direction eq 'previous' ) { 
     204                    $params->{ startIndex } -= $pager->entries_per_page 
     205                } 
     206                else { 
     207                    $params->{ startIndex } += $pager->entries_per_page; 
     208                } 
     209            } 
     210            # start index did not exist previously 
     211            else { 
     212                if( $direction eq 'previous' ) { 
     213                    $params->{ startIndex } = 1 
     214                } 
     215                else { 
     216                    $params->{ startIndex } = $pager->entries_per_page + 1; 
     217                } 
     218 
     219            } 
    208220        } 
    209         else { 
    210             $query->{ $param } = $direction eq 'previous' 
    211                 ? 1 
    212                 : $pager->entries_per_page + 1; 
    213         } 
    214     } 
    215  
    216     return $self->parent->do_search( $template->prepare_query( $query ), $method ); 
     221    } 
     222 
     223    my $agent = WWW::OpenSearch::Agent->new; 
     224    return $agent->search( WWW::OpenSearch::Request->new( 
     225        $osu, $params 
     226    ) ); 
    217227} 
    218228 
  • WWW-OpenSearch/trunk/lib/WWW/OpenSearch/Url.pm

    r1947 r2137  
    66use base qw( Class::Accessor::Fast ); 
    77 
    8 use URI; 
    9 use URI::Escape; 
     8use URI::Template; 
    109 
    11 __PACKAGE__->mk_accessors( qw( type template method params macros ) ); 
     10__PACKAGE__->mk_accessors( qw( type template method params ns ) ); 
    1211 
    1312=head1 NAME 
     
    2524=head1 METHODS 
    2625 
    27 =head2 parse_macros( ) 
    28  
    2926=head2 prepare_query( [ \%params ] ) 
    3027 
     
    4138=item * params 
    4239 
    43 =item * macro
     40=item * n
    4441 
    4542=back 
     
    5754=head1 COPYRIGHT AND LICENSE 
    5855 
    59 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy 
     56Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy 
    6057 
    6158This library is free software; you can redistribute it and/or modify 
     
    6865     
    6966    $options{ method } ||= 'GET'; 
    70     $options{ template } = URI->new( $options{ template } ); 
     67    $options{ template } = URI::Template->new( $options{ template } ); 
    7168     
    7269    my $self = $class->SUPER::new( \%options ); 
    73     $self->parse_macros; 
    7470 
    7571    return $self; 
    7672} 
    7773 
    78 sub parse_macros { 
    79     my $self = shift; 
    80      
    81     my %query = $self->method eq 'post' 
    82         ? %{ $self->params } 
    83         : $self->template->query_form; 
    84      
    85     my %macros; 
    86     for( keys %query ) { 
    87         if( $query{ $_ } =~ /^{(.+)}$/ ) { 
    88             $macros{ $1 } = $_; 
    89         } 
    90     } 
    91      
    92     $self->macros( \%macros ); 
    93 } 
    94  
    9574sub prepare_query { 
    9675    my( $self, $params ) = @_; 
    97     my $url   = $self->template->clone; 
     76    my $tmpl = $self->template; 
    9877     
    9978    $params->{ startIndex     } ||= 1; 
     
    10382    $params->{ inputEncoding  } ||= 'UTF-8'; 
    10483     
    105     my $macros = $self->macros; 
     84    # fill the uri template 
     85    my $url = $tmpl->process( %$params ); 
    10686 
    10787    # attempt to handle POST 
    10888    if( $self->method eq 'post' ) { 
    10989        my $post = $self->params; 
    110         for( keys %$macros ) { 
    111             $post->{ $macros->{ $_ } } = $params->{ $_ }
     90        for my $key ( keys %$post ) { 
     91            $post->{ $key } =~ s/{(.+)}/$params->{ $1 } || ''/eg
    11292        } 
    113         return [ $url, $post ]; 
    114     } 
    11593 
    116     my $query = { $url->query_form }; 
    117     for( keys %$macros ) { 
    118         $query->{ $macros->{ $_ } } = $params->{ $_ }; 
     94        return $url, [ %$post ]; 
    11995    } 
    12096     
    121     $url->query_form( $query ); 
    12297    return $url; 
    12398} 
  • WWW-OpenSearch/trunk/t/00_compile.t

    r1938 r2137  
    11use strict; 
    2 use Test::More tests => 6
     2use Test::More tests => 8
    33 
    44BEGIN { 
     
    99    use_ok 'WWW::OpenSearch::Query'; 
    1010    use_ok 'WWW::OpenSearch::Image'; 
     11    use_ok 'WWW::OpenSearch::Agent'; 
     12    use_ok 'WWW::OpenSearch::Request'; 
    1113} 
  • WWW-OpenSearch/trunk/t/10-description.t

    r1938 r2137  
    2121    my $osd = WWW::OpenSearch::Description->new( $description ); 
    2222    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    23     is( $osd->shortname, 'Web Search' ); 
    24     ok( !defined $osd->longname ); 
    25     is( $osd->description, 'Use Example.com to search the Web.' ); 
    26     is( $osd->tags, 'example web' ); 
    27     is( $osd->contact, 'admin@example.com' ); 
     23    is( $osd->shortname, 'Web Search', 'shortname' ); 
     24    ok( !defined $osd->longname, 'longname' ); 
     25    is( $osd->description, 'Use Example.com to search the Web.', 'description' ); 
     26    is( $osd->tags, 'example web', 'tags' ); 
     27    is( $osd->contact, 'admin@example.com', 'contact' ); 
    2828 
    2929    # count the urls 
     
    6969    my $osd = WWW::OpenSearch::Description->new( $description ); 
    7070    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    71     is( $osd->shortname, 'Web Search' ); 
    72     is( $osd->longname, 'Example.com Web Search' ); 
    73     is( $osd->description, 'Use Example.com to search the Web.' ); 
    74     is( $osd->tags, 'example web' ); 
    75     is( $osd->contact, 'admin@example.com' ); 
    76     is( $osd->developer, 'Example.com Development Team' ); 
     71    is( $osd->shortname, 'Web Search', 'shortname' ); 
     72    is( $osd->longname, 'Example.com Web Search', 'longname' ); 
     73    is( $osd->description, 'Use Example.com to search the Web.', 'description' ); 
     74    is( $osd->tags, 'example web', 'tags' ); 
     75    is( $osd->contact, 'admin@example.com', 'contact' ); 
     76    is( $osd->developer, 'Example.com Development Team', 'developer' ); 
    7777    is( $osd->attribution, ' 
    7878    Search data © 2005, Example.com, Inc., All Rights Reserved 
    79   ' ); 
    80     is( $osd->inputencoding, 'UTF-8' ); 
    81     is( $osd->outputencoding, 'UTF-8' ); 
    82     is( $osd->language, 'en-us' ); 
    83     is( $osd->adultcontent, 'false' ); 
    84     is( $osd->syndicationright, 'open' ); 
     79  ', 'attribution' ); 
     80    is( $osd->inputencoding, 'UTF-8', 'inputencoding' ); 
     81    is( $osd->outputencoding, 'UTF-8', 'outputencoding' ); 
     82    is( $osd->language, 'en-us', 'language' ); 
     83    is( $osd->adultcontent, 'false', 'adultcontent' ); 
     84    is( $osd->syndicationright, 'open', 'syndicationright' ); 
    8585 
    8686    my $queries = $osd->query; 
    8787    is( scalar @$queries, 1, 'number of query objects' ); 
    88     is( $queries->[ 0 ]->role, 'example' ); 
    89     is( $queries->[ 0 ]->searchTerms, 'cat' ); 
     88    is( $queries->[ 0 ]->role, 'example', 'role' ); 
     89    is( $queries->[ 0 ]->searchTerms, 'cat', 'searchTerms' ); 
    9090 
    9191    my $images = $osd->image; 
    9292    is( scalar @$images, 2, 'number of image objects' ); 
    93     is( $images->[ 0 ]->height, 64 ); 
    94     is( $images->[ 0 ]->width, 64 ); 
    95     is( $images->[ 0 ]->type, 'image/png' ); 
    96     is( $images->[ 0 ]->url, 'http://example.com/websearch.png' ); 
    97     is( $images->[ 1 ]->height, 16 ); 
    98     is( $images->[ 1 ]->width, 16 ); 
    99     is( $images->[ 1 ]->type, 'image/vnd.microsoft.icon' ); 
    100     is( $images->[ 1 ]->url, 'http://example.com/websearch.ico' ); 
     93    is( $images->[ 0 ]->height, 64, 'height' ); 
     94    is( $images->[ 0 ]->width, 64, 'width' ); 
     95    is( $images->[ 0 ]->type, 'image/png', 'content type' ); 
     96    is( $images->[ 0 ]->url, 'http://example.com/websearch.png', 'url' ); 
     97    is( $images->[ 1 ]->height, 16, 'height' ); 
     98    is( $images->[ 1 ]->width, 16, 'width' ); 
     99    is( $images->[ 1 ]->type, 'image/vnd.microsoft.icon', 'content type' ); 
     100    is( $images->[ 1 ]->url, 'http://example.com/websearch.ico', 'url' ); 
    101101 
    102102    # count the urls 
     
    128128    my $osd = WWW::OpenSearch::Description->new( $description ); 
    129129    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    130     is( $osd->shortname, 'Electronics' ); 
    131     is( $osd->longname, 'Amazon Electronics' ); 
    132     is( $osd->description, 'Search for electronics on Amazon.com.' ); 
    133     is( $osd->tags, 'amazon electronics' ); 
    134     is( $osd->contact, 'dewitt@unto.net' ); 
    135     is( $osd->format, 'http://a9.com/-/spec/opensearchrss/1.0/' ); 
    136     is( $osd->image, 'http://www.unto.net/search/amazon_electronics.gif' ); 
    137     is( $osd->samplesearch, 'ipod' ); 
    138     is( $osd->developer, 'DeWitt Clinton' ); 
     130    is( $osd->shortname, 'Electronics', 'shortname' ); 
     131    is( $osd->longname, 'Amazon Electronics', 'longname' ); 
     132    is( $osd->description, 'Search for electronics on Amazon.com.', 'descrpiton' ); 
     133    is( $osd->tags, 'amazon electronics', 'tags' ); 
     134    is( $osd->contact, 'dewitt@unto.net', 'contact' ); 
     135    is( $osd->format, 'http://a9.com/-/spec/opensearchrss/1.0/', 'format' ); 
     136    is( $osd->image, 'http://www.unto.net/search/amazon_electronics.gif', 'image' ); 
     137    is( $osd->samplesearch, 'ipod', 'samplesearch' ); 
     138    is( $osd->developer, 'DeWitt Clinton', 'developer' ); 
    139139    is( $osd->attribution, 'Product and search data © 2005, Amazon, Inc., 
    140    All Rights Reserved' ); 
    141     is( $osd->syndicationright, 'open' ); 
    142     is( $osd->adultcontent, 'false' ); 
     140   All Rights Reserved', 'attribution' ); 
     141    is( $osd->syndicationright, 'open', 'syndicationright' ); 
     142    is( $osd->adultcontent, 'false', 'adultcontent' ); 
    143143 
    144144    # count the urls 
    145     is( $osd->urls, 1 ); 
     145    is( $osd->urls, 1, 'urls' ); 
    146146} 
    147  
  • WWW-OpenSearch/trunk/t/11-url.t

    r1892 r2137  
    22use warnings; 
    33 
    4 use Test::More tests => 27
     4use Test::More tests => 36
    55 
    66use_ok( 'WWW::OpenSearch::Description' ); 
     
    1717    my $osd = WWW::OpenSearch::Description->new( $description ); 
    1818    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    19     is( $osd->urls, 1 ); 
     19    is( $osd->version, '1.1', 'version' ); 
     20    is( $osd->ns, 'http://a9.com/-/spec/opensearch/1.1/', 'namespace' ); 
     21    is( $osd->urls, 1, 'number of urls' ); 
    2022 
    2123    my( $url ) = $osd->urls; 
    2224    isa_ok( $url, 'WWW::OpenSearch::Url' ); 
    23     is( $url->type, 'application/rss+xml' ); 
    24     is( lc $url->method, 'get' ); 
    25     is( $url->template, 'http://example.com/?q=%7BsearchTerms%7D&pw=%7BstartPage%7D&format=rss' ); 
     25    is( $url->type, 'application/rss+xml', 'content type' ); 
     26    is( lc $url->method, 'get', 'method' ); 
     27    is( $url->template, 'http://example.com/?q={searchTerms}&pw={startPage}&format=rss', 'template' ); 
     28    my $result = $url->prepare_query( { searchTerms => 'x', startPage => 1 } ); 
     29    is( $result, 'http://example.com/?q=x&pw=1&format=rss', 'prepare_query' ); 
    2630} 
    2731 
     
    4650    my $osd = WWW::OpenSearch::Description->new( $description ); 
    4751    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    48     is( $osd->urls, 3 ); 
     52    is( $osd->urls, 3, 'number of urls' ); 
     53    is( $osd->get_best_url, $osd->url->[ 1 ], 'get_best_url' ); 
    4954 
    5055    { 
    5156        my $url = $osd->url->[ 0 ]; 
    5257        isa_ok( $url, 'WWW::OpenSearch::Url' ); 
    53         is( $url->type, 'application/rss+xml' ); 
    54         is( lc $url->method, 'get' ); 
    55         is( $url->template, 'http://example.com/?q=%7BsearchTerms%7D&pw=%7BstartPage%7D&format=rss' ); 
     58        is( $url->type, 'application/rss+xml', 'content type' ); 
     59        is( lc $url->method, 'get', 'method' ); 
     60        is( $url->template, 'http://example.com/?q={searchTerms}&pw={startPage}&format=rss', 'template' ); 
    5661    } 
    5762 
     
    5964        my $url = $osd->url->[ 1 ]; 
    6065        isa_ok( $url, 'WWW::OpenSearch::Url' ); 
    61         is( $url->type, 'application/atom+xml' ); 
    62         is( lc $url->method, 'get' ); 
    63         is( $url->template, 'http://example.com/?q=%7BsearchTerms%7D&pw=%7BstartPage%7D&format=atom' ); 
     66        is( $url->type, 'application/atom+xml', 'content type' ); 
     67        is( lc $url->method, 'get', 'method' ); 
     68        is( $url->template, 'http://example.com/?q={searchTerms}&pw={startPage}&format=atom', 'template' ); 
    6469    } 
    6570 
     
    6772        my $url = $osd->url->[ 2 ]; 
    6873        isa_ok( $url, 'WWW::OpenSearch::Url' ); 
    69         is( $url->type, 'text/html' ); 
    70         is( lc $url->method, 'post' ); 
    71         is( $url->template, 'https://intranet/search?format=html' ); 
     74        is( $url->type, 'text/html', 'content type' ); 
     75        is( lc $url->method, 'post', 'method' ); 
     76        is( $url->template, 'https://intranet/search?format=html', 'template' ); 
     77        is_deeply( $url->params, { s => '{searchTerms}', o => '{startIndex}', c => '{itemsPerPage}', l => '{language}' }, 'params' ); 
     78        my( $result, $post ) = $url->prepare_query( { searchTerms => 'x', startIndex => '1', itemsPerPage => 1, language => 'en' } ); 
     79        is( $result, 'https://intranet/search?format=html', 'prepare_query (uri)' ); 
     80        $post = { @$post }; 
     81        is_deeply( $post, { s => 'x', o => 1, c => 1, l => 'en' }, 'prepare_query (params)' ); 
    7282    } 
    7383} 
     
    8292    my $osd = WWW::OpenSearch::Description->new( $description ); 
    8393    isa_ok( $osd, 'WWW::OpenSearch::Description' ); 
    84     is( $osd->urls, 1 ); 
     94    is( $osd->version, '1.0', 'version' ); 
     95    is( $osd->ns, 'http://a9.com/-/spec/opensearchrss/1.0/', 'namespace' ); 
     96    is( $osd->urls, 1, 'number of urls' ); 
    8597 
    8698    my( $url ) = $osd->urls; 
    8799    isa_ok( $url, 'WWW::OpenSearch::Url' ); 
    88     is( lc $url->method, 'get' ); 
    89     is( $url->template, 'http://www.unto.net/aws?q=%7BsearchTerms%7D&searchindex=Electronics&flavor=osrss&itempage=%7BstartPage%7D' ); 
     100    is( lc $url->method, 'get', 'method' ); 
     101    is( $url->template, 'http://www.unto.net/aws?q={searchTerms}&searchindex=Electronics&flavor=osrss&itempage={startPage}', 'template' ); 
    90102} 
    91103