Added experimental proxy route to backend api - fixes #91

Change-Id: I9a055b33c8cacef6cfa564467a17e7f55a1eeef5
diff --git a/t/corpus_info.t b/t/corpus_info.t
index e174bf0..ecbf2dd 100644
--- a/t/corpus_info.t
+++ b/t/corpus_info.t
@@ -7,7 +7,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar');
diff --git a/t/corpus_info_cache.t b/t/corpus_info_cache.t
index 916a2d7..a11fb8b 100644
--- a/t/corpus_info_cache.t
+++ b/t/corpus_info_cache.t
@@ -7,7 +7,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 # New test with new cache
diff --git a/t/docembed.t b/t/docembed.t
index 24503f7..8224fa7 100644
--- a/t/docembed.t
+++ b/t/docembed.t
@@ -6,7 +6,7 @@
 
 my $t = Test::Mojo->new('Kalamar' => {
   Kalamar => {
-    api => 'xyz',
+    api_path => 'xyz',
     navi_ext => [
       {
         title => 'Privacy',
diff --git a/t/fixtures.t b/t/fixtures.t
index 598b918..6508cfc 100644
--- a/t/fixtures.t
+++ b/t/fixtures.t
@@ -8,23 +8,23 @@
 
 my $t = Test::Mojo->new($mock_server);
 
-$t->get_ok('/')
+$t->get_ok('/v1.0')
   ->status_is(200)
   ->content_is('Fake server available');
 
-$t->get_ok('/search?ql=cosmas3')
+$t->get_ok('/v1.0/search?ql=cosmas3')
   ->status_is(400)
   ->json_is('/errors/0/0',"307")
   ->json_is('/errors/0/1',"cosmas3 is not a supported query language!")
   ;
 
-$t->get_ok('/search?q=server_fail')
+$t->get_ok('/v1.0/search?q=server_fail')
   ->status_is(500)
   ->text_is('#error', '')
   ->content_like(qr!Oooops!)
   ;
 
-$t->get_ok('/search?q=[orth=das&ql=poliqarp&offset=0&count=25')
+$t->get_ok('/v1.0/search?q=[orth=das&ql=poliqarp&offset=0&count=25')
   ->status_is(400)
   ->text_is('#error', '')
   ->json_is('/errors/0/0',302)
@@ -33,7 +33,7 @@
   ->json_is('/errors/1/1','Could not parse query >>> [orth=das <<<.')
   ;
 
-$t->get_ok('/search?q=baum&ql=poliqarp&offset=0&count=25')
+$t->get_ok('/v1.0/search?q=baum&ql=poliqarp&offset=0&count=25')
   ->status_is(200)
   ->text_is('#error', '')
   ->json_is('/meta/count', 25)
@@ -41,7 +41,7 @@
   ->json_is('/matches/0/docSigle', "GOE/AGI")
   ;
 
-$t->get_ok('/corpus/WPD15/232/39681/p2133-2134/matchInfo?spans=false&foundry=*')
+$t->get_ok('/v1.0/corpus/WPD15/232/39681/p2133-2134/matchInfo?spans=false&foundry=*')
   ->status_is(200)
   ->json_is('/textSigle', 'WPD15/232/39681')
   ;
diff --git a/t/match_info.t b/t/match_info.t
index 59f12ed..486fe11 100644
--- a/t/match_info.t
+++ b/t/match_info.t
@@ -7,7 +7,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar');
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index d67837f..0239efe 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -8,7 +8,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar' => {
@@ -34,7 +34,7 @@
 # Configure fake backend
 $fake_backend->pattern->defaults->{app}->log($t->app->log);
 
-$t->get_ok('/api')
+$t->get_ok('/realapi/v1.0')
   ->status_is(200)
   ->content_is('Fake server available');
 
diff --git a/t/plugin/auth.t b/t/plugin/auth.t
index 6b5818d..e310988 100644
--- a/t/plugin/auth.t
+++ b/t/plugin/auth.t
@@ -8,7 +8,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar' => {
@@ -29,7 +29,7 @@
 # Configure fake backend
 $fake_backend->pattern->defaults->{app}->log($t->app->log);
 
-$t->get_ok('/api')
+$t->get_ok('/realapi/v1.0')
   ->status_is(200)
   ->content_is('Fake server available');
 
diff --git a/t/proxy.t b/t/proxy.t
new file mode 100644
index 0000000..f754f11
--- /dev/null
+++ b/t/proxy.t
@@ -0,0 +1,77 @@
+use Mojo::Base -strict;
+use Test::More;
+use Test::Mojo;
+use Mojo::File qw/path/;
+use Data::Dumper;
+
+
+#####################
+# Start Fake server #
+#####################
+my $mount_point = '/realapi/';
+$ENV{KALAMAR_API} = $mount_point;
+
+my $t = Test::Mojo->new('Kalamar' => {
+  Kalamar => {
+    plugins => ['Auth']
+  }
+});
+
+# Mount fake backend
+# Get the fixture path
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
+my $fake_backend = $t->app->plugin(
+  Mount => {
+    $mount_point =>
+      $fixtures_path->child('mock.pl')
+  }
+);
+# Configure fake backend
+$fake_backend->pattern->defaults->{app}->log($t->app->log);
+
+# Globally set server
+$t->app->ua->server->app($t->app);
+
+my $r = $t->app->routes;
+
+# API proxy route
+$r->any('/api/v#apiv' => [apiv => ['1.0']])->to('Proxy#pass');
+$r->any('/api/v#apiv/*path' => [apiv => ['1.0']])->to('Proxy#pass');
+
+$t->get_ok('/realapi/v1.0')
+  ->status_is(200)
+  ->content_is('Fake server available')
+  ;
+
+$t->get_ok('/api/v1.0/')
+  ->status_is(200)
+  ->content_is('Fake server available')
+  ;
+
+$t->get_ok('/api/v1.0/search?ql=cosmas3')
+  ->status_is(400)
+  ->json_is('/errors/0/0','307')
+  ;
+
+$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
+  username => 'test',
+  password => 'pass'
+})
+  ->status_is(400)
+  ->json_is('/errors/0/1','Grant Type unknown')
+  ;
+
+$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
+  grant_type => 'password',
+  client_id => 2,
+  client_secret => 'k414m4r-s3cr3t',
+  username => 'test',
+  password => 'pass'
+})
+  ->status_is(200)
+  ->json_is('/token_type', 'Bearer')
+  ;
+
+
+done_testing;
+__END__
diff --git a/t/query.t b/t/query.t
index 2ce7a90..81c4ac8 100644
--- a/t/query.t
+++ b/t/query.t
@@ -7,7 +7,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar');
diff --git a/t/server/mock.pl b/t/server/mock.pl
index 359f78c..2c7f43b 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -65,13 +65,13 @@
 
 
 # Base page
-get '/' => sub {
+get '/v1.0/' => sub {
   shift->render(text => 'Fake server available');
 };
 
 
 # Search fixtures
-get '/search' => sub {
+get '/v1.0/search' => sub {
   my $c = shift;
   my $v = $c->validation;
   $v->optional('q');
@@ -133,7 +133,7 @@
 };
 
 # Textinfo fixtures
-get '/corpus/:corpusId/:docId/:textId' => sub {
+get '/v1.0/corpus/:corpusId/:docId/:textId' => sub {
   my $c = shift;
 
   my $file = join('_', (
@@ -152,7 +152,7 @@
 
 
 # Matchinfo fixtures
-get '/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub {
+get '/v1.0/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub {
   my $c = shift;
 
   my $file = join('_', (
@@ -172,7 +172,7 @@
 
 
 # Statistics endpoint
-get '/statistics' => sub {
+get '/v1.0/statistics' => sub {
   my $c = shift;
   my $v = $c->validation;
   $v->optional('corpusQuery');
@@ -193,7 +193,7 @@
 ############
 
 # Request API token
-get '/auth/logout' => sub {
+get '/v1.0/auth/logout' => sub {
   my $c = shift;
 
   if (my $auth = $c->req->headers->header('Authorization')) {
@@ -210,7 +210,7 @@
 
 
 # Request API token
-get '/auth/apiToken' => sub {
+get '/v1.0/auth/apiToken' => sub {
   my $c = shift;
 
   # Get auth header
@@ -275,10 +275,12 @@
 
 
 # Request API token
-post '/oauth2/token' => sub {
+post '/v1.0/oauth2/token' => sub {
   my $c = shift;
 
-  if ($c->param('grant_type') eq 'password') {
+  my $grant_type = $c->param('grant_type') // 'undefined';
+
+  if ($grant_type eq 'password') {
 
     # Check for wrong client id
     if ($c->param('client_id') ne '2') {
@@ -346,7 +348,7 @@
   }
 
   # Refresh token
-  elsif ($c->param('grant_type') eq 'refresh_token') {
+  elsif ($grant_type eq 'refresh_token') {
     return $c->render(
       status => 200,
       json => {
@@ -361,10 +363,11 @@
   # Unknown token grant
   else {
     return $c->render(
+      status => 400,
       json => {
         "errors" => [
           [
-            0, "Grant Type unknown", $c->param("grant_type")
+            0, "Grant Type unknown", $grant_type
           ]
         ]
       }
diff --git a/t/text_info.t b/t/text_info.t
index 49be159..82e6a97 100644
--- a/t/text_info.t
+++ b/t/text_info.t
@@ -7,7 +7,7 @@
 #####################
 # Start Fake server #
 #####################
-my $mount_point = '/api/';
+my $mount_point = '/realapi/';
 $ENV{KALAMAR_API} = $mount_point;
 
 my $t = Test::Mojo->new('Kalamar');