root/WWW-OpenSearch/trunk/t/10-description.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 => 48;
5
6 use_ok( 'WWW::OpenSearch::Description' );
7
8 # simple 1.1 OSD
9 {
10     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
11 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
12   <ShortName>Web Search</ShortName>
13   <Description>Use Example.com to search the Web.</Description>
14   <Tags>example web</Tags>
15   <Contact>admin@example.com</Contact>
16   <Url type="application/rss+xml"
17        template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=rss"/>
18 </OpenSearchDescription>
19 );
20
21     my $osd = WWW::OpenSearch::Description->new( $description );
22     isa_ok( $osd, 'WWW::OpenSearch::Description' );
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' );
28
29     # count the urls
30     is( $osd->urls, 1, 'number of url objects' );
31 }
32
33 # complex 1.1 OSD
34 {
35     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
36 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
37   <ShortName>Web Search</ShortName>
38   <Description>Use Example.com to search the Web.</Description>
39   <Tags>example web</Tags>
40   <Contact>admin@example.com</Contact>
41   <Url type="application/rss+xml"
42        template="http://example.com/?q={searchTerms}&amp;pw={startPage}&amp;format=rss"/>
43   <Url type="application/atom+xml"
44        template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=atom"/>
45   <Url type="text/html"
46        method="post"
47        template="https://intranet/search?format=html">
48     <Param name="s" value="{searchTerms}"/>
49     <Param name="o" value="{startIndex?}"/>
50     <Param name="c" value="{itemsPerPage?}"/>
51     <Param name="l" value="{language?}"/>
52   </Url>
53   <LongName>Example.com Web Search</LongName>
54   <Image height="64" width="64" type="image/png">http://example.com/websearch.png</Image>
55   <Image height="16" width="16" type="image/vnd.microsoft.icon">http://example.com/websearch.ico</Image>
56   <Query role="example" searchTerms="cat" />
57   <Developer>Example.com Development Team</Developer>
58   <Attribution>
59     Search data &amp;copy; 2005, Example.com, Inc., All Rights Reserved
60   </Attribution>
61   <SyndicationRight>open</SyndicationRight>
62   <AdultContent>false</AdultContent>
63   <Language>en-us</Language>
64   <OutputEncoding>UTF-8</OutputEncoding>
65   <InputEncoding>UTF-8</InputEncoding>
66 </OpenSearchDescription>
67 );
68
69     my $osd = WWW::OpenSearch::Description->new( $description );
70     isa_ok( $osd, 'WWW::OpenSearch::Description' );
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' );
77     is( $osd->attribution, '
78     Search data &copy; 2005, Example.com, Inc., All Rights Reserved
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' );
85
86     my $queries = $osd->query;
87     is( scalar @$queries, 1, 'number of query objects' );
88     is( $queries->[ 0 ]->role, 'example', 'role' );
89     is( $queries->[ 0 ]->searchTerms, 'cat', 'searchTerms' );
90
91     my $images = $osd->image;
92     is( scalar @$images, 2, 'number of image objects' );
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' );
101
102     # count the urls
103     is( $osd->urls, 3, 'number of url objects' );
104 }
105
106 # 1.0 OSD
107 {
108     my $description = q(<?xml version="1.0" encoding="UTF-8"?>
109 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
110   <Url>http://www.unto.net/aws?q={searchTerms}&amp;searchindex=Electronics
111    &amp;flavor=osrss&amp;itempage={startPage}</Url>
112   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
113   <ShortName>Electronics</ShortName>
114   <LongName>Amazon Electronics</LongName>
115   <Description>Search for electronics on Amazon.com.</Description>
116   <Tags>amazon electronics</Tags>
117   <Image>http://www.unto.net/search/amazon_electronics.gif</Image>
118   <SampleSearch>ipod</SampleSearch>
119   <Developer>DeWitt Clinton</Developer>
120   <Contact>dewitt@unto.net</Contact>
121   <Attribution>Product and search data &amp;copy; 2005, Amazon, Inc.,
122    All Rights Reserved</Attribution>
123   <SyndicationRight>open</SyndicationRight>
124   <AdultContent>false</AdultContent>
125 </OpenSearchDescription>
126 );
127
128     my $osd = WWW::OpenSearch::Description->new( $description );
129     isa_ok( $osd, 'WWW::OpenSearch::Description' );
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' );
139     is( $osd->attribution, 'Product and search data &copy; 2005, Amazon, Inc.,
140    All Rights Reserved', 'attribution' );
141     is( $osd->syndicationright, 'open', 'syndicationright' );
142     is( $osd->adultcontent, 'false', 'adultcontent' );
143
144     # count the urls
145     is( $osd->urls, 1, 'urls' );
146 }
Note: See TracBrowser for help on using the browser.