Skip to content

Commit

Permalink
Throw an exception if double rendering is attempted
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 26, 2022
1 parent 57f6bb6 commit cd6627d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

9.25 2022-04-19
9.25 2022-04-25
- Improved Mojolicious::Renderer to throw an exception if rendering is attempted more than once.

9.24 2022-04-18
- Improved Mojo::Headers to output headers in a canonical order.
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojolicious/Renderer.pm
@@ -1,6 +1,7 @@
package Mojolicious::Renderer;
use Mojo::Base -base;

use Carp qw(croak);
use Mojo::Cache;
use Mojo::DynamicMethods;
use Mojo::File qw(curfile path);
Expand Down Expand Up @@ -123,6 +124,9 @@ sub render {
sub respond {
my ($self, $c, $output, $format, $status) = @_;

my $stash = $c->stash;
croak 'A response has already been rendered' if $stash->{'mojo.respond'}++ && $stash->{'mojo.respond'};

# Gzip compression
my $res = $c->res;
if ($self->compress && length($output) >= $self->min_compress_size) {
Expand Down
8 changes: 8 additions & 0 deletions t/mojolicious/renderer.t
Expand Up @@ -145,6 +145,14 @@ ok !$c->res->headers->vary, 'no "Vary" value';
ok !$c->res->headers->content_encoding, 'no "Content-Encoding" value';
is $c->res->body, $output, 'same string';

subtest 'Response has already been rendered' => sub {
my $c = $app->build_controller;
$c->render(text => 'First call');
is $c->render_to_string(text => 'Unrelated call'), 'Unrelated call', 'right result';
eval { $c->render(text => 'Second call') };
like $@, qr/A response has already been rendered/, 'right error';
};

# Missing method (AUTOLOAD)
my $class = ref $first->myapp;
eval { $first->myapp->missing };
Expand Down

0 comments on commit cd6627d

Please sign in to comment.