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 |
Akron | b5b70d0 | 2019-12-17 07:58:03 +0100 | [diff] [blame] | 31 | my $fake_app = $fake_backend->pattern->defaults->{app}; |
| 32 | $fake_app->log($t->app->log); |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 33 | |
| 34 | # Globally set server |
| 35 | $t->app->ua->server->app($t->app); |
| 36 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 37 | my $rendered = 0; |
| 38 | $t->app->hook( |
| 39 | after_render => sub { |
| 40 | $rendered++; |
| 41 | } |
| 42 | ); |
| 43 | |
| 44 | $t->get_ok('/doc') |
| 45 | ->status_is(200) |
| 46 | ->text_like('title', qr!KorAP!) |
| 47 | ; |
| 48 | |
| 49 | is($rendered, 1); |
| 50 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 51 | $t->get_ok('/realapi/v1.0') |
| 52 | ->status_is(200) |
| 53 | ->content_is('Fake server available') |
| 54 | ; |
| 55 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 56 | is($rendered, 1); |
| 57 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 58 | $t->get_ok('/api/v1.0/') |
| 59 | ->status_is(200) |
| 60 | ->content_is('Fake server available') |
| 61 | ; |
| 62 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 63 | # Proxy renders |
| 64 | is($rendered, 2); |
| 65 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 66 | $t->get_ok('/api/v1.0/search?ql=cosmas3') |
| 67 | ->status_is(400) |
| 68 | ->json_is('/errors/0/0','307') |
Akron | 56b3d0d | 2019-07-10 17:13:31 +0200 | [diff] [blame] | 69 | ->header_is('connection', 'close') |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 70 | ; |
| 71 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 72 | # Proxy renders |
| 73 | is($rendered, 3); |
| 74 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 75 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 76 | username => 'test', |
| 77 | password => 'pass' |
| 78 | }) |
| 79 | ->status_is(400) |
| 80 | ->json_is('/errors/0/1','Grant Type unknown') |
| 81 | ; |
| 82 | |
| 83 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 84 | grant_type => 'password', |
| 85 | client_id => 2, |
| 86 | client_secret => 'k414m4r-s3cr3t', |
| 87 | username => 'test', |
| 88 | password => 'pass' |
| 89 | }) |
| 90 | ->status_is(200) |
| 91 | ->json_is('/token_type', 'Bearer') |
| 92 | ; |
| 93 | |
Akron | b5b70d0 | 2019-12-17 07:58:03 +0100 | [diff] [blame] | 94 | # Create long-running route |
| 95 | $fake_app->routes->get('/v1.0/longwait')->to( |
| 96 | cb => sub { |
| 97 | my $c = shift; |
| 98 | |
| 99 | $c->render_later; |
| 100 | Mojo::IOLoop->timer( |
| 101 | 5 => sub { |
| 102 | return $c->render(text => 'okay'); |
| 103 | } |
| 104 | ); |
| 105 | }); |
| 106 | |
| 107 | # Set proxy timeout |
| 108 | $t->app->ua->inactivity_timeout(1); |
| 109 | |
| 110 | $t->get_ok('/api/v1.0/longwait') |
| 111 | ->status_is(400) |
| 112 | ->content_is('Proxy error: Inactivity timeout') |
| 113 | ; |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 114 | |
| 115 | done_testing; |
| 116 | __END__ |