File: 001_base.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (83 lines) | stat: -rw-r--r-- 2,371 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
use strict;
use warnings;

# There is an issue with HTTP::Parser::XS while parsing an URI with \0
# Using the pure perl via PERL_ONLY works
BEGIN { $ENV{PERL_ONLY} = 1; }

use Test::More import => ['!pass'];
use Dancer::Test;
use HTTP::Date qw( time2str );


plan skip_all => "Skip test with Test::TCP in win32"
  if $^O eq 'MSWin32';
    
plan skip_all => "Test::TCP is required"
  unless Dancer::ModuleLoader->load('Test::TCP' => "1.30");

plan skip_all => "Plack is required"
  unless Dancer::ModuleLoader->load('Plack::Loader');

plan tests => 10;

use Dancer ':syntax';

set public => path( dirname(__FILE__), 'static' );
my $public = setting('public');
my $date = time2str( (stat "$public/hello.txt")[9] );

my $req = [ GET => '/hello.txt' ];
response_is_file $req;

my $resp = Dancer::Test::_get_file_response($req);
is_deeply(
    $resp->headers_to_array,
    [ 'Content-Type' => 'text/plain', 'Last-Modified' => $date ],
    "response header looks good for @$req"
);
is( ref( $resp->{content} ), 'GLOB', "response content looks good for @$req" );

ok $resp = Dancer::Test::_get_file_response( [ GET => "/hello\0.txt" ] );
my $r = Dancer::SharedData->response();
is $r->status,  400;
is $r->content, 'Bad Request';

require HTTP::Tiny::NoProxy;

Test::TCP::test_tcp(
    client => sub {
        my $port = shift;
        my $ua  = HTTP::Tiny::NoProxy->new();
        my $res = $ua->get("http://127.0.0.1:$port/hello%00.txt");
        ok !$res->{success};
        is $res->{status}, 400;
    },
    server => sub {
        my $port = shift;
        setting apphandler => 'PSGI';
        Dancer::Config->load;
        my $app = Dancer::Handler->psgi_app;
        Plack::Loader->auto( port => $port, host => '127.0.0.1' )->run($app);
        Dancer->dance();
    }
);

Test::TCP::test_tcp(
    client => sub {
        my $port = shift;
        my $headers = { 'If-Modified-Since' => $date };
        my $ua  = HTTP::Tiny::NoProxy->new();
        my $res = $ua->get("http://127.0.0.1:$port/hello.txt", { headers => $headers });
        ok !$res->{success};
        is $res->{status}, 304;
    },
    server => sub {
        my $port = shift;
        setting apphandler => 'PSGI';
        Dancer::Config->load;
        my $app = Dancer::Handler->psgi_app;
        Plack::Loader->auto( port => $port, host => '127.0.0.1' )->run($app);
        Dancer->dance();
    }
);