File: role.t

package info (click to toggle)
libweb-simple-perl 0.033-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: perl: 1,622; makefile: 7
file content (53 lines) | stat: -rw-r--r-- 931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use strict;
use warnings FATAL => 'all';

use Test::More 0.88;

{
  package BazRole;
  use Web::Simple::Role;

  around dispatch_request => sub {
    my ($orig, $self) = @_;
    return (
      $self->$orig,
      sub (GET + /baz) {
        [ 200,
          [ "Content-type" => "text/plain" ],
          [ 'baz' ],
        ]
      }
    );
  };
}
{
  package FooBar;
  use Web::Simple;
  with 'BazRole';
  sub dispatch_request {
    sub (GET + /foo) {
      [ 200,
        [ "Content-type" => "text/plain" ],
        [ 'foo' ],
      ]
    },
    sub (GET + /bar) {
      [ 200,
        [ "Content-type" => "text/plain" ],
        [ 'bar' ],
      ]
    },
  }
}

use HTTP::Request::Common qw(GET POST);

my $app = FooBar->new;
sub run_request { $app->run_test_request(@_); }

for my $word (qw/ foo bar baz /) {
  my $get = run_request(GET "http://localhost/${word}");
  is($get->content, $word, "Dispatch $word");
}

done_testing;