/t/020_interface/poe.t

http://github.com/http-engine/HTTP-Engine · Raku · 119 lines · 106 code · 13 blank · 0 comment · 5 complexity · e8b58770ba5ecf4bb74bfff4d267c267 MD5 · raw file

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