Skip to content

Instantly share code, notes, and snippets.

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 {
@KES777
KES777 / t-mojolicoius-exception_lite_app.t
Created March 30, 2018 06:54
Mojolicous expects wrong results
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');
};
@KES777
KES777 / gist:0eec79515ef1c2c5c200ca902c9915ec
Last active March 27, 2018 20:59
proper descrition of current behaviour
DESCRIPTION OF THE PROBLEM
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;
get '/test' => { format => 'xxx' };
my $t = Test::Mojo->new;
Initial problem:
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;
get '/test' => { format => 'xxx' };
my $t = Test::Mojo->new;
@KES777
KES777 / gist:c7b9a2487b98dee19a329102844a1159
Created March 23, 2018 15:29
content negotiation draft
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
@KES777
KES777 / content_negotiation.t
Created March 23, 2018 08:00
Content negotiation tests
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;
app->defaults( format => 'zzz', template => 'test' );
get '/app';
get '/route' => { format => 'xxx' };
@KES777
KES777 / gist:8f6a32b9cde4404414aadffa8fb2515b
Created March 23, 2018 07:36
format precedence while content negotiation
>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,
@KES777
KES777 / App.pm
Created March 3, 2018 16:48
Mojolicious Theming
...
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:
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;
@KES777
KES777 / lite_app.t
Last active November 3, 2017 14:21
not workint mojolicious lite app
use Mojolicious::Lite;
package Lite;
use Mojolicious::Lite;
get '/bye' => sub{ shift->render( text => 'OK' ) };
package main;