Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::More; |
| 3 | use Test::Mojo; |
| 4 | use Mojo::File qw/path/; |
| 5 | use Data::Dumper; |
| 6 | |
| 7 | |
| 8 | ##################### |
| 9 | # Start Fake server # |
| 10 | ##################### |
| 11 | my $mount_point = '/realapi/'; |
| 12 | $ENV{KALAMAR_API} = $mount_point; |
| 13 | |
| 14 | my $t = Test::Mojo->new('Kalamar' => { |
| 15 | Kalamar => { |
Akron | b7876a8 | 2019-07-18 13:09:00 +0200 | [diff] [blame] | 16 | plugins => ['Auth'], |
| 17 | experimental_proxy => 1 |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 18 | } |
| 19 | }); |
| 20 | |
| 21 | # Mount fake backend |
| 22 | # Get the fixture path |
| 23 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server'); |
| 24 | my $fake_backend = $t->app->plugin( |
| 25 | Mount => { |
| 26 | $mount_point => |
| 27 | $fixtures_path->child('mock.pl') |
| 28 | } |
| 29 | ); |
| 30 | # Configure fake backend |
| 31 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 32 | |
| 33 | # Globally set server |
| 34 | $t->app->ua->server->app($t->app); |
| 35 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 36 | my $rendered = 0; |
| 37 | $t->app->hook( |
| 38 | after_render => sub { |
| 39 | $rendered++; |
| 40 | } |
| 41 | ); |
| 42 | |
| 43 | $t->get_ok('/doc') |
| 44 | ->status_is(200) |
| 45 | ->text_like('title', qr!KorAP!) |
| 46 | ; |
| 47 | |
| 48 | is($rendered, 1); |
| 49 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 50 | $t->get_ok('/realapi/v1.0') |
| 51 | ->status_is(200) |
| 52 | ->content_is('Fake server available') |
| 53 | ; |
| 54 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 55 | is($rendered, 1); |
| 56 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 57 | $t->get_ok('/api/v1.0/') |
| 58 | ->status_is(200) |
| 59 | ->content_is('Fake server available') |
| 60 | ; |
| 61 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 62 | # Proxy renders |
| 63 | is($rendered, 2); |
| 64 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 65 | $t->get_ok('/api/v1.0/search?ql=cosmas3') |
| 66 | ->status_is(400) |
| 67 | ->json_is('/errors/0/0','307') |
Akron | 56b3d0d | 2019-07-10 17:13:31 +0200 | [diff] [blame] | 68 | ->header_is('connection', 'close') |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 69 | ; |
| 70 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 71 | # Proxy renders |
| 72 | is($rendered, 3); |
| 73 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 74 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 75 | username => 'test', |
| 76 | password => 'pass' |
| 77 | }) |
| 78 | ->status_is(400) |
| 79 | ->json_is('/errors/0/1','Grant Type unknown') |
| 80 | ; |
| 81 | |
| 82 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 83 | grant_type => 'password', |
| 84 | client_id => 2, |
| 85 | client_secret => 'k414m4r-s3cr3t', |
| 86 | username => 'test', |
| 87 | password => 'pass' |
| 88 | }) |
| 89 | ->status_is(200) |
| 90 | ->json_is('/token_type', 'Bearer') |
| 91 | ; |
| 92 | |
| 93 | |
| 94 | done_testing; |
| 95 | __END__ |