Acme-CPANModulesBundle-Import-MojoliciousAdvent-2018

 view release on metacpan or  search on metacpan

devdata/https_mojolicious.io_blog_2018_12_04_testing-hooks-and-helpers_  view on Meta::CPAN

<pre><code>#!/usr/bin/env perl
use Mojolicious::Lite;
# Allow access via tokens
plugin Config =&gt; {
    default =&gt; {
        tokens =&gt; { }, # token =&gt; username
    },
};
helper current_user =&gt; sub( $c ) {
    my $auth = $c-&gt;req-&gt;headers-&gt;authorization;
    return undef unless $auth;
    my ( $token ) = $auth =~ /^Token\ (\S+)$/;
    return undef unless $token;
    return $c-&gt;app-&gt;config-&gt;{tokens}{ $token };
};
</code></pre>

<p>Then, rather than generating web requests to check all our
authentication edge cases, I can build a controller and set the right
headers to run my tests (using <a href="https://mojolicious.org/perldoc/Test/Mojo#new">Test::Mojo configuration
overrides</a> to add a test
token):</p>

devdata/https_mojolicious.io_blog_2018_12_04_testing-hooks-and-helpers_  view on Meta::CPAN

use Test::More;
use Test::Mojo;
use Mojo::File qw( path );
my $token = &#39;mytoken&#39;;
my $t = Test::Mojo-&gt;new( path(&#39;myapp.pl&#39;), {
    # Add a token as a configuration override
    tokens =&gt; { $token =&gt; &#39;preaction&#39; },
} );

my $c = $t-&gt;app-&gt;build_controller;
is $c-&gt;current_user, undef, &#39;current_user not set&#39;;

$c-&gt;req-&gt;headers-&gt;authorization( &#39;NOTATOKEN&#39; );
is $c-&gt;current_user, undef, &#39;current_user without &quot;Token&quot;&#39;;

$c-&gt;req-&gt;headers-&gt;authorization( &#39;Token NOTFOUND&#39; );
is $c-&gt;current_user, undef, &#39;current_user token incorrect&#39;;

$c-&gt;req-&gt;headers-&gt;authorization( &quot;Token $token&quot; );
is $c-&gt;current_user, &#39;preaction&#39;, &#39;current_user correct&#39;;
</code></pre>

<p>Of course, we&#39;ll still need to test whether the routes we want to
protect with tokens are protected, but this shows that our
authentication helper works so if there are problems with our routes,
it&#39;s probably not here.</p>

devdata/https_mojolicious.io_blog_2018_12_11_who-watches-the-minions_  view on Meta::CPAN

  &quot;children&quot; =&gt; [],
  &quot;created&quot; =&gt; &quot;2018-11-23T19:15:47Z&quot;,
  &quot;delayed&quot; =&gt; &quot;2018-11-23T19:15:47Z&quot;,
  &quot;finished&quot; =&gt; &quot;2018-11-23T19:15:48Z&quot;,
  &quot;id&quot; =&gt; 1,
  &quot;notes&quot; =&gt; {},
  &quot;parents&quot; =&gt; [],
  &quot;priority&quot; =&gt; 0,
  &quot;queue&quot; =&gt; &quot;default&quot;,
  &quot;result&quot; =&gt; &quot;0.0716841220855713&quot;,
  &quot;retried&quot; =&gt; undef,
  &quot;retries&quot; =&gt; 0,
  &quot;started&quot; =&gt; &quot;2018-11-23T19:15:47Z&quot;,
  &quot;state&quot; =&gt; &quot;finished&quot;,
  &quot;task&quot; =&gt; &quot;check_url&quot;,
  &quot;worker&quot; =&gt; 1
}
</code></pre>

<p>But, it&#39;d be a lot nicer if I didn&#39;t have to open a terminal, open an
SSH connection, and run a command to look at the status of my Minion.</p>

devdata/https_mojolicious.io_blog_2018_12_16_browser-diet_  view on Meta::CPAN

JavaScript is downloading.  These two
<a href="https://flaviocopes.com/javascript-async-defer/">wise</a>
<a href="https://bitsofco.de/async-vs-defer/">owls</a>
can help decide which one to use.</p>

<p>Tell your script to load after the main page
with this
<a href="https://mojolicious.org/perldoc/Mojolicious/Plugin/TagHelpers#javascript">tag helper</a>
in your template</p>

<pre><code>%= javascript &#39;/js/lib/jquery.min.js&#39;, defer =&gt; undef
</code></pre>

<p>which produces</p>

<pre><code>&lt;script defer src=&quot;/path/to/script.js&quot;&gt;&lt;/script&gt;
</code></pre>

<p>If you want it in the <code>&lt;head&gt;</code>, you&#39;ll have to put it in your layout
otherwise the template will do.</p>



( run in 0.984 second using v1.01-cache-2.11-cpan-d80b1682f3f )