File Coverage

blib/lib/Catalyst/ActionRole/HTTPMethods.pm
Criterion Covered Total %
statement 9 9 100.0
branch 4 6 66.6
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 17 19 89.4


line stmt bran cond sub pod time code
1              
2             use Moose::Role;
3 86     86   63770  
  86         212  
  86         918  
4             requires 'match', 'match_captures', 'list_extra_info';
5              
6              
7 58 50   58 1 72 my ($self, $expected) = @_;
  58         1275  
8             return 1 unless scalar(my @allowed = $self->allowed_http_methods);
9             return scalar(grep { lc($_) eq lc($expected) } @allowed) ?
10 58     58   110 1 : 0;
11 58 50       159 }
12 58 100       106  
  69         441  
13             around ['match','match_captures'] => sub {
14             my ($orig, $self, $ctx, @args) = @_;
15             return 0 unless $self->$orig($ctx, @args);
16              
17             my $expected = $ctx->req->method;
18             return $self->_has_expected_http_method($expected);
19             };
20              
21             around 'list_extra_info' => sub {
22             my ($orig, $self, @args) = @_;
23             return {
24             %{ $self->$orig(@args) },
25             HTTP_METHODS => [sort $self->allowed_http_methods],
26             };
27             };
28              
29             1;
30              
31             =head1 NAME
32              
33             Catalyst::ActionRole::HTTPMethods - Match on HTTP Methods
34              
35             =head1 SYNOPSIS
36              
37             package MyApp::Web::Controller::MyController;
38              
39             use Moose;
40             use MooseX::MethodAttributes;
41              
42             extends 'Catalyst::Controller';
43              
44             sub user_base : Chained('/') CaptureArg(0) { ... }
45              
46             sub get_user : Chained('user_base') Args(1) GET { ... }
47             sub post_user : Chained('user_base') Args(1) POST { ... }
48             sub put_user : Chained('user_base') Args(1) PUT { ... }
49             sub delete_user : Chained('user_base') Args(1) DELETE { ... }
50             sub head_user : Chained('user_base') Args(1) HEAD { ... }
51             sub options_user : Chained('user_base') Args(1) OPTIONS { ... }
52             sub patch_user : Chained('user_base') Args(1) PATCH { ... }
53              
54              
55             sub post_and_put : Chained('user_base') POST PUT Args(1) { ... }
56             sub method_attr : Chained('user_base') Method('DELETE') Args(0) { ... }
57              
58             __PACKAGE__->meta->make_immutable;
59              
60             =head1 DESCRIPTION
61              
62             This is an action role that lets your L<Catalyst::Action> match on standard
63             HTTP methods, such as GET, POST, etc.
64              
65             Since most web browsers have limited support for rich HTTP Method vocabularies
66             we use L<Plack::Middleware::MethodOverride> which allows you to 'tunnel' your
67             request method over POST This works in two ways. You can set an extension
68             HTTP header C<X-HTTP-Method-Override> which will contain the value of the
69             desired request method, or you may set a search query parameter
70             C<x-tunneled-method>. Remember, these only work over HTTP Request type
71             POST. See L<Plack::Middleware::MethodOverride> for more.
72              
73             =head1 REQUIRES
74              
75             This role requires the following methods in the consuming class.
76              
77             =head2 match
78              
79             =head2 match_captures
80              
81             Returns 1 if the action matches the existing request and zero if not.
82              
83             =head1 METHODS
84              
85             This role defines the following methods
86              
87             =head2 match
88              
89             =head2 match_captures
90              
91             Around method modifier that return 1 if the request method matches one of the
92             allowed methods (see L</http_methods>) and zero otherwise.
93              
94             =head2 allowed_http_methods
95              
96             An array of strings that are the allowed http methods for matching this action
97             normalized as noted above (using X-Method* overrides).
98              
99             =head2 list_extra_info
100              
101             Adds a key => [@values] "HTTP_METHODS" whose value is an ArrayRef of sorted
102             allowed methods to the ->list_extra_info HashRef. This is used primarily for
103             debugging output.
104              
105             =head2 _has_expected_http_method ($expected)
106              
107             Private method which returns 1 if C<$expected> matches one of the allowed
108             in L</http_methods> and zero otherwise.
109              
110             =head1 AUTHORS
111              
112             Catalyst Contributors, see Catalyst.pm
113              
114             =head1 COPYRIGHT
115              
116             This library is free software. You can redistribute it and/or modify it under
117             the same terms as Perl itself.
118              
119             =cut