Plack::Test::Simple - Object-Oriented PSGI Application Testing
version 0.000008
use Test::More; use Plack::Test::Simple; # prepare test container my $t = Plack::Test::Simple->new('/path/to/app.psgi'); # global request configuration my $req = $t->request; $req->headers->authorization_basic('h@cker', 's3cret'); $req->headers->content_type('application/json'); # standard GET request test # automatic JSON serialization if content-body is a hash/array reference my $tx = $t->transaction('get', '/search?q=awesomeness', 'content body'); $tx->data_has('/results/4/title', 'test description'); done_testing;
Plack::Test::Simple is a collection of testing helpers for anyone developing Plack applications. This module is a wrapper around Plack::Test providing a unified interface to test PSGI applications using HTTP::Request and HTTP::Response objects. Typically a Plack web application's deployment stack includes various middlewares and utilities which are now even easier to test along-side the actual web application code.
The request attribute contains the HTTP::Request object which will be used to process the HTTP requests. This attribute is never reset.
The psgi attribute contains a coderef containing the PSGI compliant application code. If this value is a string containing the path to the psgi file, the application code will be coerced.
The transaction method returns a Plack::Test::Simple::Transaction object containing the HTTP request and response object that will be used to facilitate the HTTP transaction. The actually HTTP request is deferred until the response object is needed, this allows you to further modify the transactions HTTP request object before it is processed. This method optionally accepts an HTTP request method, a request path (or URI object), and content (any string) you wish to place in the body of the request. If the content body argument is a hash/array reference, we will attempt to encode and serialize it automatically as a JSON object. These parameters are used to further modify the transaction's request object. Please see Plack::Test::Simple::Transaction for more information on how to use the transaction object to further automate tests.
my $tx = $self->transaction('post', '/path/to/resource', 'content body');
The connect_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->connect_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->connect_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('connect', '/path/to/resource'); $tx->status_is(200, 'test description');
The delete_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->delete_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->delete_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('delete', '/path/to/resource'); $tx->status_is(200, 'test description');
The get_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->get_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->get_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('get', '/path/to/resource'); $tx->status_is(200, 'test description');
The head_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->head_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->head_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('head', '/path/to/resource'); $tx->status_is(200, 'test description');
The options_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->options_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->options_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('options', '/path/to/resource'); $tx->status_is(200, 'test description');
The patch_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->patch_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->patch_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('patch', '/path/to/resource'); $tx->status_is(200, 'test description');
The post_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->post_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->post_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('post', '/path/to/resource'); $tx->status_is(200, 'test description');
The put_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->put_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->put_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('put', '/path/to/resource'); $tx->status_is(200, 'test description');
The trace_returns_CODE method is a shorthand method for returning a transaction object much like the transaction method except the the actual HTTP request is made and the server's HTTP response code is tested to ensure it returns a CODE. The CODE in this method description is a variable which represents any valid HTTP response code, e.g. 200.
my $tx = $self->trace_returns_200( '/path/to/resource', 'content body' ); # with content body and status test description included my $tx = $self->trace_returns_200( '/path/to/resource', 'content body', 'test description' ); # shorthand for my $tx = $self->transaction('trace', '/path/to/resource'); $tx->status_is(200, 'test description');
Al Newkirk <anewkirk@ana.io>
This software is copyright (c) 2013 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Plack::Test::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Test::Simple
CPAN shell
perl -MCPAN -e shell install Plack::Test::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.