Skip to content

Commit

Permalink
Update Test::Nginx.
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrSikora committed Aug 26, 2010
1 parent 7c9e924 commit e852769
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 46 deletions.
66 changes: 50 additions & 16 deletions test/lib/Test/Nginx/LWP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sub no_long_string () {
$NoLongString = 1;
}

sub run_test_helper ($);
sub run_test_helper ($$);

$RunTestHelper = \&run_test_helper;

Expand Down Expand Up @@ -96,8 +96,8 @@ sub chunk_it ($$$) {
}
}

sub run_test_helper ($) {
my ($block) = @_;
sub run_test_helper ($$) {
my ($block, $dry_run) = @_;

my $request = $block->request;

Expand Down Expand Up @@ -162,14 +162,23 @@ sub run_test_helper ($) {
#warn "req: ", $req->as_string, "\n";
#warn "DONE!!!!!!!!!!!!!!!!!!!!";

my $res = $UserAgent->request($req);
my $res = HTTP::Response->new;
unless ($dry_run) {
$res = $UserAgent->request($req);
}

#warn "res returned!!!";

if (defined $block->error_code) {
is($res->code, $block->error_code, "$name - status code ok");
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is($res->code, 200, "$name - status code ok");
if (defined $block->error_code) {
is($res->code, $block->error_code, "$name - status code ok");
} else {
is($res->code, 200, "$name - status code ok");
}
}

if (defined $block->response_headers) {
Expand All @@ -179,8 +188,14 @@ sub run_test_helper ($) {
if (!defined $expected_val) {
$expected_val = '';
}
is $expected_val, $val,
"$name - header $key ok";
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is $expected_val, $val,
"$name - header $key ok";
}
}
} elsif (defined $block->response_headers_like) {
my $headers = parse_headers($block->response_headers_like);
Expand All @@ -189,8 +204,14 @@ sub run_test_helper ($) {
if (!defined $expected_val) {
$expected_val = '';
}
like $expected_val, qr/^$val$/,
"$name - header $key like ok";
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
like $expected_val, qr/^$val$/,
"$name - header $key like ok";
}
}
}

Expand All @@ -206,12 +227,18 @@ sub run_test_helper ($) {
$expected =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
#warn show_all_chars($content);

if ($NoLongString) {
is($content, $expected, "$name - response_body - response is expected");
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is_string($content, $expected, "$name - response_body - response is expected");
if ($NoLongString) {
is($content, $expected, "$name - response_body - response is expected");
} else {
is_string($content, $expected, "$name - response_body - response is expected");
}
#is($content, $expected, "$name - response_body - response is expected");
}
#is($content, $expected, "$name - response_body - response is expected");

} elsif (defined $block->response_body_like) {
my $content = $res->content;
Expand All @@ -223,7 +250,14 @@ sub run_test_helper ($) {
$expected_pat =~ s/\$ServerPort\b/$ServerPort/g;
$expected_pat =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
my $summary = trim($content);
like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");

if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
}
}
}

Expand Down
91 changes: 72 additions & 19 deletions test/lib/Test/Nginx/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use List::MoreUtils qw( any );
use IO::Select ();

our $ServerAddr = 'localhost';
our $Timeout = 2;
our $Timeout = $ENV{TEST_NGINX_TIMEOUT} || 2;

use Test::Nginx::Util qw(
setup_server_root
Expand Down Expand Up @@ -44,6 +44,7 @@ use Test::Nginx::Util qw(
no_root_location
server_root
html_dir
server_port
);

#use Smart::Comments::JSON '###';
Expand All @@ -60,12 +61,13 @@ our @EXPORT = qw( plan run_tests run_test
master_process_enabled
no_long_string workers master_on
log_level no_shuffle no_root_location
server_addr server_root html_dir
server_addr server_root html_dir server_port
timeout
);

sub send_request ($$$$);

sub run_test_helper ($);
sub run_test_helper ($$);

sub error_event_handler ($);
sub read_event_handler ($);
Expand All @@ -84,6 +86,14 @@ sub server_addr (@) {
}
}

sub timeout (@) {
if (@_) {
$Timeout = shift;
} else {
$Timeout;
}
}

$RunTestHelper = \&run_test_helper;

sub parse_request ($$) {
Expand Down Expand Up @@ -116,8 +126,8 @@ sub parse_request ($$) {
};
}

sub run_test_helper ($) {
my $block = shift;
sub run_test_helper ($$) {
my ($block, $dry_run) = @_;

my $name = $block->name;

Expand Down Expand Up @@ -215,8 +225,14 @@ $parsed_req->{content}";
$timeout = $Timeout;
}

my $raw_resp = send_request($req, $block->raw_request_middle_delay,
$timeout, $block->name);
my $raw_resp;

if ($dry_run) {
$raw_resp = "200 OK HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
} else {
$raw_resp = send_request($req, $block->raw_request_middle_delay,
$timeout, $block->name);
}

#warn "raw resonse: [$raw_resp]\n";

Expand Down Expand Up @@ -275,18 +291,30 @@ $parsed_req->{content}";
$res->content($decoded);
}

if (defined $block->error_code) {
is($res->code || '', $block->error_code, "$name - status code ok");
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is($res->code || '', 200, "$name - status code ok");
if (defined $block->error_code) {
is($res->code || '', $block->error_code, "$name - status code ok");
} else {
is($res->code || '', 200, "$name - status code ok");
}
}

if (defined $block->response_headers) {
my $headers = parse_headers($block->response_headers);
while (my ($key, $val) = each %$headers) {
if (!defined $val) {
#warn "HIT";
unlike $raw_headers, qr/^\s*\Q$key\E\s*:/ms, "$name - header $key not present in the raw headers";
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
unlike $raw_headers, qr/^\s*\Q$key\E\s*:/ms, "$name - header $key not present in the raw headers";
}
next;
}

Expand All @@ -295,8 +323,14 @@ $parsed_req->{content}";
$actual_val = '';
}

is $actual_val, $val,
"$name - header $key ok";
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is $actual_val, $val,
"$name - header $key ok";
}
}
} elsif (defined $block->response_headers_like) {
my $headers = parse_headers($block->response_headers_like);
Expand All @@ -305,8 +339,14 @@ $parsed_req->{content}";
if (!defined $expected_val) {
$expected_val = '';
}
like $expected_val, qr/^$val$/,
"$name - header $key like ok";
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
like $expected_val, qr/^$val$/,
"$name - header $key like ok";
}
}
}

Expand Down Expand Up @@ -337,10 +377,16 @@ $parsed_req->{content}";
#warn show_all_chars($content);

#warn "no long string: $NoLongString";
if ($NoLongString) {
is($content, $expected, "$name - response_body - response is expected");
if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
is_string($content, $expected, "$name - response_body - response is expected");
if ($NoLongString) {
is($content, $expected, "$name - response_body - response is expected");
} else {
is_string($content, $expected, "$name - response_body - response is expected");
}
}

} elsif (defined $block->response_body_like) {
Expand All @@ -353,7 +399,14 @@ $parsed_req->{content}";
$expected_pat =~ s/\$ServerPort\b/$ServerPort/g;
$expected_pat =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
my $summary = trim($content);
like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");

if ($dry_run) {
SKIP: {
Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
}
} else {
like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
}
}
}

Expand Down
Loading

0 comments on commit e852769

Please sign in to comment.