File: 01_request.t

package info (click to toggle)
libpoe-component-client-http-perl 0.949-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 468 kB
  • ctags: 81
  • sloc: perl: 3,566; makefile: 10
file content (206 lines) | stat: -rw-r--r-- 5,098 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# vim: filetype=perl sw=2 ts=2 expandtab

use strict;

BEGIN {
  my @proxies = grep /^http.*proxy$/i, keys %ENV;
  delete @ENV{@proxies} if @proxies;
}

#sub POE::Kernel::ASSERT_DEFAULT () { 1 }

use Test::More;
use POE qw(
  Filter::Stream
  Filter::HTTPD
  Component::Client::HTTP
  Component::Client::Keepalive
);

use Test::POE::Server::TCP;

my @requests;
my $long = <<EOF;
200 OK HTTP/1.1
Connection: close
Content-Length: 300
Bogus-Header: crap

EOF

chomp $long;
$long .= "\n" . "x" x 300;

my $data = <<EOF;
200 OK HTTP/1.1
Connection: close
Content-Length: 118
Content-Type: text/html

<html>
<head><title>Test Page</title></head>
<body><p>This page exists to test POE web components.</p></body>
</html>
EOF

use HTTP::Request::Common qw(GET POST);

#my $cm = POE::Component::Client::Keepalive->new;
POE::Component::Client::HTTP->spawn(
  #MaxSize => MAX_BIG_REQUEST_SIZE,
  MaxSize => 200,
  Timeout => 3,
  #Protocol => 'HTTP/1.1', #default
  #ConnectionManager => $cm, #default
);

POE::Session->create(
  package_states => [
    main => [qw(
      _start
      testd_registered
      testd_client_input
      got_response
      send_after_timeout
    )],
  ],
  inline_states => {
    testd_client_flushed => sub { undef },
    testd_connected      => sub { undef },
    testd_disconnected   => sub { undef },
    _stop                => sub { undef },
  },
);

$poe_kernel->run;
exit 0;

sub _start {
  $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn(
    filter => POE::Filter::Stream->new,
    address => 'localhost',
  );
  my $port = $_[HEAP]->{testd}->port;
  my @badrequests = (
    GET("file:///from/a/local/filesystem"),
  );
  push @badrequests, GET("http://not.localhost.but.invalid/badhost") unless $ENV{NO_NETWORK};

  my @fields = ('field1=111&', 'field2=222');

  @requests = (
    GET("http://localhost:$port/test", Connection => 'close'),
    GET("http://localhost:$port/timeout", Connection => 'close'),
    POST("http://localhost:$port/post1", [field1 => '111', field2 => '222']),
    GET("http://localhost:$port/long", Connection => 'close'),
    HTTP::Request->new(
      POST => "http://localhost:$port/post2",
      [], sub { return shift @fields }
    ),
    @badrequests,
  );
  
  plan tests => @requests * 2 - @badrequests;
}

sub testd_registered {
  my ($kernel) = $_[KERNEL];

  foreach my $r (@requests) {
    $kernel->post(
      'weeble',
      request => 'got_response',
      $r,
    );
  }
}

sub send_after_timeout {
  my ($heap, $id) = @_[HEAP, ARG0];

  $heap->{testd}->send_to_client($id, $data);
  $heap->{testd}->shutdown;
  $_[KERNEL]->post( weeble => 'shutdown' );
}

sub testd_client_input {
  my ($kernel, $heap, $id, $input) = @_[KERNEL, HEAP, ARG0, ARG1];

  $heap->{input_buffer} .= $input;
  my $buffer = $heap->{input_buffer};

  if ($buffer =~ /^GET \/test/) {
    pass("got test request");
    $heap->{input_buffer} = "";
    $heap->{testd}->send_to_client($id, $data);
  }
  elsif ($buffer =~ /^GET \/timeout/) {
    pass("got test request we will let timeout");
    $heap->{input_buffer} = "";

    $kernel->delay_add('send_after_timeout', 3.3, $id);
  }
  elsif ($buffer =~ /^POST \/post1.*field.*field/s) {
    pass("got post request with content");
    $heap->{input_buffer} = "";
    $heap->{testd}->send_to_client($id, $data);
  }
  elsif ($buffer =~ /^POST \/post(\d)/) {
    if ($buffer =~ /field.*field/) {
      pass("got content for post request with callback");
      $heap->{input_buffer} = "";
      $heap->{testd}->send_to_client($id, $data);
    }
  }
  elsif ($buffer =~ /^GET \/long/) {
    pass("sending too much data as requested");
    $heap->{input_buffer} = "";
    $heap->{testd}->send_to_client($id, $long);
  }
  else {
    diag("INPUT: $input");
    diag("unexpected test");
  }
}

sub got_response {
  my ($kernel, $heap, $request_packet, $response_packet) = @_[KERNEL, HEAP, ARG0, ARG1];

  my $request = $request_packet->[0];
  my $response = $response_packet->[0];

  my $request_path = $request->uri->path . ''; # stringify

  if ($request_path =~ m/\/test$/ and $response->code == 200) {
    pass('got 200 response for test request')
  }
  elsif ($request_path =~ m/timeout$/ and $response->code == 408) {
    pass('got 408 response for timed out request')
  }
  elsif ($request_path =~ m/\/post\d$/ and $response->code == 200) {
    pass('got 200 response for post request')
  }
  elsif ($request_path =~ m/\/long$/ and $response->code == 406) {
    pass('got 400 response for long request')
  }
  elsif (
    $request_path =~ m/badhost$/ and
    (
      $response->code == 500 or
      $response->code == 408 or
      $response->code == 303     # some DNS's redirect bad hosts
    )
  ) {
    pass("got " . $response->code . " response for request on bad host")
  }
  elsif ($request_path =~ m/filesystem$/ and $response->code == 400) {
    pass('got 400 response for request with unsupported scheme')
  }
  else {
    fail("unexpected response");
    diag("path($request_path) code(" . $response->code() . ")");
    diag("response(((");
    diag($response->as_string);
    diag(")))");
  }
}