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

(-)a/Koha/REST/V1.pm (+35 lines)
Lines 47-53 sub startup { Link Here
47
        shift->req->url->base->path('/');
47
        shift->req->url->base->path('/');
48
    });
48
    });
49
49
50
    $self->app->hook(before_dispatch => \&log_request);
50
    $self->app->hook(before_render => \&default_exception_handling);
51
    $self->app->hook(before_render => \&default_exception_handling);
52
    $self->app->hook(before_render => \&log_response);
51
53
52
    # Force charset=utf8 in Content-Type header for JSON responses
54
    # Force charset=utf8 in Content-Type header for JSON responses
53
    $self->types->type( json    => 'application/json; charset=utf8' );
55
    $self->types->type( json    => 'application/json; charset=utf8' );
Lines 122-125 sub default_exception_handling { Link Here
122
    }
124
    }
123
}
125
}
124
126
127
=head3 log_request
128
129
=cut
130
131
sub log_request {
132
    my ($c) = @_;
133
134
    eval {
135
        $c->app->log->trace(sub { Data::Dumper::Dumper($c->req) });
136
        $c->app->log->debug(
137
            'Request JSON body ' . Mojo::JSON::encode_json($c->req->json)
138
        );
139
        $c->app->log->debug(
140
            'Request params ' . Mojo::JSON::encode_json($c->req->params->to_hash)
141
        );
142
    };
143
}
144
145
=head3 log_response
146
147
=cut
148
149
sub log_response {
150
    my ($c, $args) = @_;
151
152
    eval {
153
        $c->app->log->trace(sub { Data::Dumper::Dumper($c->res) });
154
        $c->app->log->debug(
155
            'Rendering response ' . Mojo::JSON::encode_json($args)
156
        );
157
    };
158
}
159
125
1;
160
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 30-36 use Mojolicious::Lite; Link Here
30
use Try::Tiny;
30
use Try::Tiny;
31
31
32
my $config = {
32
my $config = {
33
    'log4perl.logger.rest.Koha.REST.V1' => 'ERROR, TEST',
33
    'log4perl.logger.rest.Koha.REST.V1' => 'DEBUG, TEST',
34
    'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer',
34
    'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer',
35
    'log4perl.appender.TEST.layout' => 'SimpleLayout',
35
    'log4perl.appender.TEST.layout' => 'SimpleLayout',
36
};
36
};
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