@@ -3,6 +3,8 @@ use Mojo::Base -strict;
3
3
use Test::More;
4
4
use Mojo::Asset::File;
5
5
use Mojo::Asset::Memory;
6
+ use Mojo;
7
+ use Mojo::File qw( tempdir) ;
6
8
use Mojo::Promise;
7
9
use Mojo::Transaction::WebSocket;
8
10
use Mojo::URL;
@@ -1016,6 +1018,89 @@ subtest '301 redirect without compression' => sub {
1016
1018
is $tx -> res-> headers-> location, undef , ' no "Location" value' ;
1017
1019
};
1018
1020
1021
+ subtest ' Download' => sub {
1022
+ my $dir = tempdir;
1023
+ my $no_file = $dir -> child(' no_file' );
1024
+ my $small_file = $dir -> child(' small_file' )-> spew(' x' );
1025
+ my $large_file = $dir -> child(' large_file' )-> spew(' xxxxxxxxxxx' );
1026
+ my $correct_file = $dir -> child(' correct_file' )-> spew(' xxxxxxxxxx' );
1027
+ my $t = Mojo::UserAgent::Transactor-> new;
1028
+
1029
+ subtest ' Partial file exists' => sub {
1030
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1031
+ $head -> res-> headers-> content_length(10);
1032
+ $head -> res-> headers-> accept_ranges(' bytes' );
1033
+ my $tx = $t -> download($head , $small_file );
1034
+ is $tx -> req-> method, ' GET' , ' right method' ;
1035
+ is $tx -> req-> url-> to_abs, ' http://mojolicious.org/release.tar.gz' , ' right URL' ;
1036
+ is $tx -> req-> headers-> range, ' bytes=1-10' , ' right "Range" value' ;
1037
+ };
1038
+
1039
+ subtest ' Partial file exists (with headers)' => sub {
1040
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' => {Accept => ' application/json' });
1041
+ $head -> res-> headers-> content_length(10);
1042
+ $head -> res-> headers-> accept_ranges(' bytes' );
1043
+ my $tx = $t -> download($head , $small_file );
1044
+ is $tx -> req-> method, ' GET' , ' right method' ;
1045
+ is $tx -> req-> url-> to_abs, ' http://mojolicious.org/release.tar.gz' , ' right URL' ;
1046
+ is $tx -> req-> headers-> range, ' bytes=1-10' , ' right "Range" value' ;
1047
+ is $tx -> req-> headers-> accept, ' application/json' , ' right "Accept" value' ;
1048
+ };
1049
+
1050
+ subtest ' Failed HEAD request' => sub {
1051
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1052
+ $head -> res-> error({message => ' Failed to connect' });
1053
+ my $tx = $t -> download($head , $no_file );
1054
+ is $tx -> error-> {message }, ' Failed to connect' , ' right error' ;
1055
+ };
1056
+
1057
+ subtest ' Empty HEAD response' => sub {
1058
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1059
+ my $tx = $t -> download($head , $no_file );
1060
+ is $tx -> req-> method, ' GET' , ' right method' ;
1061
+ is $tx -> req-> url-> to_abs, ' http://mojolicious.org/release.tar.gz' , ' right URL' ;
1062
+ is $tx -> req-> headers-> range, undef , ' no "Range" value' ;
1063
+ };
1064
+
1065
+ subtest ' Empty HEAD response (file exists)' => sub {
1066
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1067
+ my $tx = $t -> download($head , $small_file );
1068
+ is $tx -> error-> {message }, ' Unknown file size' , ' right error' ;
1069
+ };
1070
+
1071
+ subtest ' Target file does not exist' => sub {
1072
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1073
+ $head -> res-> headers-> content_length(10);
1074
+ my $tx = $t -> download($head , $no_file );
1075
+ is $tx -> req-> method, ' GET' , ' right method' ;
1076
+ is $tx -> req-> url-> to_abs, ' http://mojolicious.org/release.tar.gz' , ' right URL' ;
1077
+ is $tx -> req-> headers-> range, undef , ' no "Range" value' ;
1078
+ };
1079
+
1080
+ subtest ' Partial file exists (unsupported server)' => sub {
1081
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1082
+ $head -> res-> headers-> content_length(10);
1083
+ my $tx = $t -> download($head , $small_file );
1084
+ is $tx -> error-> {message }, ' Server does not support partial requests' , ' right error' ;
1085
+ };
1086
+
1087
+ subtest ' Partial file exists (larger than download)' => sub {
1088
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1089
+ $head -> res-> headers-> content_length(10);
1090
+ $head -> res-> headers-> accept_ranges(' bytes' );
1091
+ my $tx = $t -> download($head , $large_file );
1092
+ is $tx -> error-> {message }, ' File size mismatch' , ' right error' ;
1093
+ };
1094
+
1095
+ subtest ' Download already finished' => sub {
1096
+ my $head = $t -> tx(HEAD => ' http://mojolicious.org/release.tar.gz' );
1097
+ $head -> res-> headers-> content_length(10);
1098
+ $head -> res-> headers-> accept_ranges(' bytes' );
1099
+ my $tx = $t -> download($head , $correct_file );
1100
+ is $tx -> error-> {message }, ' Download complete' , ' right error' ;
1101
+ };
1102
+ };
1103
+
1019
1104
subtest ' Promisify' => sub {
1020
1105
my $promise = Mojo::Promise-> new;
1021
1106
my (@results , @errors );
0 commit comments