summaryrefslogtreecommitdiff
path: root/t/okay.t
blob: 0f4139dfc22ffc4443bf4d239ebc43d3c4177c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env perl

use Modern::Perl;

use utf8;
use open ':encoding(utf8)';

use Test::More;
use Plack::Test::Agent;
use Plack::Request;

my $app = sub
{
    my $res  = Plack::Request->new( shift );

    my $want = $res->param( 'want' );
    my $have = $res->param( 'have' );
    my $desc = $res->param( 'desc' );

    my ($code, $output) = ( $want eq $have )
                        ? ( 200, 'ok'      )
                        : ( 412, 'not ok'  );

    $output .= ' - ' . $desc if $desc;
    return [ $code, [ 'Content-Type' => 'text/plain' ], [ $output ] ];
};

my $bare_agent   = Plack::Test::Agent->new( app    => $app );
my $server_agent = Plack::Test::Agent->new( app    => $app,
                                            server => 'HTTP::Server::PSGI' );
run_tests_with_agent( $bare_agent );
run_tests_with_agent( $server_agent );

sub run_tests_with_agent
{
    my $agent = shift;
    my $res   = $agent->get( '/?have=foo;want=foo' );
    ok $res->is_success, 'Request should succeed when values match';
    is $res->decoded_content, 'ok', '... with descriptive success message';

    $res    = $agent->get( '/?have=10;want=20' );
    ok ! $res->is_success, 'Request should fail when values do not match';
    is $res->decoded_content, 'not ok', '... with descriptive error';

    my $uri = URI->new( '/' );
    $uri->query_form( have => 'cow', want => 'cow', desc => 'Cow Comparison' );
    $res    = $agent->get( $uri );

    ok $res->is_success, 'Request should succeed when values do';
    is $res->decoded_content, 'ok - Cow Comparison',
        '... including description when provided';
    is $res->content_type, 'text/plain', '... with plain text content';
    is $res->content_charset, 'US-ASCII', '... in ASCII';

    $res    = $agent->post( '/', [ have => 'cow', want => 'pig', desc => 'æ' ] );
    ok ! $res->is_success, 'Request should fail given different values';
    is $res->decoded_content, "not ok - \x{00E6}",
        '... including description when provided';
    is $res->content_type, 'text/plain', '... with plain text content';
    is $res->content_charset, 'UTF-8', '... in ASCII';
}

done_testing;