root/WWW-OpenSearch/trunk/t/11-url.t

Revision 2137 (checked in by bricas, 14 years ago)

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

Line 
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, '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' );
30 }
31
32 {
33     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
34 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
35   <Url type="application/rss+xml"
36        template="http://example.com/?q={searchTerms}&amp;pw={startPage}&amp;format=rss"/>
37   <Url type="application/atom+xml"
38        template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=atom"/>
39   <Url type="text/html"
40        method="post"
41        template="https://intranet/search?format=html">
42     <Param name="s" value="{searchTerms}"/>
43     <Param name="o" value="{startIndex?}"/>
44     <Param name="c" value="{itemsPerPage?}"/>
45     <Param name="l" value="{language?}"/>
46   </Url>
47 </OpenSearchDescription>
48 );
49
50     my $osd = WWW::OpenSearch::Description->new( $description );
51     isa_ok( $osd, 'WWW::OpenSearch::Description' );
52     is( $osd->urls, 3, 'number of urls' );
53     is( $osd->get_best_url, $osd->url->[ 1 ], 'get_best_url' );
54
55     {
56         my $url = $osd->url->[ 0 ];
57         isa_ok( $url, 'WWW::OpenSearch::Url' );
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' );
61     }
62
63     {
64         my $url = $osd->url->[ 1 ];
65         isa_ok( $url, 'WWW::OpenSearch::Url' );
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' );
69     }
70
71     {
72         my $url = $osd->url->[ 2 ];
73         isa_ok( $url, 'WWW::OpenSearch::Url' );
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)' );
82     }
83 }
84
85 {
86     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
87 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
88   <Url>http://www.unto.net/aws?q={searchTerms}&amp;searchindex=Electronics&amp;flavor=osrss&amp;itempage={startPage}</Url>
89 </OpenSearchDescription>
90 );
91
92     my $osd = WWW::OpenSearch::Description->new( $description );
93     isa_ok( $osd, 'WWW::OpenSearch::Description' );
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' );
97
98     my( $url ) = $osd->urls;
99     isa_ok( $url, 'WWW::OpenSearch::Url' );
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' );
102 }
103
Note: See TracBrowser for help on using the browser.