Improve error handling for non-redirect errors

Change-Id: I7b8b62e52b08ed92edf02c770fd908be47917505
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index a2cc73c..67ddedd 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -955,6 +955,23 @@
 
 $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
+  state => 'abcde',
+  scope => 'search match',
+  redirect_uri_server => 'http://example.com/',
+  redirect_uri => $t->app->close_redirect_to('http://wrong'),
+  csrf_token => $csrf,
+}))
+  ->status_is(302)
+  ->header_is('location', '/settings/oauth')
+  ->tx->res->headers->header('location')
+  ;
+
+$t->get_ok('/settings/oauth')
+  ->text_is('div.notify-error', 'Invalid redirect URI')
+  ;
+
+$t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
+  client_id => 'xyz',
   state => 'fail',
   scope => 'search match',
 # redirect_uri_server => 'http://example.com/',
diff --git a/t/server/mock.pl b/t/server/mock.pl
index ab0c34d..e615306 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -697,6 +697,14 @@
       return $c->rendered;
     };
 
+    if (index($redirect_uri,'http://wrong') >= 0) {
+      return $c->render(
+        code => 400,
+        content_type => 'text/plain',
+        text => '{"error_description":"Invalid redirect URI","state":"ZMwDGTZ2RY","error":"invalid_request"}'
+      );
+    };
+
     return $c->redirect_to(
       Mojo::URL->new($redirect_uri)->query({
         code => $tokens{auth_token_1},
@@ -713,7 +721,13 @@
         scope => 'match_info search openid'
       })
       );
-  }
+  };
+
+  return $c->render(
+    code => 400,
+    content_type => 'text/plain',
+    content => 'Unknown'
+  );
 };