File: jsonp.t

package info (click to toggle)
libplack-perl 0.9941-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 1,472 kB
  • ctags: 600
  • sloc: perl: 7,155; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 935 bytes parent folder | download | duplicates (2)
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
use strict;
use Test::More;
use Plack::Test;
use Plack::Builder;

my $json = '{"foo":"bar"}';

my @app = (
    sub {
        return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ];
    },
    sub {
        return sub {
            my $respond = shift;
            $respond->(
                [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]
            );
        };
    },
);

for my $app ( @app ) {
    $app = builder {
        enable "Plack::Middleware::JSONP";
        $app;
    };

    test_psgi app => $app, client => sub {
        my $cb = shift;

        my $res = $cb->(HTTP::Request->new(GET => 'http://localhost/'));
        is $res->content_type, 'application/json';
        is $res->content, $json;
        $res = $cb->(HTTP::Request->new(GET => 'http://localhost/?callback=foo'));
        is $res->content_type, 'text/javascript';
        is $res->content, "foo($json)";
    };
}

done_testing;