root/lang/perl/WebService-Simple/tags/release-0.05/t/02_parser_json.t @ 10306

Revision 10118, 1.3 kB (checked in by daisuke, 6 years ago)

add json parser test

Line 
1use strict;
2use Test::More ( tests => 4 );
3
4my $flickr_api_key = $ENV{FLICKR_API_KEY};
5BEGIN
6{
7    use_ok("WebService::Simple");
8}
9
10{
11    my $service = WebService::Simple->new(
12        base_url => "http://api.flickr.com/services/rest/",
13        response_parser => 'JSON',
14        params   => {
15            api_key => $flickr_api_key
16        }
17    );
18
19    isa_ok( $service->response_parser, "WebService::Simple::Parser::JSON");
20
21    SKIP: {
22        if (! $flickr_api_key ) {
23            skip( "Please set FLICKR_API_KEY to enable this test", 2 );
24        }
25   
26        my $response = $service->get( { method => "flickr.test.echo", name => "value", format => "json" } );
27
28        # XXX - This is a hack. Flickr returns values as JSONP construct,
29        # not as a pure JSON. for our parse_response() to work properly,
30        # we need to fix the result first
31        ${ $response->content_ref } =~ s/jsonFlickrApi\((.+)\)/$1/;
32
33        my $json = $response->parse_response;
34   
35        isa_ok( $json, 'HASH' );
36        is_deeply(
37            $json,
38            {
39                'name' => { _content => 'value' },
40                'method' => { _content => 'flickr.test.echo' },
41                'api_key' => { _content => $flickr_api_key },
42                'stat' => 'ok',
43                'format' => { _content => 'json' },
44            }
45        );
46    }
47}
Note: See TracBrowser for help on using the browser.