PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/t/020_interface/poe.t

http://github.com/http-engine/HTTP-Engine
Unknown | 119 lines | 106 code | 13 blank | 0 comment | 0 complexity | e8b58770ba5ecf4bb74bfff4d267c267 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4use Test::TCP;
  5use HTTP::Engine;
  6
  7eval "use POE;use POE::Session;";
  8plan skip_all => "this test requires POE" if $@;
  9eval "use POE::Component::Client::HTTP;";
 10plan skip_all => "this test requires POE::Component::Client::HTTP" if $@;
 11
 12plan tests => 5;
 13
 14use_ok 'HTTP::Engine::Interface::POE';
 15
 16note "POE: $POE::VERSION";
 17note "PoCo::C::HTTP: $POE::Component::Client::HTTP::VERSION";
 18
 19my $port = empty_port;
 20my $port2 = empty_port($port);
 21
 22HTTP::Engine::Interface::POE->new(
 23    request_handler => sub {
 24        my $req = shift;
 25        HTTP::Engine::Response->new(
 26            status => 200,
 27            body   => $req->method,
 28        );
 29    },
 30    alias => 'he',
 31    port => $port,
 32)->run;
 33
 34HTTP::Engine::Interface::POE->new(
 35    request_handler => sub {
 36        my $req = shift;
 37        HTTP::Engine::Response->new(
 38            status => 200,
 39            body   => $req->method,
 40        );
 41    },
 42    port => $port2,
 43)->run;
 44
 45POE::Component::Client::HTTP->spawn(
 46    Alias => 'ua',
 47);
 48
 49my %case = (
 50    'HTTP/1.0' => sub {
 51        my($req, $res) = @_;
 52        is $res->code, 200;
 53        is $res->content, 'GET';
 54    },
 55    'HTTP/1.1' => sub {
 56        my($req, $res) = @_;
 57        is $res->code, 200;
 58        is $res->content, 'GET';
 59    },
 60);
 61
 62POE::Session->create(
 63    inline_states => {
 64        _start => sub {
 65            my ($kernel, ) = @_[POE::Session::KERNEL()];
 66            my $req = HTTP::Request->new(
 67                'GET',
 68                "http://localhost:$port/",
 69            );
 70            $req->protocol('HTTP/1.1'); # POST request in HTTP/1.1 is valid.
 71            $kernel->post(
 72                'ua',
 73                'request',
 74                'response',
 75                $req,
 76            );
 77            do {
 78                my $req = HTTP::Request->new(
 79                    'POST',
 80                    "http://localhost:$port/",
 81                    HTTP::Headers::Fast->new(),
 82                    "FOO=BAR",
 83                );
 84                $req->protocol('HTTP/0.9'); # POST request in HTTP/0.9 is invalid.
 85                $kernel->post(
 86                    'ua',
 87                    'request',
 88                    'response',
 89                    $req,
 90                );
 91            };
 92            do {
 93                my $req = HTTP::Request->new(
 94                    'GET',
 95                    "http://localhost:$port2/",
 96                    HTTP::Headers::Fast->new(),
 97                );
 98                $req->protocol('HTTP/1.0');
 99                $kernel->post(
100                    'ua',
101                    'request',
102                    'response',
103                    $req,
104                );
105            };
106        },
107        'response' => sub {
108            my ($kernel, ) = @_[POE::Session::KERNEL()];
109            my $req = @_[POE::Session::ARG0()]->[0];
110            my $res = @_[POE::Session::ARG1()]->[0];
111
112            (delete $case{$req->protocol})->($req, $res);
113            $kernel->stop unless %case;
114        },
115    },
116);
117
118POE::Kernel->run;
119