blob: 3c7e4eaeecb8c4ec96e68372631f8fcac841012e [file] [log] [blame]
Akron63d963b2019-07-05 15:35:51 +02001use Mojo::Base -strict;
2use Test::More;
3use Test::Mojo;
4use Mojo::File qw/path/;
5use Data::Dumper;
6
7
8#####################
9# Start Fake server #
10#####################
11my $mount_point = '/realapi/';
12$ENV{KALAMAR_API} = $mount_point;
13
14my $t = Test::Mojo->new('Kalamar' => {
15 Kalamar => {
Akronb7876a82019-07-18 13:09:00 +020016 plugins => ['Auth'],
Akron23ab0472019-12-17 16:55:55 +010017 proxy_inactivity_timeout => 99,
18 proxy_connect_timeout => 66,
Akron7fb78d62021-06-10 12:51:13 +020019 },
20 'Kalamar-Auth' => {
21 client_id => 2,
22 client_secret => 'k414m4r-s3cr3t',
23 oauth2 => 1
Akron63d963b2019-07-05 15:35:51 +020024 }
25});
26
27# Mount fake backend
28# Get the fixture path
29my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
30my $fake_backend = $t->app->plugin(
31 Mount => {
32 $mount_point =>
33 $fixtures_path->child('mock.pl')
34 }
35);
36# Configure fake backend
Akronb5b70d02019-12-17 07:58:03 +010037my $fake_app = $fake_backend->pattern->defaults->{app};
38$fake_app->log($t->app->log);
Akron63d963b2019-07-05 15:35:51 +020039
40# Globally set server
41$t->app->ua->server->app($t->app);
42
Akrona130fa52019-08-07 20:12:26 +020043my $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
55is($rendered, 1);
56
Akron63d963b2019-07-05 15:35:51 +020057$t->get_ok('/realapi/v1.0')
58 ->status_is(200)
Akronb9070eb2025-12-16 12:17:59 +010059 ->content_is('Fake server available: 1.0')
Akron63d963b2019-07-05 15:35:51 +020060 ;
61
Akronb9070eb2025-12-16 12:17:59 +010062$t->get_ok('/realapi/v1.1')
63 ->status_is(200)
64 ->content_is('Fake server available: 1.1')
65 ;
66
67
Akrona130fa52019-08-07 20:12:26 +020068is($rendered, 1);
69
Akron63d963b2019-07-05 15:35:51 +020070$t->get_ok('/api/v1.0/')
71 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +010072 ->header_is('X-Proxy', 'Kalamar')
73 ->header_is('X-Robots', 'noindex')
74 ->header_is('Connection', 'close')
75 ->header_is('Access-Control-Allow-Origin', '*')
76 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akronb9070eb2025-12-16 12:17:59 +010077 ->content_is('Fake server available: 1.0')
Akron63d963b2019-07-05 15:35:51 +020078 ;
79
Akrona130fa52019-08-07 20:12:26 +020080# Proxy renders
81is($rendered, 2);
82
Akron63d963b2019-07-05 15:35:51 +020083$t->get_ok('/api/v1.0/search?ql=cosmas3')
84 ->status_is(400)
Akron337f15d2021-01-14 12:57:21 +010085 ->header_is('Access-Control-Allow-Origin', '*')
86 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akron63d963b2019-07-05 15:35:51 +020087 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020088 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020089 ;
90
Akrona130fa52019-08-07 20:12:26 +020091# Proxy renders
92is($rendered, 3);
93
Akron63d963b2019-07-05 15:35:51 +020094$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
95 username => 'test',
96 password => 'pass'
97})
98 ->status_is(400)
99 ->json_is('/errors/0/1','Grant Type unknown')
100 ;
101
102$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
103 grant_type => 'password',
104 client_id => 2,
105 client_secret => 'k414m4r-s3cr3t',
106 username => 'test',
107 password => 'pass'
108})
109 ->status_is(200)
110 ->json_is('/token_type', 'Bearer')
111 ;
112
Akronb5b70d02019-12-17 07:58:03 +0100113# Create long-running route
114$fake_app->routes->get('/v1.0/longwait')->to(
115 cb => sub {
116 my $c = shift;
117
118 $c->render_later;
119 Mojo::IOLoop->timer(
120 5 => sub {
121 return $c->render(text => 'okay');
122 }
123 );
124 });
125
126# Set proxy timeout
Akron23ab0472019-12-17 16:55:55 +0100127is($t->app->ua->inactivity_timeout, 99);
128is($t->app->ua->connect_timeout, 66);
129
130# Set proxy timeout
Akronb5b70d02019-12-17 07:58:03 +0100131$t->app->ua->inactivity_timeout(1);
132
133$t->get_ok('/api/v1.0/longwait')
134 ->status_is(400)
135 ->content_is('Proxy error: Inactivity timeout')
136 ;
Akron63d963b2019-07-05 15:35:51 +0200137
Akrond00b4272020-02-05 17:00:33 +0100138$t->get_ok('/api/v1.0/redirect-target-a')
139 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +0100140 ->header_is('Access-Control-Allow-Origin', '*')
141 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akrond00b4272020-02-05 17:00:33 +0100142 ->content_is('Redirect Target!')
143 ;
144
145$t->get_ok('/api/v1.0/redirect')
146 ->status_is(308)
147 ->content_is('')
148 ;
149
150$t->ua->max_redirects(2);
151
152$t->get_ok('/api/v1.0/redirect')
153 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +0100154 ->header_is('Access-Control-Allow-Origin', '*')
Akrond00b4272020-02-05 17:00:33 +0100155 ->content_is('Redirect Target!')
156 ;
157
Akron337f15d2021-01-14 12:57:21 +0100158# Check CORS
159$t->options_ok('/api/v1.0/search?ql=cosmas3')
160 ->status_is(204)
161 ->content_is('')
162 ->header_is('Access-Control-Allow-Origin', '*')
163 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
164 ->header_is('Access-Control-Max-Age', '86400')
165 ;
166
Akronacb7f3d2025-09-17 11:56:45 +0200167# General proxy mounts
168my $plugin_path = '/realplugin';
169
170$t = Test::Mojo->new('Kalamar' => {
171 Kalamar => {
172 proxies => [
173 'PROXY_STUB',
174 {
175 root_path => '/plugin/hello',
176 mount => $plugin_path,
177 service => 'export-plugin-proxy'
178 }
179 ],
180 proxy_inactivity_timeout => 99,
181 proxy_connect_timeout => 66,
182 }
183});
184
185my $fake_plugin = $t->app->plugin(
186 Mount => {
187 $plugin_path =>
188 $fixtures_path->child('plugin-ex.pl')
189 }
190);
191
192# Configure fake plugin
193my $fake_plugin_app = $fake_plugin->pattern->defaults->{app};
194$fake_plugin_app->log($t->app->log);
195
196# Globally set server
197$t->app->ua->server->app($t->app);
198
199$t->get_ok('/realplugin')
200 ->status_is(200)
201 ->content_is('Hello base world!');
202
203$t->get_ok('/realplugin/huhux')
204 ->status_is(200)
205 ->content_is('Hello world! huhux');
206
207$t->get_ok('/plugin/hello/huhux')
208 ->status_is(200)
209 ->content_is('Hello world! huhux');
210
211# require Mojolicious::Command::routes;
212# use Mojo::Util qw(encode tablify);
213# my $rows = [];
214# Mojolicious::Command::routes::_walk($_, 0, $rows, 0) for @{$t->app->routes->children};
215# warn encode('UTF-8', tablify($rows));
Akron337f15d2021-01-14 12:57:21 +0100216
Akron63d963b2019-07-05 15:35:51 +0200217done_testing;
218__END__