Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active November 30, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/129b51397d275d3e18ba455592b73347 to your computer and use it in GitHub Desktop.
Save jberger/129b51397d275d3e18ba455592b73347 to your computer and use it in GitHub Desktop.
use Dancer2;
set template => 'simple';
set views => '.';
any '/text' => sub {
my $name = param('name') // 'world';
send_as plain => "hello $name";
};
any '/data' => sub {
my $name = param('name') // 'world';
send_as JSON => { hello => $name };
};
any '/html' => sub {
my $name = param('name') // 'world';
template 'hello' => { name => $name };
};
dance;
requires 'Dancer2';
test_requires 'Mojolicious'; # Test::Mojo
test_requires 'Test::Mojo::Role::PSGI';
<dl id="data">
<dt id="hello">hello</dt>
<dd><% name %></dd>
</dl>
use Mojo::Base -strict;
# or:
# use strict;
# use warnings;
# use utf8;
# use IO::Handle;
# use feature ':5.10';
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->with_roles('+PSGI')->new('app.psgi');
$t->get_ok('/text')
->content_type_like(qr[text/plain])
->content_is('hello world');
$t->get_ok('/text', form => { name => 'santa' })
->content_type_like(qr[text/plain])
->content_is('hello santa');
$t->get_ok('/data')
->status_is(200)
->content_type_like(qr[application/json])
->json_is('/hello' => 'world');
$t->post_ok('/data' => form => { name => 'rudolph' })
->status_is(200)
->content_type_like(qr[application/json])
->json_is('/hello' => 'rudolph');
$t->get_ok('/html')
->status_is(200)
->content_type_like(qr[text/html])
->text_is('dl#data dt#hello + dd', 'world');
$t->post_ok('/html' => form => { name => 'grinch' })
->status_is(200)
->content_type_like(qr[text/html])
->text_is('dl#data dt#hello + dd', 'grinch');
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment