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