Plack-Test-AnyEvent

 view release on metacpan or  search on metacpan

t/lib/Plack/Test/AnyEvent/Test.pm  view on Meta::CPAN

            my ( $error ) = @_;

            unless($error =~ /bad apple/) {
                warn $error;
            }
            return 1;
        })};
    };
}

sub test_simple_app :Test(3) {
    my $app = sub {
        return [
            200,
            ['Content-Type' => 'text/plain'],
            ['OK'],
        ];
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        is $res->content_type, 'text/plain';
        is $res->content, 'OK';
    };
}

sub test_delayed_app :Test(3) {
    my $app = sub {
        return sub {
            my ( $respond ) = @_;

            my $timer;
            $timer = AnyEvent->timer(
                after => 1,
                cb    => sub {
                    undef $timer;
                    $respond->([
                        200,
                        ['Content-Type' => 'text/plain'],
                        ['OK'],
                    ]);
                },
            );
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        is $res->content_type, 'text/plain';
        is $res->content, 'OK';
    };

}

sub test_streaming_app :Test(6) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            my $writer = $respond->([
                200,
                ['Content-Type' => 'text/plain'],
            ]);
            my $timer;
            my $i  = 0;

            $timer = AnyEvent->timer(
                interval => 1,
                cb       => sub {
                    $writer->write($i++);
                    if($i > 2) {
                        $writer->close;
                        undef $timer;
                    }
                },
            );
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        is $res->content_type, 'text/plain';
        is $res->content, '';

        my $i = 0;
        $res->on_content_received(sub {
            my ( $chunk ) = @_;
            is $chunk, $i++;
        });
        $res->recv;
    };
}

sub test_infinite_app :Test(6) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            my $writer = $respond->([
                200,
                ['Content-Type' => 'text/plain'],
            ]);
            my $timer;
            my $i  = 0;
            $timer = AnyEvent->timer(
                interval => 1,
                cb       => sub {
                    local $SIG{__WARN__} = sub {}; # $writer complains if its

t/lib/Plack/Test/AnyEvent/Test.pm  view on Meta::CPAN

            );
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        like $res->content, qr/Hey!/;
    };
}

sub test_bad_app_die_in_response :Test(2) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            die "bad apple";
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        throws_ok {
            $cb->(GET '/');
        } qr/bad apple/;
    };
}

sub test_responsible_app_die_in_response :Test(2) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            eval {
                die "bad apple";
            };
            $respond->([
                200,
                ['Content-Type' => 'text/plain'],
                ['All Alright'],
            ]);
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        like $res->content, qr/All Alright/;
    };
}

sub test_bad_app_streaming :Test(2) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            my $timer;
            $timer = AnyEvent->timer(
                after => 0.5,
                cb    => sub {
                    my $writer = $respond->([
                        200,
                        ['Content-Type' => 'text/plain'],
                    ]);

                    $timer = AnyEvent->timer(
                        after => 0.5,
                        cb    => sub {
                            undef $timer;
                            die "bad apple";
                        },
                    );
                },
            );
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        my $timer = AnyEvent->timer(
            after => 5,
            cb    => sub {
                $res->send; # self-inflicted timeout
            },
        );
        is $res->code, 200;
        $res->on_content_received(sub {
            # no-op
        });
        throws_ok {
            $res->recv;
        } qr/bad apple/;
    };
}

sub test_responsible_app_streaming :Test(2) {
    my $app = sub {
        my ( $env ) = @_;

        return sub {
            my ( $respond ) = @_;

            my $timer;
            $timer = AnyEvent->timer(
                after => 0.5,
                cb    => sub {
                    my $writer = $respond->([
                        200,
                        ['Content-Type' => 'text/plain'],
                    ]);

                    $timer = AnyEvent->timer(
                        after => 0.5,
                        cb    => sub {
                            eval {
                                die "bad apple";
                            };
                            $writer->write('All Alright');
                            $writer->close;
                            undef $timer;
                        },
                    );
                },
            );
        };
    };

    test_psgi $app, sub {
        my ( $cb ) = @_;

        my $res = $cb->(GET '/');
        is $res->code, 200;
        $res->on_content_received(sub {
            # no-op
        });
        lives_ok {
            $res->recv;
        };
    };
}

sub test_infinite_request_shutdown :Test {
    my $app = sub {
        return sub {
            my ( $respond ) = @_;

            my $writer = $respond->([
                200,
                ['Content-Type' => 'text/plain'],
            ]);

            my $timer;
            my $count = 0;
            $timer = AnyEvent->timer(
                interval => 0.1,
                cb       => sub {



( run in 1.673 second using v1.01-cache-2.11-cpan-437f7b0c052 )