69ce83379ed534dbb6034279202cb73eef5e422e
[libwww-opensearch-perl.git] / t / 11-url.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 36;
5
6 use_ok( 'WWW::OpenSearch::Description' );
7 use_ok( 'WWW::OpenSearch::Url' );
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 </OpenSearchDescription>
15 );
16
17     my $osd = WWW::OpenSearch::Description->new( $description );
18     isa_ok( $osd, 'WWW::OpenSearch::Description' );
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' );
22
23     my ( $url ) = $osd->urls;
24     isa_ok( $url, 'WWW::OpenSearch::Url' );
25     is( $url->type, 'application/rss+xml', 'content type' );
26     is( lc $url->method, 'get', 'method' );
27     is( $url->template,
28         'http://example.com/?q={searchTerms}&pw={startPage}&format=rss',
29         'template' );
30     my $result
31         = $url->prepare_query( { searchTerms => 'x', startPage => 1 } );
32     is( $result, 'http://example.com/?q=x&pw=1&format=rss', 'prepare_query' );
33 }
34
35 {
36     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
37 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
38   <Url type="application/rss+xml"
39        template="http://example.com/?q={searchTerms}&amp;pw={startPage}&amp;format=rss"/>
40   <Url type="application/atom+xml"
41        template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=atom"/>
42   <Url type="text/html" 
43        method="post"
44        template="https://intranet/search?format=html">
45     <Param name="s" value="{searchTerms}"/>
46     <Param name="o" value="{startIndex?}"/>
47     <Param name="c" value="{itemsPerPage?}"/>
48     <Param name="l" value="{language?}"/>
49   </Url>
50 </OpenSearchDescription>
51 );
52
53     my $osd = WWW::OpenSearch::Description->new( $description );
54     isa_ok( $osd, 'WWW::OpenSearch::Description' );
55     is( $osd->urls, 3, 'number of urls' );
56     is( $osd->get_best_url, $osd->url->[ 1 ], 'get_best_url' );
57
58     {
59         my $url = $osd->url->[ 0 ];
60         isa_ok( $url, 'WWW::OpenSearch::Url' );
61         is( $url->type, 'application/rss+xml', 'content type' );
62         is( lc $url->method, 'get', 'method' );
63         is( $url->template,
64             'http://example.com/?q={searchTerms}&pw={startPage}&format=rss',
65             'template' );
66     }
67
68     {
69         my $url = $osd->url->[ 1 ];
70         isa_ok( $url, 'WWW::OpenSearch::Url' );
71         is( $url->type, 'application/atom+xml', 'content type' );
72         is( lc $url->method, 'get', 'method' );
73         is( $url->template,
74             'http://example.com/?q={searchTerms}&pw={startPage}&format=atom',
75             'template'
76         );
77     }
78
79     {
80         my $url = $osd->url->[ 2 ];
81         isa_ok( $url, 'WWW::OpenSearch::Url' );
82         is( $url->type,      'text/html', 'content type' );
83         is( lc $url->method, 'post',      'method' );
84         is( $url->template, 'https://intranet/search?format=html',
85             'template' );
86         is_deeply(
87             $url->params,
88             {   s => '{searchTerms}',
89                 o => '{startIndex}',
90                 c => '{itemsPerPage}',
91                 l => '{language}'
92             },
93             'params'
94         );
95         my ( $result, $post ) = $url->prepare_query(
96             {   searchTerms  => 'x',
97                 startIndex   => '1',
98                 itemsPerPage => 1,
99                 language     => 'en'
100             }
101         );
102         is( $result,
103             'https://intranet/search?format=html',
104             'prepare_query (uri)'
105         );
106         $post = { @$post };
107         is_deeply(
108             $post,
109             { s => 'x', o => 1, c => 1, l => 'en' },
110             'prepare_query (params)'
111         );
112     }
113 }
114
115 {
116     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
117 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
118   <Url>http://www.unto.net/aws?q={searchTerms}&amp;searchindex=Electronics&amp;flavor=osrss&amp;itempage={startPage}</Url>
119 </OpenSearchDescription>
120 );
121
122     my $osd = WWW::OpenSearch::Description->new( $description );
123     isa_ok( $osd, 'WWW::OpenSearch::Description' );
124     is( $osd->version, '1.0', 'version' );
125     is( $osd->ns, 'http://a9.com/-/spec/opensearchrss/1.0/', 'namespace' );
126     is( $osd->urls, 1, 'number of urls' );
127
128     my ( $url ) = $osd->urls;
129     isa_ok( $url, 'WWW::OpenSearch::Url' );
130     is( lc $url->method, 'get', 'method' );
131     is( $url->template,
132         'http://www.unto.net/aws?q={searchTerms}&searchindex=Electronics&flavor=osrss&itempage={startPage}',
133         'template'
134     );
135 }
136