Kelp::Test - Automated tests for a Kelp web app
use MyApp; use Kelp::Test; use HTTP::Request::Common; my $app = MyApp->new; my $t = Kelp::Test->new( app => $app ); $t->request( GET '/path' ) ->code_is(200) ->content_is("It works"); $t->request( POST '/api' ) ->json_cmp({auth => 1});
This module provides basic tools for testing a Kelp based web application. It is object oriented, and all methods return $self, so they can be chained together. Testing is done by sending HTTP requests to an already built application and analyzing the response. Therefore, each test usually begins with the "request" method, which takes a single HTTP::Request parameter. It sends the request to the web app and saves the response as an HTTP::Response object.
$self
This module sets the KELP_TESTING environmental variable to a true value.
KELP_TESTING
The Kelp::Test object is instantiated with single attribute called app. It is a reference to a Kelp based web app.
app
my $myapp = MyApp->new; my $t = Kelp::Test->new( app => $myapp );
From this point on, all requests run with $t->request will be sent to $app.
$t->request
$app
Each time $t->request is used to send a request, an HTTP::Response object is returned and saved in the res attribute. You can use it to run tests, although as you will see, this module provides methods which make this a lot easier. It is recommended that you use the convenience methods rather than using res.
res
$t->request( GET '/path' ) is $t->res->code, 200, "It's a success";
An HTTP::Cookies object containing the cookie jar for all tests.
request( $http_request )
Takes an HTTP::Request object and sends it to the application. When the HTTP::Response object is returned, it is initialized in the "res" attribute. It is very convenient to use HTTP::Request::Common in your test modules, so you can take advantage of the simplified syntax for creating an HTTP request.
$t->request( POST '/api', [ user => 'jane' ] );
This method returns $self, so other methods can be chained after it.
request_ok( $http_request, $test_name )
Runs request, then tests if the response code is 200. Equivalent to the following code:
request
$t->request( GET '/path' )->code_is(200); $t->request_ok( GET '/path' ); # Same as the above
code_is( $code, $test_name ), code_isnt( $code, $test_name )
code_is( $code, $test_name )
code_isnt( $code, $test_name )
Tests if the last response returned a status code equal or not equal to $code. An optional name of the test can be added as a second parameter.
$code
$t->request( GET '/path' )->code_is(200); $t->request( GET '/path' )->code_isnt(500);
Same as "request", but also runs code_is(200).
code_is(200)
$t->request_ok( GET '/home' ); # Tests for code = 200
content_is( $value, $test_name ), content_isnt( $value, $test_name )
content_is( $value, $test_name )
content_isnt( $value, $test_name )
Tests if the last response returned content equal or not equal to $value. An optional name of the test can be added as a second parameter.
$value
$t->request( GET '/path' )->content_is("Ok."); $t->request( GET '/path' )->content_isnt("Fail.");
content_like( $regexp, $test_name ), content_unlike( $regexp, $test_name )
content_like( $regexp, $test_name )
content_unlike( $regexp, $test_name )
Tests if the last response returned content that matches or doesn't match $regexp. An optional name of the test can be added as a second parameter.
$regexp
$t->request( GET '/path' )->content_like(qr{Amsterdam}); $t->request( GET '/path' )->content_unlike(qr{Rotterdam});
content_type_is( $value, $test_name ), content_type_isnt( $value, $test_name )
content_type_is( $value, $test_name )
content_type_isnt( $value, $test_name )
Tests if the last response's content-type header is equal or not equal to $value. An optional name of the test can be added as a second parameter.
$t->request( GET '/path' )->content_type_is("text/plain"); $t->request( GET '/path' )->content_type_isnt("text/html");
header_is( $header, $value, $test_name ), header_isnt( $header, $value, $test_name )
header_is( $header, $value, $test_name )
header_isnt( $header, $value, $test_name )
Tests if the last response returned a header $header that is equal or not equal to $value. An optional name of the test can be added as a second parameter.
$header
$t->request( GET '/path' )->header_is( "Pragma", "no-cache" ); $t->request( GET '/path' )->header_isnt( "X-Check", "yes" );
header_like( $header, $regexp, $test_name ), header_unlike( $header, $regexp, $test_name )
header_like( $header, $regexp, $test_name )
header_unlike( $header, $regexp, $test_name )
Tests if the last response returned a header $header that matches or doesn't match $regexp. An optional name of the test can be added as a second parameter.
$t->request( GET '/path' )->header_like( "Content-Type", qr/json/ ); $t->request( GET '/path' )->header_unlike( "Content-Type", qr/image/ );
json_cmp( $expected, $test_name )
This tests for two things: If the returned content-type is application-json, and if the returned JSON structure matches the structure specified in $expected. To compare the two structures this method uses cmp_deeply from Test::Deep, so you can use all the goodies from the SPECIAL-COMPARISONS-PROVIDED section of the Test::Deep module.
content-type
application-json
$expected
cmp_deeply
SPECIAL-COMPARISONS-PROVIDED
$t->request( GET '/api' )->json_cmp( { auth => 1, timestamp => ignore(), info => subhashof( { name => 'Rick James' } ) } );
An optional name of the test can be added as a second parameter.
note( $note )
Print a note, using the Test::More note function.
note
$t->request( GET '/path' ) ->note("Checking headers now") ->header_is( "Content-Type", qr/json/ );
Prints all headers for debugging purposes.
$t->request( GET '/path' ) ->header_is( "Content-Type", qr/json/ ) ->diag_headers();
Prints the entire content for debugging purposes.
$t->request( GET '/path' ) ->content_is("Well") ->diag_content();
To install Kelp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kelp
CPAN shell
perl -MCPAN -e shell install Kelp
For more information on module installation, please visit the detailed CPAN module installation guide.