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