Bugzilla – Attachment 69233 Details for
Bug 19652
REST API: Log request and response content
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19652: Debug-level logging for response JSON
Bug-19652-Debug-level-logging-for-response-JSON.patch (text/plain), 3.42 KB, created by
Lari Taskula
on 2017-11-20 12:18:47 UTC
(
hide
)
Description:
Bug 19652: Debug-level logging for response JSON
Filename:
MIME Type:
Creator:
Lari Taskula
Created:
2017-11-20 12:18:47 UTC
Size:
3.42 KB
patch
obsolete
>From f678b8e9f2835c80222f261aa83b7c44dc62bd42 Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@jns.fi> >Date: Mon, 20 Nov 2017 12:41:34 +0200 >Subject: [PATCH] Bug 19652: Debug-level logging for response JSON > >This patch adds debug-level logging for response JSON. This is useful for viewing >responses via REST API log file. > >To test: >1. prove t/db_dependent/api/v1.t >2. Configure log4perl configuration file for REST API and set log level to DEBUG >3. Make any request to existing REST API endpoint >4. Observe response JSON in your log file >--- > Koha/REST/V1.pm | 11 +++++++++++ > t/db_dependent/api/v1.t | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- > 2 files changed, 57 insertions(+), 2 deletions(-) > >diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm >index 665e9c4..e887799 100644 >--- a/Koha/REST/V1.pm >+++ b/Koha/REST/V1.pm >@@ -52,6 +52,7 @@ sub startup { > } > > $self->app->hook(before_render => \&default_exception_handling); >+ $self->app->hook(before_render => \&log_response); > > $self->plugin(OpenAPI => { > url => $self->home->rel_file("api/v1/swagger/swagger.json"), >@@ -89,4 +90,14 @@ sub default_exception_handling { > } > } > >+=head3 log_response >+ >+=cut >+ >+sub log_response { >+ my ($c, $args) = @_; >+ >+ eval { $c->app->log->debug(Mojo::JSON::encode_json($args)) }; >+} >+ > 1; >diff --git a/t/db_dependent/api/v1.t b/t/db_dependent/api/v1.t >index b5804b5..a74ddcf 100644 >--- a/t/db_dependent/api/v1.t >+++ b/t/db_dependent/api/v1.t >@@ -17,7 +17,7 @@ > > use Modern::Perl; > >-use Test::More tests => 1; >+use Test::More tests => 2; > use Test::Mojo; > use Test::Warn; > >@@ -29,7 +29,7 @@ use Mojolicious::Lite; > use Try::Tiny; > > my $config = { >- 'log4perl.logger.rest.Koha.REST.V1' => 'ERROR, TEST', >+ 'log4perl.logger.rest.Koha.REST.V1' => 'DEBUG, TEST', > 'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer', > 'log4perl.appender.TEST.layout' => 'SimpleLayout', > }; >@@ -103,6 +103,50 @@ subtest 'default_exception_handling() tests' => sub { > }; > }; > >+subtest 'log_response() tests' => sub { >+ plan tests => 12; >+ >+ $t->app->routes->get('/response/log/json' => sub { >+ $_[0]->render( status => 200, json => { wow => "it worked" } ) >+ }); >+ $t->app->routes->get('/response/log/other' => sub { >+ $_[0]->render( status => 200, data => '<b>ERROR!</b>' ) >+ }); >+ $t->app->routes->get('/response/log/500' => sub { >+ die; >+ }); >+ >+ my $appender = Log::Log4perl->appenders->{TEST}; >+ >+ $t->get_ok('/response/log/json') >+ ->status_is(200) >+ ->json_is('/wow' => 'it worked'); >+ is($appender->buffer, >+ "DEBUG - {\"json\":{\"wow\":\"it worked\"},\"status\":200}\n", >+ 'Found response JSON' >+ ); >+ $appender->{appender}->{buffer} = undef; >+ >+ $t->get_ok('/response/log/other') >+ ->status_is(200) >+ ->content_is('<b>ERROR!</b>'); >+ is($appender->buffer, >+ "DEBUG - {\"data\":\"<b>ERROR!<\\/b>\",\"status\":200}\n", >+ 'Found response JSON' >+ ); >+ $appender->{appender}->{buffer} = undef; >+ >+ $t->get_ok('/response/log/500') >+ ->status_is(500) >+ ->json_is('/error' => 'Something went wrong, check the logs.'); >+ like($appender->buffer, >+ qr/DEBUG - \{"json":\{"error":"Something went wrong, check the logs."\},"status":500\}\n/, >+ 'Found response error content' >+ ); >+ $appender->{appender}->{buffer} = undef; >+ >+}; >+ > sub add_default_exception_routes { > my ($t) = @_; > >-- >2.7.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19652
:
69225
|
69233
|
69265
|
69267
|
85718