|
| 1 | +use Mojo::Base -strict; |
| 2 | + |
| 3 | +BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' } |
| 4 | + |
| 5 | +use Test::More; |
| 6 | +use Mojo::File qw(tempdir); |
| 7 | +use Mojo::UserAgent; |
| 8 | +use Mojolicious::Lite; |
| 9 | + |
| 10 | +# Silence |
| 11 | +app->log->level('trace')->unsubscribe('message'); |
| 12 | + |
| 13 | +get '/simple' => sub { |
| 14 | + my $c = shift; |
| 15 | + $c->res->headers->accept_ranges('bytes'); |
| 16 | + return $c->render(data => 'CHUNK1CHUNK2CHUNK3'); |
| 17 | +}; |
| 18 | + |
| 19 | +get '/redirect' => sub { |
| 20 | + my $c = shift; |
| 21 | + return $c->redirect_to('/simple'); |
| 22 | +}; |
| 23 | + |
| 24 | +get '/resume' => sub { |
| 25 | + my $c = shift; |
| 26 | + $c->res->headers->accept_ranges('bytes'); |
| 27 | + return $c->render(data => 'CHUNK1CHUNK2CHUNK3') if $c->req->method eq 'HEAD'; |
| 28 | + my $range = $c->req->headers->range; |
| 29 | + return $c->write('CHUNK1')->finish unless $range; |
| 30 | + return $c->write('CHUNK2')->finish if $range eq 'bytes=6-18'; |
| 31 | + return $c->write('CHUNK3')->finish; |
| 32 | +}; |
| 33 | + |
| 34 | +get '/stream' => sub { |
| 35 | + my $c = shift; |
| 36 | + my $chunks = [qw(CHUNK1 CHUNK2 CHUNK3 CHUNK4)]; |
| 37 | + $c->res->code(200); |
| 38 | + $c->res->headers->content_type('text/plain'); |
| 39 | + my $cb = sub { |
| 40 | + my $content = shift; |
| 41 | + my $chunk = shift @$chunks || ''; |
| 42 | + $content->write_chunk($chunk, $chunk ? __SUB__ : undef); |
| 43 | + }; |
| 44 | + $c->res->content->$cb; |
| 45 | + $c->rendered; |
| 46 | +}; |
| 47 | + |
| 48 | +get '/header' => sub { |
| 49 | + my $c = shift; |
| 50 | + my $custom = $c->req->headers->header('X-Custom-Header') || 'MISSINGHEADER'; |
| 51 | + $c->res->headers->accept_ranges('bytes'); |
| 52 | + return $c->render(data => $custom); |
| 53 | +}; |
| 54 | + |
| 55 | +my $dir = tempdir; |
| 56 | +my $ua = Mojo::UserAgent->new; |
| 57 | + |
| 58 | +subtest 'Basic file download' => sub { |
| 59 | + my $file = $dir->child('simple1.txt'); |
| 60 | + ok $file->download('/simple'), 'file downloaded'; |
| 61 | + ok -e $file, 'file exists'; |
| 62 | + is $file->slurp, 'CHUNK1CHUNK2CHUNK3', 'right content'; |
| 63 | +}; |
| 64 | + |
| 65 | +subtest 'File already downloaded' => sub { |
| 66 | + my $file = $dir->child('simple1.txt'); |
| 67 | + is $file->slurp, 'CHUNK1CHUNK2CHUNK3', 'right content'; |
| 68 | + ok $file->download('/simple'), 'file downloaded'; |
| 69 | + is $file->slurp, 'CHUNK1CHUNK2CHUNK3', 'right content'; |
| 70 | +}; |
| 71 | + |
| 72 | +subtest 'Basic file download (custom header)' => sub { |
| 73 | + my $file = $dir->child('header1.txt'); |
| 74 | + ok $file->download('/header', {headers => {'X-Custom-Header' => 'CORRECTFILECONTENT'}}), 'file downloaded'; |
| 75 | + ok -e $file, 'file exists'; |
| 76 | + is $file->slurp, 'CORRECTFILECONTENT', 'right content'; |
| 77 | + |
| 78 | + my $file2 = $dir->child('header2.txt'); |
| 79 | + ok $file2->download('/header'), 'file downloaded'; |
| 80 | + ok -e $file2, 'file exists'; |
| 81 | + is $file2->slurp, 'MISSINGHEADER', 'right content'; |
| 82 | +}; |
| 83 | + |
| 84 | +subtest 'Basic file download (redirect)' => sub { |
| 85 | + my $file = $dir->child('redirect1.txt'); |
| 86 | + ok $file->download('/redirect'), 'file downloaded'; |
| 87 | + ok -e $file, 'file exists'; |
| 88 | + is $file->slurp, '', 'right content'; |
| 89 | + |
| 90 | + my $ua2 = Mojo::UserAgent->new(max_redirects => 5); |
| 91 | + my $file2 = $dir->child('redirect2.txt'); |
| 92 | + ok $file2->download('/redirect', {ua => $ua2}), 'file downloaded'; |
| 93 | + ok -e $file2, 'file exists'; |
| 94 | + is $file2->slurp, 'CHUNK1CHUNK2CHUNK3', 'right content'; |
| 95 | +}; |
| 96 | + |
| 97 | +subtest 'Existing file is larger' => sub { |
| 98 | + my $file = $dir->child('simple2.txt')->spew('CHUNK1CHUNK2CHUNK3CHUNK4'); |
| 99 | + eval { $file->download('/simple') }; |
| 100 | + like $@, qr/Download error: File size mismatch/, 'right error'; |
| 101 | +}; |
| 102 | + |
| 103 | +subtest 'Resumed file download' => sub { |
| 104 | + my $file = $dir->child('resume1.txt'); |
| 105 | + ok !$file->download('/resume'), 'file partially downloaded'; |
| 106 | + ok !$file->download('/resume'), 'file continued'; |
| 107 | + ok $file->download('/resume'), 'file downloaded'; |
| 108 | + ok -e $file, 'file exists'; |
| 109 | + is $file->slurp, 'CHUNK1CHUNK2CHUNK3', 'right content'; |
| 110 | +}; |
| 111 | + |
| 112 | +subtest 'Missing file' => sub { |
| 113 | + my $file = $dir->child('missing1.txt'); |
| 114 | + eval { $file->download('/missing') }; |
| 115 | + like $@, qr/404 response: Not Found/, 'file not found'; |
| 116 | +}; |
| 117 | + |
| 118 | +subtest 'File of unknown size' => sub { |
| 119 | + my $file = $dir->child('stream1.txt')->spew('C'); |
| 120 | + eval { $file->download('/stream') }; |
| 121 | + like $@, qr/Download error: Unknown file size/, 'right error'; |
| 122 | +}; |
| 123 | + |
| 124 | +subtest 'File of unknown size downloaded' => sub { |
| 125 | + my $file = $dir->child('stream2.txt'); |
| 126 | + ok $file->download('/stream'), 'file downloaded'; |
| 127 | + ok -e $file, 'file exists'; |
| 128 | + is $file->slurp, 'CHUNK1CHUNK2CHUNK3CHUNK4', 'right content'; |
| 129 | +}; |
| 130 | + |
| 131 | +done_testing(); |
0 commit comments