Skip to content

Instantly share code, notes, and snippets.

@KES777
Created March 23, 2018 08:00
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 KES777/c56e5043ebde250754df34a473941864 to your computer and use it in GitHub Desktop.
Save KES777/c56e5043ebde250754df34a473941864 to your computer and use it in GitHub Desktop.
Content negotiation tests
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;
app->defaults( format => 'zzz', template => 'test' );
get '/app';
get '/route' => { format => 'xxx' };
my $t = Test::Mojo->new;
$t->get_ok('/app')->content_is( "zzz\n" );
$t->get_ok('/app' => {Accept => 'application/json'} )->content_is( "json\n" );
$t->get_ok('/app.json')->content_is( "json\n" );
$t->get_ok('/app?format=json')->content_is( "json\n" );
$t->get_ok('/route')->content_is( "xxx\n" );
$t->get_ok('/route' => {Accept => 'application/json'} )->content_is( "json\n" );
$t->get_ok('/route.json')->content_is( "json\n" );
$t->get_ok('/route?format=json')->content_is( "json\n" );
done_testing();
__DATA__
@@ test.zzz.ep
zzz
@@ test.xxx.ep
xxx
@@ test.json.ep
json
@@ test.xml.ep
xml
__END__
ok 1 - GET /app
ok 2 - exact match for content
ok 3 - GET /app
not ok 4 - exact match for content
# Failed test 'exact match for content'
# at t/mojolicious/basic.t2 line 14.
# got: 'zzz
# '
# expected: 'json
# '
ok 5 - GET /app.json
ok 6 - exact match for content
ok 7 - GET /app?format=json
not ok 8 - exact match for content
# Failed test 'exact match for content'
# at t/mojolicious/basic.t2 line 16.
# got: 'zzz
# '
# expected: 'json
# '
ok 9 - GET /route
ok 10 - exact match for content
ok 11 - GET /route
not ok 12 - exact match for content
# Failed test 'exact match for content'
# at t/mojolicious/basic.t2 line 20.
# got: 'xxx
# '
# expected: 'json
# '
ok 13 - GET /route.json
ok 14 - exact match for content
ok 15 - GET /route?format=json
not ok 16 - exact match for content
# Failed test 'exact match for content'
# at t/mojolicious/basic.t2 line 22.
# got: 'xxx
# '
# expected: 'json
# '
1..16
# Looks like you failed 4 tests of 16.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment