From c8183f96899d669e7190947492d6c1f01c9ab6a7 Mon Sep 17 00:00:00 2001 From: Gabriel Hodoroaga Date: Mon, 3 May 2021 14:48:46 +0200 Subject: [PATCH] Add support for array of requests with delays to pipelined_requests option --- lib/Test/Nginx/Socket.pm | 53 +++++++++++++++++++++++++++++++++++----- t/get_req_from_block.t | 13 +++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/Test/Nginx/Socket.pm b/lib/Test/Nginx/Socket.pm index 9e78e358..54cc1af4 100644 --- a/lib/Test/Nginx/Socket.pm +++ b/lib/Test/Nginx/Socket.pm @@ -376,7 +376,7 @@ sub get_req_from_block ($) { "$name - invalid entries in --- pipelined_requests"); } my $i = 0; - my $prq = ""; + my @prq = (); for my $request (@$reqs) { my $conn_type; if ($i == @$reqs - 1) { @@ -399,15 +399,56 @@ sub get_req_from_block ($) { } else { ($hdr, $is_chunked) = parse_more_headers($more_headers); } + + if (!ref $request) { + # This request is a good old string. + my $r_br = build_request_from_packets($name, $hdr, + $is_chunked, $conn_type, + [$request] ); + + push @prq, {value => $$r_br[0]}; + + } elsif (ref $request eq 'ARRAY') { + # Request expressed as a serie of packets + my @packet_array = (); + for my $one_packet (@$request) { + if (!ref $one_packet) { + # Packet is a string. + push @packet_array, $one_packet; + } elsif (ref $one_packet eq 'HASH'){ + # Packet is a hash with a value... + push @packet_array, $one_packet->{value}; + } else { + bail_out "$name - Invalid syntax. $one_packet should be a string or hash with value."; + } + } + + my $transformed_packet_array = build_request_from_packets($name, $hdr, + $is_chunked, $conn_type, + \@packet_array); + my @transformed_req = (); + my $idx = 0; + for my $one_transformed_packet (@$transformed_packet_array) { + if (!ref $$request[$idx]) { + # it is a string + push @transformed_req, {value => $one_transformed_packet}; + } else { + # Is a HASH (checked above as $one_packet) + $$request[$idx]->{value} = $one_transformed_packet; + push @transformed_req, $$request[$idx]; + } + $idx++; + } + + push @prq, @transformed_req + } else { + bail_out "$name - Invalid syntax. $request should be a string or an array of packets."; + } - my $r_br = build_request_from_packets($name, $hdr, - $is_chunked, $conn_type, - [$request] ); - $prq .= $$r_br[0]; $i++; } - push @req_list, [{value =>$prq}]; + push @req_list, \@prq; } else { my ($is_chunked, $hdr); diff --git a/t/get_req_from_block.t b/t/get_req_from_block.t index 03f179f7..ec369336 100644 --- a/t/get_req_from_block.t +++ b/t/get_req_from_block.t @@ -1,6 +1,6 @@ # Unit tests for Test::Nginx::Socket::get_req_from_block use lib 'lib'; -use Test::Nginx::Socket tests => 7; +use Test::Nginx::Socket tests => 8; my @block_list = blocks(); my $i = 0; # Use $i to make copy/paste of tests easier. @@ -12,8 +12,8 @@ is_deeply(Test::Nginx::Socket::get_req_from_block($block_list[$i]), ."\r\nContent-Length: 15\r\n\r\nvalue=N%3A12345"}]], $block_list[$i++]->name); is_deeply(Test::Nginx::Socket::get_req_from_block($block_list[$i]), - [[{ value => "HEAD /foo HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n". - "GET /bar HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"}]], + [[{ value => "HEAD /foo HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"}, + { value => "GET /bar HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"}]], $block_list[$i++]->name); is_deeply(Test::Nginx::Socket::get_req_from_block($block_list[$i]), [[{ value => "POST /foo HTTP/1.1\r @@ -38,6 +38,10 @@ is_deeply(Test::Nginx::Socket::get_req_from_block($block_list[$i]), [{value =>"GET "}, {value =>"/foo HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"}]], $block_list[$i++]->name); +is_deeply(Test::Nginx::Socket::get_req_from_block($block_list[$i]), + [[{value =>"GET /foo HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"}, + {value =>"GET /bar HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",delay_before => 3}]], + $block_list[$i++]->name); __DATA__ === request: basic string @@ -73,3 +77,6 @@ rub my face in the dirt" --- request eval [["POST /foo\r\n", {value => "value=N%3A12345", delay_before =>3}], [{value => "GET "}, {value => "/foo"}]] +=== pipelined_requests: an array of requests with delays. +--- pipelined_requests eval +["GET /foo", [{value => "GET /bar", delay_before =>3}]]