File: basic.t

package info (click to toggle)
libmojolicious-plugin-renderfile-perl 0.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 100 kB
  • sloc: perl: 70; makefile: 8
file content (113 lines) | stat: -rwxr-xr-x 3,325 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
112
113
use Mojo::Base -strict;

use Test::More;

use Mojolicious::Lite;
use Test::Mojo;

use lib '../lib';

use File::Basename qw/dirname/;
use File::Spec::Functions qw/rel2abs/;

my $FILE = rel2abs( dirname(__FILE__) . '/' . 'sample.txt' );

plugin 'RenderFile';

get '/default' => sub {
    my $self = shift;
    $self->render_file( filepath => $FILE );
};

get '/data' => sub {
    my $self = shift;
    $self->render_file( data => 'data to download', filename => 'sample.txt' );
};

get '/data/custom_content_type' => sub {
    my $self = shift;
    $self->render_file(
        data         => 'data to download',
        filename     => 'sample.txt',
        content_type => 'application/pdf'
    );
};

get '/data/no_filename' => sub {
    my $self = shift;
    $self->render_file( data => 'data to download' );
};

get '/all_attrs' => sub {
    my $self = shift;
    $self->render_file(
        filepath => $FILE,
        filename => 'mysample.txt',
        status => 201,
        format => 'pdf',
        content_disposition => 'inline'
    );
};


my $t = Test::Mojo->new;

$t->get_ok('/default')
    ->status_is(200)
    ->content_is('file to download')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/all_attrs')
    ->status_is(201)
    ->content_is('file to download')
    ->content_type_is('application/pdf;name="mysample.txt"')
    ->header_is( 'Content-Disposition' => 'inline;filename="mysample.txt"' );

$t->get_ok('/default' => { 'Range' => 'bytes=5-' })
    ->status_is(206)
    ->content_is('to download')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/default' => { 'Range' => 'bytes=5-6' })
    ->status_is(206)
    ->content_is('to')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/default' => { 'Range' => 'bytes=17-3' })
    ->status_is(416);

$t->get_ok('/data')
    ->status_is(200)
    ->content_is('data to download')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/data' => { 'Range' => 'bytes=5-' })
    ->status_is(206)
    ->content_is('to download')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/data' => { 'Range' => 'bytes=5-6' })
    ->status_is(206)
    ->content_is('to')
    ->content_type_is('application/x-download;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/data/custom_content_type' => { 'Range' => 'bytes=5-6' })
    ->status_is(206)
    ->content_is('to')
    ->content_type_is('application/pdf;name="sample.txt"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="sample.txt"' );

$t->get_ok('/data/no_filename')
    ->status_is(200)
    ->content_is('data to download')
    ->content_type_is('application/x-download;name="no_filename"')
    ->header_is( 'Content-Disposition' => 'attachment;filename="no_filename"' );


done_testing();