File: ajax.t

package info (click to toggle)
libdancer2-plugin-ajax-perl 0.400000-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: perl: 199; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,619 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common qw<GET POST DELETE PUT>;

{
    package AjaxApp;
    use Dancer2;
    use Dancer2::Plugin::Ajax;

    set plugins => { 'Ajax' => { content_type => 'application/json' } };

    ajax '/test' => sub {
        "{some: 'json'}";
    };

    get '/test' => sub {
        "some text";
    };

    ajax '/another/test' => sub {
        "{more: 'json'}";
    };

    ajax ['put', 'delete', 'get'] => "/more/test" => sub {
        "{some: 'json'}";
    };

    ajax '/subargs' => sub {
        return $_[0]->app->request->to_string;
    };
}

my $test = Plack::Test->create( AjaxApp->to_app );

subtest 'Ajax with POST' => sub {
    my $res = $test->request(
        POST '/test',
            'X-Requested-With' => 'XMLHttpRequest'
    );

    is( $res->content, q({some: 'json'}), 'ajax works with POST' );
    is( $res->content_type, 'application/json', 'ajax content type' );
};

is(
    $test->request( GET '/test', 'X-Requested-With' => 'XMLHttpRequest' )
         ->content,
    q({some: 'json'}),
    'ajax works with GET',
);

is(
    $test->request(
        GET '/more/test',
            'X-Requested-With' => 'XMLHttpRequest',
    )->content,
    q({some: 'json'}),
    'ajax works with GET on multi-method route',
);

is(
    $test->request(
        PUT '/more/test',
            'X-Requested-With' => 'XMLHttpRequest'
    )->content,
    q({some: 'json'}),
    'ajax works with PUT on multi-method route',
);

is(
    $test->request(
        DELETE '/more/test',
            'X-Requested-With' => 'XMLHttpRequest'
    )->content,
    q({some: 'json'}),
    'ajax works with DELETE on multi-method route',
);

is(
    $test->request(
        POST '/more/test',
            'X-Requested-With' => 'XMLHttpRequest'
    )->code,
    404,
    'ajax multi-method route only valid for the defined routes',
);

is(
    $test->request( POST '/another/test' )->code,
    404,
    'ajax route passed for non-XMLHttpRequest',
);

    # GitHub #143 - response content type not munged if ajax route passes
subtest 'GH #143: response content with ajax route' => sub {
    my $res = $test->request( GET '/test' );
    is $res->code, 200, 'ajax route passed for non-XMLHttpRequest';
    is $res->content, 'some text', 'ajax route has proper content for GET without XHR';
    is $res->content_type, 'text/html', 'content type on non-XMLHttpRequest not munged';
};

like(
    $test->request( GET '/subargs', 'X-Requested-With' => 'XMLHttpRequest' )
         ->content,
    qr{/subargs},
    'GH #7: ajax routes have access to $self',
);

done_testing;