The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer2::Test - Useful routines for testing Dancer2 apps

VERSION

version 0.03

DESCRIPTION

provides useful routines to test Dancer2 apps.

$test_name is always optional.

FUNCTIONS

dancer_response ($method, $path, $params, $arg_env);

Returns a Dancer2::Response object for the given request.

Only $method and $path are required.

$params is a hashref with 'body' as a string; 'headers' can be an arrayref or a HTTP::Headers object, 'files' can be arrayref of hashref, containing some files to upload:

        dancer_response($method, $path, 
                { params => $params, 
                        body => $body, 
                        headers => $headers, 
                        files => [{filename => '/path/to/file', name => 'my_file'}] 
                }
        );

A good reason to use this function is for testing POST requests. Since POST requests may not be idempotent, it is necessary to capture the content and status in one shot. Calling the response_status_is and response_content_is functions in succession would make two requests, each of which could alter the state of the application and cause Schrodinger's cat to die.

    my $response = dancer_response POST => '/widgets';
    is $response->{status}, 202, "response for POST /widgets is 202";
    is $response->{content}, "Widget #1 has been scheduled for creation",
        "response content looks good for first POST /widgets";

    $response = dancer_response POST => '/widgets';
    is $response->{status}, 202, "response for POST /widgets is 202";
    is $response->{content}, "Widget #2 has been scheduled for creation",
        "response content looks good for second POST /widgets";

It's possible to test file uploads:

    post '/upload' => sub { return upload('image')->content };

    $response = dancer_response(POST => '/upload', {files => [{name => 'image', filename => '/path/to/image.jpg'}]});

In addition, you can supply the file contents as the data key:

    my $data  = 'A test string that will pretend to be file contents.';
    $response = dancer_response(POST => '/upload', {
        files => [{name => 'test', filename => "filename.ext", data => $data}]
    });

response_status_is ($request, $expected, $test_name);

Asserts that Dancer2's response for the given request has a status equal to the one given.

    response_status_is [GET => '/'], 200, "response for GET / is 200";

route_exists([$method, $path], $test_name)

Asserts that the given request matches a route handler in Dancer2's registry.

    route_exists [GET => '/'], "GET / is handled";

route_doesnt_exist([$method, $path], $test_name)

Asserts that the given request does not match any route handler in Dancer2's registry.

    route_doesnt_exist [GET => '/bogus_path'], "GET /bogus_path is not handled";

response_status_isnt([$method, $path], $status, $test_name)

Asserts that the status of Dancer2's response is not equal to the one given.

    response_status_isnt [GET => '/'], 404, "response for GET / is not a 404";

response_content_is([$method, $path], $expected, $test_name)

Asserts that the response content is equal to the $expected string.

 response_content_is [GET => '/'], "Hello, World", 
        "got expected response content for GET /";

response_content_isnt([$method, $path], $not_expected, $test_name)

Asserts that the response content is not equal to the $not_expected string.

    response_content_isnt [GET => '/'], "Hello, World", 
        "got expected response content for GET /";

response_content_like([$method, $path], $regexp, $test_name)

Asserts that the response content for the given request matches the regexp given.

    response_content_like [GET => '/'], qr/Hello, World/, 
        "response content looks good for GET /";

response_content_unlike([$method, $path], $regexp, $test_name)

Asserts that the response content for the given request does not match the regexp given.

    response_content_unlike [GET => '/'], qr/Page not found/, 
        "response content looks good for GET /";

response_content_is_deeply([$method, $path], $expected_struct, $test_name)

Similar to response_content_is(), except that if response content and $expected_struct are references, it does a deep comparison walking each data structure to see if they are equivalent.

If the two structures are different, it will display the place where they start differing.

    response_content_is_deeply [GET => '/complex_struct'], 
        { foo => 42, bar => 24}, 
        "got expected response structure for GET /complex_struct";

response_is_file ($request, $test_name);

response_headers_are_deeply([$method, $path], $expected, $test_name)

Asserts that the response headers data structure equals the one given.

    response_headers_are_deeply [GET => '/'], [ 'X-Powered-By' => 'Dancer2 1.150' ];

response_headers_include([$method, $path], $expected, $test_name)

Asserts that the response headers data structure includes some of the defined ones.

    response_headers_include [GET => '/'], [ 'Content-Type' => 'text/plain' ];

import

When Dancer2::Test is imported, it should be passed all the applications that are supposed to be tested.

If none passed, then the caller is supposed to be the sole application to test.

    # t/sometest.t

    use t::lib::Foo;
    use t::lib::Bar;

    use Dancer2::Test apps => ['t::lib::Foo', 't::lib::Bar'];

AUTHOR

Dancer Core Developers

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.