Last active
November 30, 2018 16:31
-
-
Save jberger/129b51397d275d3e18ba455592b73347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requires 'Dancer2'; | |
test_requires 'Mojolicious'; # Test::Mojo | |
test_requires 'Test::Mojo::Role::PSGI'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dl id="data"> | |
<dt id="hello">hello</dt> | |
<dd><% name %></dd> | |
</dl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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