blob: 1db8ac98ec4b2ea77f73f37591334f1254868cea [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)
59 ->content_is('Fake server available')
60 ;
61
Akrona130fa52019-08-07 20:12:26 +020062is($rendered, 1);
63
Akron63d963b2019-07-05 15:35:51 +020064$t->get_ok('/api/v1.0/')
65 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +010066 ->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')
Akron63d963b2019-07-05 15:35:51 +020071 ->content_is('Fake server available')
72 ;
73
Akrona130fa52019-08-07 20:12:26 +020074# Proxy renders
75is($rendered, 2);
76
Akron63d963b2019-07-05 15:35:51 +020077$t->get_ok('/api/v1.0/search?ql=cosmas3')
78 ->status_is(400)
Akron337f15d2021-01-14 12:57:21 +010079 ->header_is('Access-Control-Allow-Origin', '*')
80 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akron63d963b2019-07-05 15:35:51 +020081 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020082 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020083 ;
84
Akrona130fa52019-08-07 20:12:26 +020085# Proxy renders
86is($rendered, 3);
87
Akron63d963b2019-07-05 15:35:51 +020088$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
Akronb5b70d02019-12-17 07:58:03 +0100107# 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
Akron23ab0472019-12-17 16:55:55 +0100121is($t->app->ua->inactivity_timeout, 99);
122is($t->app->ua->connect_timeout, 66);
123
124# Set proxy timeout
Akronb5b70d02019-12-17 07:58:03 +0100125$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 ;
Akron63d963b2019-07-05 15:35:51 +0200131
Akrond00b4272020-02-05 17:00:33 +0100132$t->get_ok('/api/v1.0/redirect-target-a')
133 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +0100134 ->header_is('Access-Control-Allow-Origin', '*')
135 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akrond00b4272020-02-05 17:00:33 +0100136 ->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)
Akron337f15d2021-01-14 12:57:21 +0100148 ->header_is('Access-Control-Allow-Origin', '*')
Akrond00b4272020-02-05 17:00:33 +0100149 ->content_is('Redirect Target!')
150 ;
151
Akron337f15d2021-01-14 12:57:21 +0100152# 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
Akron63d963b2019-07-05 15:35:51 +0200162done_testing;
163__END__