File: Test.pm

package info (click to toggle)
libplack-app-proxy-perl 0.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: perl: 1,897; makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,691 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package Plack::App::Proxy::Test;
use strict;
use warnings;
use Carp;
use Plack::Loader;
use Plack::Test;
use Test::More;
use Test::TCP;
use LWP::UserAgent;
use base Exporter::;
our @EXPORT = qw(test_proxy);

BEGIN {
  # disable HTTP proxy when testing since we are connecting to localhost
  delete $ENV{http_proxy};
}

our @BACKENDS = qw/LWP AnyEvent::HTTP/;

sub test_proxy {
    my %args = @_;

    local $Plack::Test::Impl = 'Server';

    my $client = delete $args{client} or croak "client test code needed";
    my $app    = delete $args{app}    or croak "app needed";
    my $proxy  = delete $args{proxy}  or croak "proxy needed";
    my $host   = delete $args{host} || '127.0.0.1';

    for my $backend (@BACKENDS) {

        local $ENV{PLACK_PROXY_BACKEND} = $backend;

        test_tcp(
            client => sub {
                my $port = shift;
                test_psgi(
                    app => $proxy->( $host, $port ),
                    client => $client,
                    host => $host,
                    # disable the auto redirection of LWP::UA
                    ua => LWP::UserAgent->new( max_redirect => 0 ),
                );
            },
            server => sub {
                my $port = shift;

                # Use an ordinary server.
                local $ENV{PLACK_SERVER} = 'Standalone';

                my $server = Plack::Loader->auto(port => $port, host => $host);
                $server->run($app);
            },
        );
    }
}

1;

__END__

=head1 NAME

Plack::App::Proxy::Test - Is utilities to test Plack::App::Proxy.

=head1 SYNOPSIS

  test_proxy(
      app   => $backend_app,
      proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]") },
      client => sub {
          my $cb = shift;
          my $res = $cb->(GET '/');
          ok $res->is_success, "Check the status line.";
      },
  );

=head1 DESCRIPTION

Plack::App::Proxy::Test provids test_proxy function which wraps 
test_psgi of Plack::Test simply. 

=head1 FUNCTIONS

=over 4

=item test_proxy

  test_proxy app    => $app, 
             proxy  => $proxy_cb->($app_host, $app_port), 
             client => $client_cb->($cb);

=back

test_proxy runs two servers, 'C<app>' as an origin server and the proxy server.
In 'C<proxy>' callback, you should create the proxy server instance to send 
requests to 'C<app>' server. Then 'C<client>' callback is called to run your 
tests. In 'C<client>' callback, all HTTP requests are sent to 'C<proxy>' 
server. (And the proxy server will proxy your request to the app server.)

=head1 AUTHOR

Masahiro Honma E<lt>hiratara@cpan.orgE<gt>

=cut

=head1 SEE ALSO

L<Plack::App::Proxy> L<Plack::Test>

=cut