NAME

Test::HTTP::Syntax - HTTP tests in a natural style.

SYNOPSIS

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
}

DESCRIPTION

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.

SYNTAX

test_http block

Test::HTTP::Syntax only filters sections of code which are delimited by a test_http block.

test_http TEST_NAME {
    # Code to be filtered
    # ...
}

This gets translated into

{
    my $test = Test::HTTP->new(TEST_NAME);
    # Filtered code
    # ...
}

REQUESTS

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.

REQUEST START

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.

REQUEST HEADER

>> HEADER: VALUE

This sets the value of an HTTP request header.

REQUEST BODY

>>
>> body line 1
>> body line 2

This sets the contents of the body of the HTTP packet.

RESPONSES

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.

RESPONSE START

<< NNN

NNN is a 3-digit HTTP response code which we expect to receive.

LITERAL RESPONSE HEADER

<< HEADER: VALUE

Performs a literal match on the value of the HEADER header in the HTTP response packet.

REGEX RESPONSE HEADER

~< HEADER: REGEX

Performs a regular expression match on the value of HEADER against the REGEX qr{REGEX}.

RESPONSE BODY

<<
<< body line 1
<< body line 2

Performs a literal match on the given body with the body of the HTTP response packet.

SEE ALSO

http://www.w3.org/Protocols/rfc2616/rfc2616.html, Test::HTTP

AUTHOR

Socialtext, Inc. <code@socialtext.com>

COPYRIGHT & LICENSE

Copyright 2006 Socialtext, Inc., all rights reserved.

Same terms as Perl.