Last active
April 1, 2022 00:49
-
-
Save jhthorsen/7761cde29c258094f6b4d9865041e964 to your computer and use it in GitHub Desktop.
Test for changing max_message_size dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Test2::V0; | |
use Test::Mojo; | |
# Note that this does not work for small HTTP messages, since they might get | |
# parsed way before the "progress" event gets fired, and the "max_message_size" | |
# check is done. | |
use Mojolicious::Lite -signatures; | |
hook after_build_tx => sub ($tx, $app) { | |
# Override default size for testing purpose | |
$tx->req->max_message_size(2_000_000); | |
$tx->req->once(progress => sub ($req, @) { | |
# Set a very big size for /upload/big | |
$req->max_message_size(512_000_000) if $req->url =~ m!/upload/big!; | |
}); | |
}; | |
post '/upload/:type', [part => [qw(big small)]] => sub ($c) { | |
$c->render(json => { | |
error => $c->tx->error, | |
size => $c->tx->req->body_size, | |
}); | |
}; | |
my $t = Test::Mojo->new; | |
my $big_body = 'a' x 10_000_001; | |
subtest 'successful upload' => sub { | |
$t->post_ok('/upload/big' => $big_body)->status_is(200) | |
->json_is('/error/message', undef) | |
->json_is('/size', 10_000_001); | |
}; | |
subtest 'fail upload' => sub { | |
$t->post_ok('/upload/small' => $big_body)->status_is(200) | |
->json_is('/error/message', 'Maximum message size exceeded') | |
->json_has('/size'); | |
ok $t->tx->res->json->{size} < 3_000_000, 'did not read all of big body'; | |
}; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment