1use Test::More tests => 9;
2
3use strict;
4use warnings;
5
6use_ok( 'WWW::OpenSearch::Description' );
7use_ok( 'WWW::OpenSearch::Request' );
8
9{
10    my $description = q(<?xml version="1.0" encoding="UTF-8"?>
11<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
12  <Url type="application/rss+xml"
13       template="http://example.com/?q={searchTerms}&amp;pw={startPage}&amp;format=rss"/>
14  <Url type="application/atom+xml"
15       template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=atom"/>
16  <Url type="text/html"
17       method="post"
18       template="https://intranet/search?format=html">
19    <Param name="s" value="{searchTerms}"/>
20    <Param name="o" value="{startIndex?}"/>
21    <Param name="c" value="{itemsPerPage?}"/>
22    <Param name="l" value="{language?}"/>
23  </Url>
24</OpenSearchDescription>
25);
26
27    my $osd = WWW::OpenSearch::Description->new( $description );
28
29    {
30        my $req = WWW::OpenSearch::Request->new( $osd->url->[ 2 ],
31            { searchTerms => 'iPod' } );
32        isa_ok( $req, 'WWW::OpenSearch::Request' );
33        is( lc $req->method, 'post', 'method' );
34        is( $req->uri, 'https://intranet/search?format=html', 'uri' );
35        is( _sort_result( $req->content ), 'c=&l=*&o=1&s=iPod', 'content' );
36    }
37
38    {
39        my $req = WWW::OpenSearch::Request->new( $osd->url->[ 1 ],
40            { searchTerms => 'iPod' } );
41        isa_ok( $req, 'WWW::OpenSearch::Request' );
42        is( lc $req->method, 'get', 'method' );
43        is( $req->uri, 'http://example.com/?q=iPod&pw=1&format=atom', 'uri' );
44    }
45}
46
47sub _sort_result {
48    my $s = shift;
49    return join( '&',
50        sort { substr( $a, 0, 1 ) cmp substr( $b, 0, 1 ) }
51            split( /\&/, $s ) );
52}
53