Test::HTTP::Syntax - HTTP tests in a natural style.
use Test::HTTP::Syntax; use Test::HTTP tests => 9;
or
use Test::HTTP '-syntax', tests => 9;
then
test_http 'echo test' { >> GET /echo/foo >> Accept: text/plain << 200 ~< Content-type: ^text/plain\b << << foo }
Test::HTTP::Syntax is a source filter module designed to work with Test::HTTP. It provides a simple, linewise syntax for specifying HTTP tests in a way that looks a lot like HTTP request and response packets.
All this module does is translate the linewise packet syntax into calls to Test::HTTP.
The actual module used for the tests can be set by setting the variable $Test::HTTP::Syntax::Test_package. It defaults to Test::HTTP.
$Test::HTTP::Syntax::Test_package
Test::HTTP
Test::HTTP::Syntax only filters sections of code which are delimited by a test_http block.
test_http
test_http TEST_NAME { # Code to be filtered # ... }
This gets translated into
{ my $test = Test::HTTP->new(TEST_NAME); # Filtered code # ... }
A request packet consists of a REQUEST START line, 0 or more REQUEST HEADER lines, and an optional REQUEST BODY. The packet ends when a blank line is encountered.
The presence of a REQUEST packet only constructs the request within $test. The request does not get run unless a RESPONSE packet is encountered or $test->run_request() is called explicitly.
$test
$test->run_request()
This line marks the start of a request block.
>> METHOD URI
METHOD is one of GET, PUT, POST, HEAD, or DELETE, and URI is a URI. This line is followed by 0 or more REQUEST HEADERS, and then optionally a REQUEST BODY.
METHOD
GET
PUT
POST
HEAD
DELETE
URI
>> HEADER: VALUE
This sets the value of an HTTP request header.
>> >> body line 1 >> body line 2
This sets the contents of the body of the HTTP packet.
A response packet consists of a RESPONSE START line, 0 or more LITERAL or REGEX RESPONSE HEADER lines, and an optional RESPONSE BODY.
The start of a response packet triggers the execution of the pending request, and starts testing the response received therefrom.
<< NNN
NNN is a 3-digit HTTP response code which we expect to receive.
NNN
<< HEADER: VALUE
Performs a literal match on the value of the HEADER header in the HTTP response packet.
HEADER
~< HEADER: REGEX
Performs a regular expression match on the value of HEADER against the REGEX qr{REGEX}.
<< << body line 1 << body line 2
Performs a literal match on the given body with the body of the HTTP response packet.
http://www.w3.org/Protocols/rfc2616/rfc2616.html, Test::HTTP
Socialtext, Inc. <code@socialtext.com>
<code@socialtext.com>
Copyright 2006 Socialtext, Inc., all rights reserved.
Same terms as Perl.
To install Test::HTTP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::HTTP
CPAN shell
perl -MCPAN -e shell install Test::HTTP
For more information on module installation, please visit the detailed CPAN module installation guide.