File: 01_simple.t

package info (click to toggle)
libcgi-emulate-psgi-perl 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 138; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use CGI;
use CGI::Emulate::PSGI;
use Test::More;

my $fh = select;

my $err;
$ENV{REQUEST_METHOD} = 'GET';
$ENV{HOK} = 'gahaha';
my $handler = CGI::Emulate::PSGI->handler(
    sub {
        is $ENV{REMOTE_ADDR}, '192.168.1.1';
        is $ENV{REQUEST_METHOD}, 'POST';
        is $ENV{HOK}, 'gahaha', 'env passed in';
        $ENV{HOK} = 'zzz';
        my $q = CGI->new();
        is $q->param('hello'), 'world';
        print "Content-Type: text/html; charset=utf-8\r\n";
        print "X-Foo: Bar\r\n";
        print "Content-Length: 4\r\n";
        print "\r\n";
        print "KTKR";
        print STDERR "hello error\n";
    }
);

open my $in, '<', \do { my $body = "hello=world" };
open my $errors, '>', \$err;
my $res = $handler->(
    +{
        'psgi.input'   => $in,
        REMOTE_ADDR    => '192.168.1.1',
        REQUEST_METHOD => 'POST',
        CONTENT_TYPE   => 'application/x-www-form-urlencoded',
        CONTENT_LENGTH => 11,
        'psgi.errors'  => $errors,
    }
);

my $post_fh = select;

is $res->[0], 200;
my $headers = +{@{$res->[1]}};
is $headers->{'X-Foo'}, 'Bar';
is $headers->{'Content-Type'}, 'text/html; charset=utf-8';
is $headers->{'Content-Length'}, 4;
is_deeply $res->[2], ['KTKR'];
is $ENV{REQUEST_METHOD}, 'GET', 'restored';

is $err, "hello error\n";

is $fh,$post_fh,'SelectSaver worked';

done_testing;