File Coverage

blib/lib/Mojolicious/Plugin/MethodOverride.pm
Criterion Covered Total %
statement 27 27 100.0
branch 14 14 100.0
condition 7 9 77.7
subroutine 6 6 100.0
pod 1 1 100.0
total 55 57 96.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::MethodOverride;
2              
3 5     5   16637 use 5.010;
  5         21  
4 5     5   457 use Mojo::Base 'Mojolicious::Plugin';
  5         6808  
  5         28  
5              
6 5     5   1380 use Scalar::Util qw(weaken);
  5         9  
  5         1777  
7              
8             our $VERSION = '0.060';
9              
10             sub register {
11 4     4 1 128 my ($self, $app, $conf) = @_;
12              
13             my $header =
14 4 100       9 exists $conf->{header} ? $conf->{header} : 'X-HTTP-Method-Override';
15 4 100       10 my $param = exists $conf->{param} ? $conf->{param} : undef;
16              
17             $app->hook(
18             after_build_tx => sub {
19 39     39   207968 my $tx = shift;
20              
21 39         107 weaken $tx;
22              
23             $tx->req->content->on(body => sub {
24 39         16896 my $req = $tx->req;
25              
26 39 100       185 return unless $req->method eq 'POST';
27              
28 35   100     275 my $method = defined $header && $req->headers->header($header);
29              
30 35 100       339 if ($method) {
    100          
31 8         20 $req->headers->remove($header);
32             }
33             elsif (defined $param) {
34 21         47 $method = $req->url->query->clone->param($param);
35             }
36              
37 35 100 66     2293 if ($method and $method =~ /^[A-Za-z]+$/) {
38 21   66     81 $app->log->debug(($header // $param) . ': ' . $method);
39 21         587 $req->method($method);
40             }
41 39         118 });
42             }
43 4         28 );
44              
45 4 100       66 if (defined $param) {
46             $app->hook(before_dispatch => sub {
47 25     25   7839 shift->req->url->query->remove($param);
48 3         12 });
49             }
50             }
51              
52             1;
53              
54             __END__