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