| 1 | package Test::Nginx::LWP; | ||
| 2 | |||
| 3 | use lib 'lib'; | ||
| 4 | use lib 'inc'; | ||
| 5 | use Test::Base -Base; | ||
| 6 | |||
| 7 | our $VERSION = '0.32'; | ||
| 8 | |||
| 9 | our $NoLongString; | ||
| 10 | |||
| 11 | use LWP::UserAgent; | ||
| 12 | use Time::HiRes qw(sleep); | ||
| 13 | use Test::LongString; | ||
| 14 | use Test::Nginx::Util qw( | ||
| 15 | setup_server_root | ||
| 16 | write_config_file | ||
| 17 | get_canon_version | ||
| 18 | get_nginx_version | ||
| 19 | trim | ||
| 20 | show_all_chars | ||
| 21 | parse_headers | ||
| 22 | run_tests | ||
| 23 | $ServerPortForClient | ||
| 24 | $PidFile | ||
| 25 | $ServRoot | ||
| 26 | $ConfFile | ||
| 27 | $ServerPort | ||
| 28 | $RunTestHelper | ||
| 29 | $NoNginxManager | ||
| 30 | $RepeatEach | ||
| 31 | worker_connections | ||
| 32 | master_process_enabled | ||
| 33 | master_on | ||
| 34 | master_off | ||
| 35 | config_preamble | ||
| 36 | repeat_each | ||
| 37 | no_shuffle | ||
| 38 | no_root_location | ||
| 39 | ); | ||
| 40 | |||
| 41 | our $UserAgent = LWP::UserAgent->new; | ||
| 42 | $UserAgent->agent(__PACKAGE__); | ||
| 43 | #$UserAgent->default_headers(HTTP::Headers->new); | ||
| 44 | |||
| 45 | #use Smart::Comments::JSON '##'; | ||
| 46 | |||
| 47 | our @EXPORT = qw( plan run_tests run_test | ||
| 48 | repeat_each config_preamble worker_connections | ||
| 49 | master_process_enabled master_on master_off | ||
| 50 | no_long_string no_shuffle no_root_location); | ||
| 51 | |||
| 52 | sub no_long_string () { | ||
| 53 | $NoLongString = 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub run_test_helper ($$); | ||
| 57 | |||
| 58 | $RunTestHelper = \&run_test_helper; | ||
| 59 | |||
| 60 | sub parse_request ($$) { | ||
| 61 | my ($name, $rrequest) = @_; | ||
| 62 | open my $in, '<', $rrequest; | ||
| 63 | my $first = <$in>; | ||
| 64 | if (!$first) { | ||
| 65 | Test::More::BAIL_OUT("$name - Request line should be non-empty"); | ||
| 66 | die; | ||
| 67 | } | ||
| 68 | $first =~ s/^\s+|\s+$//g; | ||
| 69 | my ($meth, $rel_url) = split /\s+/, $first, 2; | ||
| 70 | my $url = "http://localhost:$ServerPortForClient" . $rel_url; | ||
| 71 | |||
| 72 | my $content = do { local $/; <$in> }; | ||
| 73 | if ($content) { | ||
| 74 | $content =~ s/^\s+|\s+$//s; | ||
| 75 | } | ||
| 76 | |||
| 77 | close $in; | ||
| 78 | |||
| 79 | return { | ||
| 80 | method => $meth, | ||
| 81 | url => $url, | ||
| 82 | content => $content, | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub chunk_it ($$$) { | ||
| 87 | my ($chunks, $start_delay, $middle_delay) = @_; | ||
| 88 | my $i = 0; | ||
| 89 | return sub { | ||
| 90 | if ($i == 0) { | ||
| 91 | if ($start_delay) { | ||
| 92 | sleep($start_delay); | ||
| 93 | } | ||
| 94 | } elsif ($middle_delay) { | ||
| 95 | sleep($middle_delay); | ||
| 96 | } | ||
| 97 | return $chunks->[$i++]; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub run_test_helper ($$) { | ||
| 102 | my ($block, $dry_run) = @_; | ||
| 103 | |||
| 104 | my $request = $block->request; | ||
| 105 | |||
| 106 | my $name = $block->name; | ||
| 107 | |||
| 108 | #if (defined $TODO) { | ||
| 109 | #$name .= "# $TODO"; | ||
| 110 | #} | ||
| 111 | |||
| 112 | my $req_spec = parse_request($name, \$request); | ||
| 113 | ## $req_spec | ||
| 114 | my $method = $req_spec->{method}; | ||
| 115 | my $req = HTTP::Request->new($method); | ||
| 116 | my $content = $req_spec->{content}; | ||
| 117 | |||
| 118 | if (defined ($block->request_headers)) { | ||
| 119 | my $headers = parse_headers($block->request_headers); | ||
| 120 | while (my ($key, $val) = each %$headers) { | ||
| 121 | $req->header($key => $val); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | #$req->header('Accept', '*/*'); | ||
| 126 | $req->url($req_spec->{url}); | ||
| 127 | if ($content) { | ||
| 128 | if ($method eq 'GET' or $method eq 'HEAD') { | ||
| 129 | croak "HTTP 1.0/1.1 $method request should not have content: $content"; | ||
| 130 | } | ||
| 131 | $req->content($content); | ||
| 132 | } elsif ($method eq 'POST' or $method eq 'PUT') { | ||
| 133 | my $chunks = $block->chunked_body; | ||
| 134 | if (defined $chunks) { | ||
| 135 | if (!ref $chunks or ref $chunks ne 'ARRAY') { | ||
| 136 | |||
| 137 | Test::More::BAIL_OUT("$name - --- chunked_body should takes a Perl array ref as its value"); | ||
| 138 | } | ||
| 139 | |||
| 140 | my $start_delay = $block->start_chunk_delay || 0; | ||
| 141 | my $middle_delay = $block->middle_chunk_delay || 0; | ||
| 142 | $req->content(chunk_it($chunks, $start_delay, $middle_delay)); | ||
| 143 | if (!defined $req->header('Content-Type')) { | ||
| 144 | $req->header('Content-Type' => 'text/plain'); | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | if (!defined $req->header('Content-Type')) { | ||
| 148 | $req->header('Content-Type' => 'text/plain'); | ||
| 149 | } | ||
| 150 | |||
| 151 | $req->header('Content-Length' => 0); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($block->more_headers) { | ||
| 156 | my @headers = split /\n+/, $block->more_headers; | ||
| 157 | for my $header (@headers) { | ||
| 158 | next if $header =~ /^\s*\#/; | ||
| 159 | my ($key, $val) = split /:\s*/, $header, 2; | ||
| 160 | #warn "[$key, $val]\n"; | ||
| 161 | $req->header($key => $val); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | #warn "req: ", $req->as_string, "\n"; | ||
| 166 | #warn "DONE!!!!!!!!!!!!!!!!!!!!"; | ||
| 167 | |||
| 168 | my $res = HTTP::Response->new; | ||
| 169 | unless ($dry_run) { | ||
| 170 | $res = $UserAgent->request($req); | ||
| 171 | } | ||
| 172 | |||
| 173 | #warn "res returned!!!"; | ||
| 174 | |||
| 175 | if ($dry_run) { | ||
| 176 | SKIP: { | ||
| 177 | Test::More::skip("$name - tests skipped due to $dry_run", 1); | ||
| 178 | } | ||
| 179 | } else { | ||
| 180 | if (defined $block->error_code) { | ||
| 181 | is($res->code, $block->error_code, "$name - status code ok"); | ||
| 182 | } else { | ||
| 183 | is($res->code, 200, "$name - status code ok"); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | if (defined $block->response_headers) { | ||
| 188 | my $headers = parse_headers($block->response_headers); | ||
| 189 | while (my ($key, $val) = each %$headers) { | ||
| 190 | my $expected_val = $res->header($key); | ||
| 191 | if (!defined $expected_val) { | ||
| 192 | $expected_val = ''; | ||
| 193 | } | ||
| 194 | if ($dry_run) { | ||
| 195 | SKIP: { | ||
| 196 | Test::More::skip("$name - tests skipped due to $dry_run", 1); | ||
| 197 | } | ||
| 198 | } else { | ||
| 199 | is $expected_val, $val, | ||
| 200 | "$name - header $key ok"; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } elsif (defined $block->response_headers_like) { | ||
| 204 | my $headers = parse_headers($block->response_headers_like); | ||
| 205 | while (my ($key, $val) = each %$headers) { | ||
| 206 | my $expected_val = $res->header($key); | ||
| 207 | if (!defined $expected_val) { | ||
| 208 | $expected_val = ''; | ||
| 209 | } | ||
| 210 | if ($dry_run) { | ||
| 211 | SKIP: { | ||
| 212 | Test::More::skip("$name - tests skipped due to $dry_run", 1); | ||
| 213 | } | ||
| 214 | } else { | ||
| 215 | like $expected_val, qr/^$val$/, | ||
| 216 | "$name - header $key like ok"; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | if (defined $block->response_body) { | ||
| 222 | my $content = $res->content; | ||
| 223 | if (defined $content) { | ||
| 224 | $content =~ s/^TE: deflate,gzip;q=0\.3\r\n//gms; | ||
| 225 | } | ||
| 226 | |||
| 227 | $content =~ s/^Connection: TE, close\r\n//gms; | ||
| 228 | my $expected = $block->response_body; | ||
| 229 | $expected =~ s/\$ServerPort\b/$ServerPort/g; | ||
| 230 | $expected =~ s/\$ServerPortForClient\b/$ServerPortForClient/g; | ||
| 231 | #warn show_all_chars($content); | ||
| 232 | |||
| 233 | if ($dry_run) { | ||
| 234 | SKIP: { | ||
| 235 | Test::More::skip("$name - tests skipped due to $dry_run", 1); | ||
| 236 | } | ||
| 237 | } else { | ||
| 238 | if ($NoLongString) { | ||
| 239 | is($content, $expected, "$name - response_body - response is expected"); | ||
| 240 | } else { | ||
| 241 | is_string($content, $expected, "$name - response_body - response is expected"); | ||
| 242 | } | ||
| 243 | #is($content, $expected, "$name - response_body - response is expected"); | ||
| 244 | } | ||
| 245 | |||
| 246 | } elsif (defined $block->response_body_like) { | ||
| 247 | my $content = $res->content; | ||
| 248 | if (defined $content) { | ||
| 249 | $content =~ s/^TE: deflate,gzip;q=0\.3\r\n//gms; | ||
| 250 | } | ||
| 251 | $content =~ s/^Connection: TE, close\r\n//gms; | ||
| 252 | my $expected_pat = $block->response_body_like; | ||
| 253 | $expected_pat =~ s/\$ServerPort\b/$ServerPort/g; | ||
| 254 | $expected_pat =~ s/\$ServerPortForClient\b/$ServerPortForClient/g; | ||
| 255 | my $summary = trim($content); | ||
| 256 | |||
| 257 | if ($dry_run) { | ||
| 258 | SKIP: { | ||
| 259 | Test::More::skip("$name - tests skipped due to $dry_run", 1); | ||
| 260 | } | ||
| 261 | } else { | ||
| 262 | like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)"); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | 1; | ||
| 268 | __END__ |