View format.t
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 { |
View t-mojolicoius-exception_lite_app.t
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'); | |
}; |
View gist:0eec79515ef1c2c5c200ca902c9915ec
DESCRIPTION OF THE PROBLEM | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
get '/test' => { format => 'xxx' }; | |
my $t = Test::Mojo->new; |
View gist:1d3e4a5fa342c906f2ea5fde3bb165b9
Initial problem: | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
get '/test' => { format => 'xxx' }; | |
my $t = Test::Mojo->new; |
View gist:c7b9a2487b98dee19a329102844a1159
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 |
View content_negotiation.t
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
app->defaults( format => 'zzz', template => 'test' ); | |
get '/app'; | |
get '/route' => { format => 'xxx' }; |
View gist:8f6a32b9cde4404414aadffa8fb2515b
>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, |
View App.pm
... | |
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: |
View patch.diff
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; |
View lite_app.t
use Mojolicious::Lite; | |
package Lite; | |
use Mojolicious::Lite; | |
get '/bye' => sub{ shift->render( text => 'OK' ) }; | |
package main; |
NewerOlder