File: 11-url.t

package info (click to toggle)
libwww-opensearch-perl 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,663; xml: 31; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 4,745 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use strict;
use warnings;

use Test::More tests => 36;

use_ok( 'WWW::OpenSearch::Description' );
use_ok( 'WWW::OpenSearch::Url' );

{
    my $description = q(<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <Url type="application/rss+xml" 
       template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=rss"/>
</OpenSearchDescription>
);

    my $osd = WWW::OpenSearch::Description->new( $description );
    isa_ok( $osd, 'WWW::OpenSearch::Description' );
    is( $osd->version, '1.1', 'version' );
    is( $osd->ns, 'http://a9.com/-/spec/opensearch/1.1/', 'namespace' );
    is( $osd->urls, 1, 'number of urls' );

    my ( $url ) = $osd->urls;
    isa_ok( $url, 'WWW::OpenSearch::Url' );
    is( $url->type, 'application/rss+xml', 'content type' );
    is( lc $url->method, 'get', 'method' );
    is( $url->template->template,
        'http://example.com/?q={searchTerms}&pw={startPage}&format=rss',
        'template' );
    my $result
        = $url->prepare_query( { searchTerms => 'x', startPage => 1 } );
    is( $result, 'http://example.com/?q=x&pw=1&format=rss', 'prepare_query' );
}

{
    my $description = q(<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <Url type="application/rss+xml"
       template="http://example.com/?q={searchTerms}&amp;pw={startPage}&amp;format=rss"/>
  <Url type="application/atom+xml"
       template="http://example.com/?q={searchTerms}&amp;pw={startPage?}&amp;format=atom"/>
  <Url type="text/html" 
       method="post"
       template="https://intranet/search?format=html">
    <Param name="s" value="{searchTerms}"/>
    <Param name="o" value="{startIndex?}"/>
    <Param name="c" value="{itemsPerPage?}"/>
    <Param name="l" value="{language?}"/>
  </Url>
</OpenSearchDescription>
);

    my $osd = WWW::OpenSearch::Description->new( $description );
    isa_ok( $osd, 'WWW::OpenSearch::Description' );
    is( $osd->urls, 3, 'number of urls' );
    is( $osd->get_best_url, $osd->url->[ 1 ], 'get_best_url' );

    {
        my $url = $osd->url->[ 0 ];
        isa_ok( $url, 'WWW::OpenSearch::Url' );
        is( $url->type, 'application/rss+xml', 'content type' );
        is( lc $url->method, 'get', 'method' );
        is( $url->template->template,
            'http://example.com/?q={searchTerms}&pw={startPage}&format=rss',
            'template' );
    }

    {
        my $url = $osd->url->[ 1 ];
        isa_ok( $url, 'WWW::OpenSearch::Url' );
        is( $url->type, 'application/atom+xml', 'content type' );
        is( lc $url->method, 'get', 'method' );
        is( $url->template->template,
            'http://example.com/?q={searchTerms}&pw={startPage}&format=atom',
            'template'
        );
    }

    {
        my $url = $osd->url->[ 2 ];
        isa_ok( $url, 'WWW::OpenSearch::Url' );
        is( $url->type,      'text/html', 'content type' );
        is( lc $url->method, 'post',      'method' );
        is( $url->template->template, 'https://intranet/search?format=html',
            'template' );
        is_deeply(
            $url->params,
            {   s => '{searchTerms}',
                o => '{startIndex}',
                c => '{itemsPerPage}',
                l => '{language}'
            },
            'params'
        );
        my ( $result, $post ) = $url->prepare_query(
            {   searchTerms  => 'x',
                startIndex   => '1',
                itemsPerPage => 1,
                language     => 'en'
            }
        );
        is( $result,
            'https://intranet/search?format=html',
            'prepare_query (uri)'
        );
        $post = { @$post };
        is_deeply(
            $post,
            { s => 'x', o => 1, c => 1, l => 'en' },
            'prepare_query (params)'
        );
    }
}

{
    my $description = q(<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
  <Url>http://www.unto.net/aws?q={searchTerms}&amp;searchindex=Electronics&amp;flavor=osrss&amp;itempage={startPage}</Url>
</OpenSearchDescription>
);

    my $osd = WWW::OpenSearch::Description->new( $description );
    isa_ok( $osd, 'WWW::OpenSearch::Description' );
    is( $osd->version, '1.0', 'version' );
    is( $osd->ns, 'http://a9.com/-/spec/opensearchrss/1.0/', 'namespace' );
    is( $osd->urls, 1, 'number of urls' );

    my ( $url ) = $osd->urls;
    isa_ok( $url, 'WWW::OpenSearch::Url' );
    is( lc $url->method, 'get', 'method' );
    is( $url->template->template,
        'http://www.unto.net/aws?q={searchTerms}&searchindex=Electronics&flavor=osrss&itempage={startPage}',
        'template'
    );
}