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
|
use strict;
use warnings;
use Test::More import => ['!pass'];
use Dancer ':syntax';
use Dancer::Test;
plan skip_all => "skip test with Test::TCP in win32" if $^O eq 'MSWin32';
plan skip_all => 'Test::TCP is needed to run this test'
unless Dancer::ModuleLoader->load('Test::TCP' => "1.30");
use HTTP::Tiny::NoProxy;
plan tests => 45;
ok(Dancer::App->current->registry->is_empty,
"registry is empty");
ok(Dancer::Plugin::Ajax::ajax( '/', sub { "ajax" } ), "ajax helper called");
ok(!Dancer::App->current->registry->is_empty,
"registry is not empty");
Test::TCP::test_tcp(
client => sub {
my $port = shift;
my $ua = HTTP::Tiny::NoProxy->new;
my @queries = (
{ path => 'req', ajax => 1, success => 1, content => 1 },
{ path => 'req', ajax => 0, success => 0 },
{ path => 'foo', ajax => 1, success => 1, content => 'ajax' },
{ path => 'foo', ajax => 0, success => 1, content => 'not ajax' },
{ path => 'bar', ajax => 1, success => 1, content => 'ajax' },
{ path => 'bar', ajax => 0, success => 1, content => 'not ajax' },
{ path => 'layout', ajax => 0, success => 1, content => 'wibble' },
{ path => 'die', ajax => 1, success => 0 },
{ path => 'layout', ajax => 0, success => 1, content => 'wibble' },
{ path => 'order/A', ajax => 1, success => 1, content => 'A' },
{ path => 'order/*', ajax => 1, success => 1, content => '*' },
{ path => 'order/Z', ajax => 1, success => 1, content => '*' },
);
foreach my $query (@queries) {
my %headers;
$headers{'X-Requested-With'} = 'XMLHttpRequest'
if ( $query->{ajax} == 1);
ok my $res = $ua->get("http://127.0.0.1:$port/" . $query->{path}, { headers => \%headers });
if ( $query->{success} == 1) {
ok $res->{success};
is $res->{content}, $query->{content};
like $res->{headers}{'content-type'}, qr/text\/xml/ if $query->{ajax} == 1;
}
else {
ok !$res->{success};
}
}
# test ajax with content_type to json
my %headers = ( 'X-Requested-With' => 'XMLHttpRequest' );
ok my $res = $ua->get("http://127.0.0.1:$port/ajax.json", { headers => \%headers });
like $res->{headers}{'content-type'}, qr/json/;
},
server => sub {
my $port = shift;
use Dancer;
use Dancer::Plugin::Ajax;
set startup_info => 0, port => $port, server => '127.0.0.1', layout => 'wibble';
ajax '/req' => sub {
return 1;
};
get '/foo' => sub {
return 'not ajax';
};
ajax '/foo' => sub {
return 'ajax';
};
get '/bar' => sub {
return 'not ajax';
};
get '/bar', {ajax => 1} => sub {
return 'ajax';
};
get '/ajax.json' => sub {
content_type('application/json');
return '{"foo":"bar"}';
};
ajax '/die' => sub {
die;
};
get '/layout' => sub {
return setting 'layout';
};
ajax '/order/A' => sub {
return 'A';
};
ajax '/order/*' => sub {
return '*';
};
ajax '/order/Z' => sub {
return 'Z';
};
start();
},
);
|