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