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) |
Akron | 337f15d | 2021-01-14 12:57:21 +0100 | [diff] [blame] | 61 | ->header_is('X-Proxy', 'Kalamar') |
| 62 | ->header_is('X-Robots', 'noindex') |
| 63 | ->header_is('Connection', 'close') |
| 64 | ->header_is('Access-Control-Allow-Origin', '*') |
| 65 | ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS') |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 66 | ->content_is('Fake server available') |
| 67 | ; |
| 68 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 69 | # Proxy renders |
| 70 | is($rendered, 2); |
| 71 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 72 | $t->get_ok('/api/v1.0/search?ql=cosmas3') |
| 73 | ->status_is(400) |
Akron | 337f15d | 2021-01-14 12:57:21 +0100 | [diff] [blame] | 74 | ->header_is('Access-Control-Allow-Origin', '*') |
| 75 | ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS') |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 76 | ->json_is('/errors/0/0','307') |
Akron | 56b3d0d | 2019-07-10 17:13:31 +0200 | [diff] [blame] | 77 | ->header_is('connection', 'close') |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 78 | ; |
| 79 | |
Akron | a130fa5 | 2019-08-07 20:12:26 +0200 | [diff] [blame] | 80 | # Proxy renders |
| 81 | is($rendered, 3); |
| 82 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 83 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 84 | username => 'test', |
| 85 | password => 'pass' |
| 86 | }) |
| 87 | ->status_is(400) |
| 88 | ->json_is('/errors/0/1','Grant Type unknown') |
| 89 | ; |
| 90 | |
| 91 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 92 | grant_type => 'password', |
| 93 | client_id => 2, |
| 94 | client_secret => 'k414m4r-s3cr3t', |
| 95 | username => 'test', |
| 96 | password => 'pass' |
| 97 | }) |
| 98 | ->status_is(200) |
| 99 | ->json_is('/token_type', 'Bearer') |
| 100 | ; |
| 101 | |
Akron | b5b70d0 | 2019-12-17 07:58:03 +0100 | [diff] [blame] | 102 | # Create long-running route |
| 103 | $fake_app->routes->get('/v1.0/longwait')->to( |
| 104 | cb => sub { |
| 105 | my $c = shift; |
| 106 | |
| 107 | $c->render_later; |
| 108 | Mojo::IOLoop->timer( |
| 109 | 5 => sub { |
| 110 | return $c->render(text => 'okay'); |
| 111 | } |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | # Set proxy timeout |
Akron | 23ab047 | 2019-12-17 16:55:55 +0100 | [diff] [blame] | 116 | is($t->app->ua->inactivity_timeout, 99); |
| 117 | is($t->app->ua->connect_timeout, 66); |
| 118 | |
| 119 | # Set proxy timeout |
Akron | b5b70d0 | 2019-12-17 07:58:03 +0100 | [diff] [blame] | 120 | $t->app->ua->inactivity_timeout(1); |
| 121 | |
| 122 | $t->get_ok('/api/v1.0/longwait') |
| 123 | ->status_is(400) |
| 124 | ->content_is('Proxy error: Inactivity timeout') |
| 125 | ; |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 126 | |
Akron | d00b427 | 2020-02-05 17:00:33 +0100 | [diff] [blame] | 127 | $t->get_ok('/api/v1.0/redirect-target-a') |
| 128 | ->status_is(200) |
Akron | 337f15d | 2021-01-14 12:57:21 +0100 | [diff] [blame] | 129 | ->header_is('Access-Control-Allow-Origin', '*') |
| 130 | ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS') |
Akron | d00b427 | 2020-02-05 17:00:33 +0100 | [diff] [blame] | 131 | ->content_is('Redirect Target!') |
| 132 | ; |
| 133 | |
| 134 | $t->get_ok('/api/v1.0/redirect') |
| 135 | ->status_is(308) |
| 136 | ->content_is('') |
| 137 | ; |
| 138 | |
| 139 | $t->ua->max_redirects(2); |
| 140 | |
| 141 | $t->get_ok('/api/v1.0/redirect') |
| 142 | ->status_is(200) |
Akron | 337f15d | 2021-01-14 12:57:21 +0100 | [diff] [blame] | 143 | ->header_is('Access-Control-Allow-Origin', '*') |
Akron | d00b427 | 2020-02-05 17:00:33 +0100 | [diff] [blame] | 144 | ->content_is('Redirect Target!') |
| 145 | ; |
| 146 | |
Akron | 337f15d | 2021-01-14 12:57:21 +0100 | [diff] [blame] | 147 | # Check CORS |
| 148 | $t->options_ok('/api/v1.0/search?ql=cosmas3') |
| 149 | ->status_is(204) |
| 150 | ->content_is('') |
| 151 | ->header_is('Access-Control-Allow-Origin', '*') |
| 152 | ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS') |
| 153 | ->header_is('Access-Control-Max-Age', '86400') |
| 154 | ; |
| 155 | |
| 156 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 157 | done_testing; |
| 158 | __END__ |