View | Details | Raw Unified | Return to bug 19652
Collapse All | Expand All

(-)a/Koha/REST/V1.pm (+35 lines)
Lines 51-57 sub startup { Link Here
51
        $self->secrets([$secret_passphrase]);
51
        $self->secrets([$secret_passphrase]);
52
    }
52
    }
53
53
54
    $self->app->hook(before_dispatch => \&log_request);
54
    $self->app->hook(before_render => \&default_exception_handling);
55
    $self->app->hook(before_render => \&default_exception_handling);
56
    $self->app->hook(before_render => \&log_response);
55
57
56
    $self->plugin(OpenAPI => {
58
    $self->plugin(OpenAPI => {
57
        url => $self->home->rel_file("api/v1/swagger/swagger.json"),
59
        url => $self->home->rel_file("api/v1/swagger/swagger.json"),
Lines 89-92 sub default_exception_handling { Link Here
89
    }
91
    }
90
}
92
}
91
93
94
=head3 log_request
95
96
=cut
97
98
sub log_request {
99
    my ($c) = @_;
100
101
    eval {
102
        $c->app->log->trace(sub { Data::Dumper::Dumper($c->req) });
103
        $c->app->log->debug(
104
            'Request JSON body ' . Mojo::JSON::encode_json($c->req->json)
105
        );
106
        $c->app->log->debug(
107
            'Request params ' . Mojo::JSON::encode_json($c->req->params->to_hash)
108
        );
109
    };
110
}
111
112
=head3 log_response
113
114
=cut
115
116
sub log_response {
117
    my ($c, $args) = @_;
118
119
    eval {
120
        $c->app->log->trace(sub { Data::Dumper::Dumper($c->res) });
121
        $c->app->log->debug(
122
            'Rendering response ' . Mojo::JSON::encode_json($args)
123
        );
124
    };
125
}
126
92
1;
127
1;
(-)a/t/db_dependent/api/v1.t (-3 / +51 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 29-35 use Mojolicious::Lite; Link Here
29
use Try::Tiny;
29
use Try::Tiny;
30
30
31
my $config = {
31
my $config = {
32
    'log4perl.logger.rest.Koha.REST.V1' => 'ERROR, TEST',
32
    'log4perl.logger.rest.Koha.REST.V1' => 'DEBUG, TEST',
33
    'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer',
33
    'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer',
34
    'log4perl.appender.TEST.layout' => 'SimpleLayout',
34
    'log4perl.appender.TEST.layout' => 'SimpleLayout',
35
};
35
};
Lines 103-108 subtest 'default_exception_handling() tests' => sub { Link Here
103
    };
103
    };
104
};
104
};
105
105
106
subtest 'log_request() and log_response() tests' => sub {
107
    plan tests => 12;
108
109
    $t->app->routes->get('/response/log/json' => sub {
110
        $_[0]->render( status => 200, json => { wow => "it worked" } )
111
    });
112
    $t->app->routes->post('/response/log/other' => sub {
113
        $_[0]->render( status => 200, data => '<b>ERROR!</b>' )
114
    });
115
    $t->app->routes->put('/response/log/500' => sub {
116
        die;
117
    });
118
119
    my $appender = Log::Log4perl->appenders->{TEST};
120
121
    $t->get_ok('/response/log/json')
122
      ->status_is(200)
123
      ->json_is('/wow' => 'it worked');
124
    is($appender->buffer,
125
       "DEBUG - Request JSON body null\nDEBUG - Request params {}\n".
126
       "DEBUG - Rendering response {\"json\":{\"wow\":\"it worked\"},\"status\":200}\n",
127
       'Found request and response content'
128
    );
129
    $appender->{appender}->{buffer} = undef;
130
131
    $t->post_ok('/response/log/other' => form => { param1 => "value" })
132
      ->status_is(200)
133
      ->content_is('<b>ERROR!</b>');
134
    is($appender->buffer,
135
       "DEBUG - Request JSON body null\nDEBUG - Request params {\"param1\":\"value\"}\n".
136
       "DEBUG - Rendering response {\"data\":\"<b>ERROR!<\\/b>\",\"status\":200}\n",
137
       'Found request and response content'
138
    );
139
    $appender->{appender}->{buffer} = undef;
140
141
    $t->put_ok('/response/log/500' => json => { param2 => "value" })
142
      ->status_is(500)
143
      ->json_is('/error' => 'Something went wrong, check the logs.');
144
    like($appender->buffer,
145
qr{DEBUG - Request JSON body \{"param2":"value"\}\nDEBUG - Request params \{\}
146
ERROR - Died at .* line \d+\.\n
147
DEBUG - Rendering response \{"json":\{"error":"Something went wrong, check the logs\."\},"status":500\}
148
}msi,
149
       'Found request and response content'
150
    );
151
    $appender->{appender}->{buffer} = undef;
152
153
};
154
106
sub add_default_exception_routes {
155
sub add_default_exception_routes {
107
    my ($t) = @_;
156
    my ($t) = @_;
108
157
109
- 

Return to bug 19652