### ----------------------------------------------------
### -- Humming-Bird
### -- Licenses: MIT
### -- Authors: Rawley Fowler
### -- File: t/04-middleware.rakutest
### ----------------------------------------------------
use v6;
use lib 'lib';

use Test;

use Humming-Bird::Core;
use Humming-Bird::Glue;
use lib 't/lib';
use Humming-Bird::Test;

plan 6;

use-ok 'Humming-Bird::Middleware';

use Humming-Bird::Middleware;

get('/', -> $request, $response {
  $response.html('Hello World!');
}, [ &middleware-logger ]);

is routes{'/'}{GET}.middlewares[0].raku, &middleware-logger.raku, 'Is middleware properly assigned?';
is routes{'/'}{GET}.middlewares[0].elems, 1, 'Is proper number of middleware inside of route?';

group((
  &get.assuming('/hello', -> $request, $response {
    $response.html('Hello!');
  }),
  &get.assuming('/hello/world', -> $request, $response {
    $response.html('Hello World!');
  })
), [ &middleware-logger ]);

is routes{'/'}{'hello'}{GET}.middlewares[0].raku, &middleware-logger.raku, 'Is middleware of group properly assigned?';
is routes{'/'}{'hello'}{'world'}{GET}.middlewares[0].raku, &middleware-logger.raku, 'Is middleware of group properly assigned?';

middleware(sub ($request, $response, &next) { $response.html('Foo Bar') });
get('/foobar', -> $request, $response { $response.write('YOU CANT SEE ME') });
my @context = get-context(path => '/foobar', method => GET, version => 'HTTP/1.1');
is routes{'/'}{'foobar'}{GET}(|@context).body, Buf.new('Foo Bar'.encode), 'Is global middleware OK?';

done-testing;


