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; | |
use Test::Mojo; | |
use Test::More; | |
use Mojolicious::Lite; | |
app->defaults( handler => 'my_handler' ); | |
get '/example' => sub{ die }; | |
hook before_render => sub { |
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 Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
hook before_render => sub { | |
my ($c, $args) = @_; | |
@$args{qw(text format)} = ($args->{exception}, 'txt') | |
if ($args->{template} // '') eq 'exception' && $c->accepts('txt'); | |
}; |
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
DESCRIPTION OF THE PROBLEM | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
get '/test' => { format => 'xxx' }; | |
my $t = Test::Mojo->new; |
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
Initial problem: | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
get '/test' => { format => 'xxx' }; | |
my $t = Test::Mojo->new; |
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
Mojolicious content negotiation gotchas | |
Let's begin with parts which participate in content negotiation. | |
First of all there are client requirements and server capabilities. | |
This is obvious that we should prefer client requirements over server capabilies by default. And, if server (controller's action) wants, it may force {format}. | |
So picture is next by precedence: | |
1. Controller's action format preferences |
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 Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
app->defaults( format => 'zzz', template => 'test' ); | |
get '/app'; | |
get '/route' => { format => 'xxx' }; |
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
>Rails picks up the expected format from the query parameter format, or if not there from the URL path suffix, or it not there from the Accept header | |
Blog post: https://dzone.com/articles/rest-with-rails-part-2-serving | |
>format query parameter is useful for rendering JSON output from a web browser without | |
special tools to modify the Accept header | |
IBM knowledge base: https://www.ibm.com/support/knowledgecenter/en/SS4GCC_6.1.1/com.ibm.urelease.doc/topics/rest_api_ref_conventions.html | |
>However, if a request uses multiple methods simultaneously, |
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
... | |
sub startup { | |
... | |
$app->hook( before_render => sub{ | |
my( $c, $args ) = @_; | |
my $theme = $c->stash->{ theme } // ''; | |
$args->{template} = "$theme/$args->{template}"; | |
# But here we get two problems: |
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
diff --git a/lib/Mojolicious/Lite.pm b/lib/Mojolicious/Lite.pm | |
index cf3cd04..21277fb 100644 | |
--- a/lib/Mojolicious/Lite.pm | |
+++ b/lib/Mojolicious/Lite.pm | |
@@ -46,9 +46,6 @@ sub import { | |
plugin => sub { $app->plugin(@_) }, | |
under => sub { $routes = $root->under(@_) }; | |
- # Make sure there's a default application for testing | |
- Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app; |
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 Mojolicious::Lite; | |
package Lite; | |
use Mojolicious::Lite; | |
get '/bye' => sub{ shift->render( text => 'OK' ) }; | |
package main; |
NewerOlder