=encoding utf8 =head1 NAME Mojolicious::Guides::Testing - Web Application Testing Made Easy =head1 OVERVIEW This document is an introduction to testing web applications with L. L can be thought of as a module that provides all of the tools and testing assertions needed to test web applications in a Perl-ish way. While L can be used to test any web application, it has shortcuts designed to make testing L web applications easy and pain-free. Please refer to the L documentation for a complete reference to many of the ideas and syntax introduced in this document. A test file for a simple web application might look like: use Mojo::Base -strict; use Test::Mojo; use Test::More; # Start a Mojolicious app named "Celestial" my $t = Test::Mojo->new('Celestial'); # Post a JSON document $t->post_ok('/notifications' => json => {event => 'full moon'}) ->status_is(201) ->json_is('/message' => 'notification created'); # Perform GET requests and look at the responses $t->get_ok('/sunrise') ->status_is(200) ->content_like(qr/ am$/); $t->get_ok('/sunset') ->status_is(200) ->content_like(qr/ pm$/); # Post a URL-encoded form $t->post_ok('/insurance' => form => {name => 'Jimmy', amount => '€3.000.000'}) ->status_is(200); # Use Test::More's like() to check the response like $t->tx->res->dom->at('div#thanks')->text, qr/thank you/, 'thanks'; done_testing(); In the rest of this document we'll explore these concepts and others related to L. =head1 CONCEPTS Essentials every L developer should know. =head2 L at a glance The L module bundled with Perl includes several primitive test assertions, such as C, C, C, C, C, C, etc. An assertion "passes" if its expression returns a true value. The assertion method prints "ok" or "not ok" if an assertion passes or fails (respectively). L supplies additional test assertions organized around the web application request/response transaction (transport, response headers, response bodies, etc.), and WebSocket communications. One interesting thing of note: the return value of L object assertions is always the test object itself, allowing us to "chain" test assertion methods. So rather than grouping related test statements like this: $t->get_ok('/frogs'); $t->status_is(200); $t->content_like(qr/bullfrog/); $t->content_like(qr/hypnotoad/); Method chaining allows us to connect test assertions that belong together: $t->get_ok('/frogs') ->status_is(200) ->content_like(qr/bullfrog/) ->content_like(qr/hypnotoad/); This makes for a much more I and I testing experience: concise because we are not repeating the invocant for each test, and coherent because assertions that belong to the same request are syntactically bound in the same method chain. Occasionally it makes sense to break up a test to perform more complex assertions on a response. L exposes the entire transaction object so you can get all the data you need from a response: $t->put_ok('/bees' => json => {type => 'worker', name => 'Karl'}) ->status_is(202) ->json_has('/id'); # Pull out the id from the response my $newbee = $t->tx->res->json('/id'); # Make a new request with data from the previous response $t->get_ok("/bees/$newbee") ->status_is(200) ->json_is('/name' => 'Karl'); The L object is I. As long as we haven't started a new transaction by invoking one of the C<*_ok> methods, the request and response objects from the previous transaction are available in the L object: # First transaction $t->get_ok('/frogs?q=bullfrog' => {'Content-Type' => 'application/json'}) ->status_is(200) ->json_like('/0/species' => qr/catesbeianus/i); # Still first transaction $t->content_type_is('application/json'); # Second transaction $t->get_ok('/frogs?q=banjo' => {'Content-Type' => 'text/html'}) ->status_is(200) ->content_like(qr/interioris/i); # Still second transaction $t->content_type_is('text/html'); This statefulness also enables L to handle sessions, follow redirects, and inspect past responses during a redirect. =head2 The L object The L object manages the Mojolicious application lifecycle (if a Mojolicious application class is supplied) as well as exposes the built-in L object. To create a bare L object: my $t = Test::Mojo->new; This object initializes a L object and provides a variety of test assertion methods for accessing a web application. For example, with this object, we could test any running web application: $t->get_ok('https://www.google.com/') ->status_is(200) ->content_like(qr/search/i); You can access the user agent directly if you want to make web requests without triggering test assertions: my $tx = $t->ua->post('https://duckduckgo.com/html' => form => {q => 'hypnotoad'}); $tx->result->dom->find('a.result__a')->each(sub { say $_->text }); See L for the complete API and return values. =head2 Testing Mojolicious applications If you pass the name of a L application class (e.g., 'MyApp') to the L constructor, L will instantiate the class and start it, and cause it to listen on a random (unused) port number. Testing a Mojolicious application using L will never conflict with running applications, including the application you're testing. The L object in L will know where the application is running and make requests to it. Once the tests have completed, the L application will be torn down. # Listens on localhost:32114 (some unused TCP port) my $t = Test::Mojo->new('Frogs'); To test a L application, pass the file path to the application script to the constructor. # Load application script relative to the "t" directory use Mojo::File qw(curfile); my $t = Test::Mojo->new(curfile->dirname->sibling('myapp.pl')); The object initializes a L object, loads the Mojolicious application, binds and listens on a free TCP port (e.g., 32114), and starts the application event loop. When the L object (C<$t>) goes out of scope, the application is stopped. Relative URLs in the test object method assertions (C, C, etc.) will be sent to the Mojolicious application started by L: # Rewritten to "http://localhost:32114/frogs" $t->get_ok('/frogs'); L has a lot of handy shortcuts built into it to make testing L or L applications enjoyable. =head3 An example Let's spin up a Mojolicious application using C. The C utility will create a working application and a C directory with a working test file: $ mojo generate app MyApp [mkdir] /my_app/script [write] /my_app/script/my_app [chmod] /my_app/script/my_app 744 ... [mkdir] /my_app/t [write] /my_app/t/basic.t ... Let's run the tests (we'll create the C directory to quiet the application output): $ cd my_app $ mkdir log $ prove -lv t t/basic.t .. ok 1 - GET / ok 2 - 200 OK ok 3 - content is similar 1..3 ok All tests successful. Files=1, Tests=3, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.33 cusr 0.07 csys = 0.44 CPU) Result: PASS The boilerplate test file looks like this: use Mojo::Base -strict; use Test::More; use Test::Mojo; my $t = Test::Mojo->new('MyApp'); $t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); done_testing(); Here we can see our application class name C is passed to the L constructor. Under the hood, L creates a new L instance, loads C (which we just created), and runs the application. We write our tests with relative URLs because L takes care of getting the request to the running test application (since its port may change between runs). =head3 Testing with configuration data We can alter the behavior of our application using environment variables (such as C) and through configuration values. One nice feature of L is its ability to pass configuration values directly from its constructor. Let's modify our application and add a "feature flag" to enable a new feature when the C configuration value is set: # Load configuration from hash returned by "my_app.conf" my $config = $self->plugin('Config'); # Normal route to controller $r->get('/')->to('example#welcome'); # NEW: this route only exists if "enable_weather" is set in the configuration if ($config->{enable_weather}) { $r->get('/weather' => sub ($c) { $c->render(text => "It's hot! 🔥"); }); } To test this new feature, we don't even need to create a configuration file—we can simply pass the configuration data to the application directly via L's constructor: my $t = Test::Mojo->new(MyApp => {enable_weather => 1}); $t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); $t->get_ok('/weather')->status_is(200)->content_like(qr/🔥/); When we run these tests, L will pass this configuration data to the application, which will cause it to create a special C route that we can access in our tests. Unless C is set in a configuration file, this route will not exist when the application runs. Feature flags like this allow us to do soft rollouts of features, targeting a small audience for a period of time. Once the feature has been proven, we can refactor the conditional and make it a full release. This example shows how easy it is to start testing a Mojolicious application and how to set specific application configuration directives from a test file. =head3 Testing application helpers Let's say we register a helper in our application to generate an HTTP Basic Authorization header: use Mojo::Util qw(b64_encode); app->helper(basic_auth => sub ($c, @values) { return {Authorization => 'Basic ' . b64_encode join(':' => @values), ''}; }); How do we test application helpers like this? L has access to the application object, which allows us to invoke helpers from our test file: my $t = Test::Mojo->new('MyApp'); is_deeply $t->app->basic_auth(bif => "Bif's Passwerdd"), {Authorization => 'Basic YmlmOkJpZidzIFBhc3N3ZXJkZA=='}, 'correct header value'; Any aspect of the application (helpers, plugins, routes, etc.) can be introspected from L through the application object. This enables us to get deep test coverage of L-based applications. =head1 ASSERTIONS This section describes the basic test assertions supplied by L. There are four broad categories of assertions for HTTP requests: =over 2 =item * HTTP requests =item * HTTP response status =item * HTTP response headers =item * HTTP response content/body =back WebSocket test assertions are covered in L. =head2 HTTP request assertions L has a L object that allows it to make HTTP requests and check for HTTP transport errors. HTTP request assertions include C, C, etc. These assertions do not test whether the request was handled I, only that the web application handled the request in an HTTP compliant way. You may also make HTTP requests using custom verbs (beyond C, C, C, etc.) by building your own transaction object. See L below. =head3 Using HTTP request assertions To post a URL-encoded form to the C endpoint of an application, we simply use the C
content type shortcut: $t->post_ok('/calls' => form => {to => '+43.55.555.5555'}); Which will create the following HTTP request: POST /calls HTTP/1.1 Content-Length: 20 Content-Type: application/x-www-form-urlencoded to=%2B43.55.555.5555 The C<*_ok> HTTP request assertion methods accept the same arguments as their corresponding L methods (except for the callback argument). This allows us to set headers and build query strings for authentic test situations: $t->get_ok('/internal/personnel' => {Authorization => 'Token secret-password'} => form => {q => 'Professor Plum'}); which generates the following request: GET /internal/personnel?q=Professor+Plum HTTP/1.1 Content-Length: 0 Authorization: Token secret-password The C content generator (see L) will generate a query string for C requests and C or C for POST requests. While these C<*_ok> assertions make the HTTP I we expect, they tell us little about I the application handled the request. The application we're testing might have returned any content-type, body, or HTTP status code (200, 302, 400, 404, 500, etc.) and we wouldn't know it. L provides assertions to test almost every aspect of the HTTP response, including the HTTP response status code, the value of the C header, and other arbitrary HTTP header information. =head2 HTTP response status code While not technically an HTTP header, the status line is the first line in an HTTP response and is followed by the response headers. Testing the response status code is common in REST-based and other web applications that use the HTTP status codes to broadly indicate the type of response the server is returning. Testing the status code is as simple as adding the C assertion: $t->post_ok('/doorbell' => form => {action => 'ring once'}) ->status_is(200); Along with C, this will cover most needs. For more elaborate status code testing, you can access the response internals directly: $t->post_ok('/doorbell' => form => {action => 'ring once'}); is $t->tx->res->message, 'Moved Permanently', 'try next door'; =head2 HTTP response headers L allows us to inspect and make assertions about HTTP response headers. The C header is commonly tested and has its own assertion: $t->get_ok('/map-of-the-world.pdf') ->content_type_is('application/pdf'); This is equivalent to the more verbose: $t->get_ok('/map-of-the-world.pdf') ->header_is('Content-Type' => 'application/pdf'); We can test for multiple headers in a single response using method chains: $t->get_ok('/map-of-the-world.pdf') ->content_type_is('application/pdf') ->header_isnt('Compression' => 'gzip') ->header_unlike('Server' => qr/IIS/i); =head2 HTTP response content assertions L also exposes a rich set of assertions for testing the body of a response, whether that body be HTML, plain-text, or JSON. The C methods look at the body of the response as plain text (as defined by the response's character set): $t->get_ok('/scary-things/spiders.json') ->content_is('{"arachnid":"brown recluse"}'); Although this is a JSON document, C treats it as if it were a text document. This may be useful for situations where we're looking for a particular string and not concerned with the structure of the document. For example, we can do the same thing with an HTML document: $t->get_ok('/scary-things/spiders.html') ->content_like(qr{All The Spiders}); But because L has access to everything that L does, we can introspect JSON documents as well as DOM-based documents (HTML, XML) with assertions that allow us to check for the existence of elements as well as inspect the content of text nodes. =head3 JSON response assertions L's L has access to a JSON parser, which allows us to test to see if a JSON response contains a value at a location in the document using JSON pointer syntax: $t->get_ok('/animals/friendly.json') ->json_has('/beings/jeremiah/age'); This assertion tells us that the C document contains a value at the C JSON pointer location. We can also inspect the value at JSON pointer locations: $t->get_ok('/animals/friendly.json') ->json_has('/beings/jeremiah/age') ->json_is('/beings/jeremiah/age' => 42) ->json_like('/beings/jeremiah/species' => qr/bullfrog/i); JSON pointer syntax makes testing JSON responses simple and readable. =head3 DOM response assertions We can also inspect HTML and XML responses using the L parser in the user agent. Here are a few examples from the L documentation: $t->text_is('div.foo[x=y]' => 'Hello!'); $t->text_is('html head title' => 'Hello!', 'right title'); The L parser uses the CSS selector syntax described in L, allowing us to test for values in HTML and XML documents without resorting to typically verbose and inflexible DOM traversal methods. =head1 ADVANCED TOPICS This section describes some complex (but common) testing situations that L excels in making simple. =head2 Redirects The L object in L can handle HTTP redirections internally to whatever level you need. Let's say we have a web service that redirects C to C, C redirects to C, C redirects to C, and C redirects to C: GET /1 returns: 302 Found Location: /2 and: GET /2 returns: 302 Found Location: /3 and so forth, up to C: GET /5 which returns the data we wanted: 200 OK {"message":"this is five"} We can tell the user agent in L how to deal with redirects. Each test is making a request to C, but we vary the number of redirects the user agent should follow with each test: my $t = Test::Mojo->new; $t->get_ok('/1') ->header_is(location => '/2'); $t->ua->max_redirects(1); $t->get_ok('/1') ->header_is(location => '/3'); $t->ua->max_redirects(2); $t->get_ok('/1') ->header_is(location => '/4'); # Look at the previous hop is $t->tx->previous->res->headers->location, '/3', 'previous redirect'; $t->ua->max_redirects(3); $t->get_ok('/1') ->header_is(location => '/5'); $t->ua->max_redirects(4); $t->get_ok('/1') ->json_is('/message' => 'this is five'); When we set C, it stays set for the life of the test object until we change it. L's handling of HTTP redirects eliminates the need for making many, sometimes an unknown number, of redirections to keep testing precise and easy to follow (ahem). =head2 Cookies and session management We can use L to test applications that keep session state in cookies. By default, the L object in L will manage session for us by saving and sending cookies automatically, just like common web browsers: use Mojo::Base -strict; use Test::More; use Test::Mojo; my $t = Test::Mojo->new('MyApp'); # No authorization cookie $t->get_ok('/') ->status_is(401) ->content_is('Please log in'); # Application sets an authorization cookie $t->post_ok('/login' => form => {password => 'let me in'}) ->status_is(200) ->content_is('You are logged in'); # Sends the cookie from the previous transaction $t->get_ok('/') ->status_is(200) ->content_like(qr/You logged in at \d+/); # Clear the cookies $t->reset_session; # No authorization cookie again $t->get_ok('/') ->status_is(401) ->content_is('Please log in'); We can also inspect cookies in responses for special values through the transaction's response (L) object: $t->get_ok('/'); like $t->tx->res->cookie('smarty'), qr/smarty=pants/, 'cookie found'; =head2 Custom transactions Let's say we have an application that responds to a new HTTP verb C and to use it we must also pass in a secret cookie value. This is not a problem. We can test the application by creating a L object, setting the cookie (see L), then passing the transaction object to C: # Use custom "RING" verb my $tx = $t->ua->build_tx(RING => '/doorbell'); # Set a special cookie $tx->req->cookies({name => 'Secret', value => "don't tell anybody"}); # Make the request $t->request_ok($tx) ->status_is(200) ->json_is('/status' => 'ding dong'); =head2 Testing WebSocket web services While the message flow on WebSocket connections can be rather dynamic, it more often than not is quite predictable, which allows this rather pleasant L WebSocket API to be used: use Mojo::Base -strict; use Test::More; use Test::Mojo; # Test echo web service my $t = Test::Mojo->new('EchoService'); $t->websocket_ok('/echo') ->send_ok('Hello Mojo!') ->message_ok ->message_is('echo: Hello Mojo!') ->finish_ok; # Test JSON web service $t->websocket_ok('/echo.json') ->send_ok({json => {test => [1, 2, 3]}}) ->message_ok ->json_message_is('/test' => [1, 2, 3]) ->finish_ok; done_testing(); Because of their inherent asynchronous nature, testing WebSocket communications can be tricky. The L WebSocket assertions serialize messages via event loop primitives. This enables us to treat WebSocket messages as if they were using the same request-response communication pattern we're accustomed to with HTTP. To illustrate, let's walk through these tests. In the first test, we use the C assertion to ensure that we can connect to our application's WebSocket route at C and that it's "speaking" WebSocket protocol to us. The next C assertion tests the connection again (in case it closed, for example) and attempts to send the message C. The next assertion, C, blocks (using the L singleton in the application) and waits for a response from the server. The response is then compared with C<'echo: Hello Mojo!'> in the C assertion, and finally we close and test our connection status again with C. The second test is like the first, but now we're sending and expecting JSON documents at C. In the C assertion we take advantage of L's JSON content generator (see L) to marshal hash and array references into JSON documents, and then send them as a WebSocket message. We wait (block) for a response from the server with C. Then because we're expecting a JSON document back, we can leverage C which parses the WebSocket response body and returns an object we can access through L syntax. Then we close (and test) our WebSocket connection. Testing WebSocket servers does not get any simpler than with L. =head2 Extending L If you see that you're writing a lot of test assertions that aren't chainable, you may benefit from writing your own test assertions. Let's say we want to test the C header after a redirect. We'll create a new class with L that implements a test assertion named C: package Test::Mojo::Role::Location; use Mojo::Base -role, -signatures; sub location_is ($self, $value, $desc = "Location: $value") { return $self->test('is', $self->tx->res->headers->location, $value, $desc); } 1; When we make new test assertions using roles, we want to use method signatures that match other C<*_is> methods in L, so here we accept the test object, the value to compare, and an optional description. We assign a default description value (C<$desc>), then we use L to compare the location header with the expected header value, and finally propagates the L object for method chaining. With this new package, we're ready to compose a new test object that uses the role: my $t = Test::Mojo->with_roles('+Location')->new('MyApp'); $t->post_ok('/redirect/mojo' => json => {message => 'Mojo, here I come!'}) ->status_is(302) ->location_is('http://mojolicious.org') ->or(sub { diag 'I miss tempire.' }); In this section we've covered how to add custom test assertions to L with roles and how to use those roles to simplify testing. =head1 MORE You can continue with L now or take a look at the L, which contains a lot more documentation and examples by many different authors. =head1 SUPPORT If you have any questions the documentation might not yet answer, don't hesitate to ask in the L, on L, or L. =cut