[Bps-public-commit] rt-extension-rest2 branch, dev, updated. f00e2c6c63455c241be7f936f020e891041f9e63

Wallace Reis wreis at bestpractical.com
Fri Feb 20 08:30:15 EST 2015


The branch, dev has been updated
       via  f00e2c6c63455c241be7f936f020e891041f9e63 (commit)
      from  557ebf82074ef0bae4e266be7acd616ef447bf7d (commit)

Summary of changes:
 lib/RT/Extension/REST2.pm                        |  1 +
 lib/RT/Extension/REST2/Middleware/ErrorAsJSON.pm | 30 ++++++++++++++++++++++++
 t/acceptance/main.t                              | 14 +++++------
 t/acceptance/not_found.t                         |  9 +++----
 4 files changed, 40 insertions(+), 14 deletions(-)
 create mode 100644 lib/RT/Extension/REST2/Middleware/ErrorAsJSON.pm

- Log -----------------------------------------------------------------
commit f00e2c6c63455c241be7f936f020e891041f9e63
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Fri Feb 20 09:45:11 2015 -0300

    Error response in JSON format

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index 3ad1817..a3d6591 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -194,6 +194,7 @@ sub to_app {
     RT::ConnectToDatabase();
 
     return builder {
+        enable '+RT::Extension::REST2::Middleware::ErrorAsJSON';
         enable '+RT::Extension::REST2::Middleware::Log';
         enable '+RT::Extension::REST2::Middleware::Auth';
         enable 'RequestHeaders',
diff --git a/lib/RT/Extension/REST2/Middleware/ErrorAsJSON.pm b/lib/RT/Extension/REST2/Middleware/ErrorAsJSON.pm
new file mode 100644
index 0000000..7bf3686
--- /dev/null
+++ b/lib/RT/Extension/REST2/Middleware/ErrorAsJSON.pm
@@ -0,0 +1,30 @@
+package RT::Extension::REST2::Middleware::ErrorAsJSON;
+
+use strict;
+use warnings;
+
+use base 'Plack::Middleware';
+
+use Plack::Util;
+use HTTP::Status qw(is_error status_message);
+use RT::Extension::REST2::Util 'error_as_json';
+
+sub call {
+    my ( $self, $env ) = @_;
+    my $res = $self->app->($env);
+    return Plack::Util::response_cb($res, sub {
+        my $psgi_res = shift;
+        my $status_code = $psgi_res->[0];
+        my $headers = $psgi_res->[1];
+        my $is_json
+            = Plack::Util::header_get($headers, 'content-type') =~ m/json/i;
+        if ( is_error($status_code) && !$is_json ) {
+            my $plack_res = Plack::Response->new($status_code, $headers);
+            error_as_json($plack_res, undef, status_message($status_code));
+            @$psgi_res = @{ $plack_res->finalize };
+        }
+        return;
+    });
+}
+
+1;
diff --git a/t/acceptance/main.t b/t/acceptance/main.t
index 468e27d..efbe587 100644
--- a/t/acceptance/main.t
+++ b/t/acceptance/main.t
@@ -13,8 +13,9 @@ my $json = JSON->new->utf8;
 {
     ok(my $res = $mech->get($rest_base_path), "GET $rest_base_path");
     is($res->code, 401, 'Unauthorized');
-    is($res->content, 'Authorization required');
-    like($res->header('content-type'), qr{text/plain});
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = try { $json->decode($res->content) });
+    is($data->{'message'}, 'Unauthorized');
     like($res->header('www-authenticate'), qr/example\.com\s+REST\s+API/);
 }
 
@@ -45,12 +46,9 @@ my $auth = RT::Extension::REST2::Test->authorization_header;
     ), "POST $rest_base_path");
     is($res->code, 405);
     like($res->header('allow'), qr/GET|HEAD|OPTIONS/);
-    TODO : {
-        local $TODO = 'Error response in JSON format';
-        like($res->header('content-type'), qr{application/json});
-        ok(my $data = try { $json->decode($res->content) });
-        is($data->{'message'}, 'Method not allowed');
-    }
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = try { $json->decode($res->content) });
+    is($data->{'message'}, 'Method Not Allowed');
 }
 
 done_testing;
diff --git a/t/acceptance/not_found.t b/t/acceptance/not_found.t
index cd68392..560727f 100644
--- a/t/acceptance/not_found.t
+++ b/t/acceptance/not_found.t
@@ -14,12 +14,9 @@ my $json = JSON->new->utf8;
 sub check_404 {
     my $res = shift;
     is($res->code, 404);
-    TODO : {
-        local $TODO = 'Error response in JSON format';
-        like($res->header('content-type'), qr{application/json});
-        ok(my $data = try { $json->decode($res->content) });
-        is($data->{'message'}, 'Not found');
-    }
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = try { $json->decode($res->content) });
+    is($data->{'message'}, 'Not Found');
 }
 
 {

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list