blob: 518d7a4ee47dbf6241a3c70186f972dc383fb1a9 [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,
Akron63d963b2019-07-05 15:35:51 +020019 }
20});
21
22# Mount fake backend
23# Get the fixture path
24my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
25my $fake_backend = $t->app->plugin(
26 Mount => {
27 $mount_point =>
28 $fixtures_path->child('mock.pl')
29 }
30);
31# Configure fake backend
Akronb5b70d02019-12-17 07:58:03 +010032my $fake_app = $fake_backend->pattern->defaults->{app};
33$fake_app->log($t->app->log);
Akron63d963b2019-07-05 15:35:51 +020034
35# Globally set server
36$t->app->ua->server->app($t->app);
37
Akrona130fa52019-08-07 20:12:26 +020038my $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
50is($rendered, 1);
51
Akron63d963b2019-07-05 15:35:51 +020052$t->get_ok('/realapi/v1.0')
53 ->status_is(200)
54 ->content_is('Fake server available')
55 ;
56
Akrona130fa52019-08-07 20:12:26 +020057is($rendered, 1);
58
Akron63d963b2019-07-05 15:35:51 +020059$t->get_ok('/api/v1.0/')
60 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +010061 ->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')
Akron63d963b2019-07-05 15:35:51 +020066 ->content_is('Fake server available')
67 ;
68
Akrona130fa52019-08-07 20:12:26 +020069# Proxy renders
70is($rendered, 2);
71
Akron63d963b2019-07-05 15:35:51 +020072$t->get_ok('/api/v1.0/search?ql=cosmas3')
73 ->status_is(400)
Akron337f15d2021-01-14 12:57:21 +010074 ->header_is('Access-Control-Allow-Origin', '*')
75 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akron63d963b2019-07-05 15:35:51 +020076 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020077 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020078 ;
79
Akrona130fa52019-08-07 20:12:26 +020080# Proxy renders
81is($rendered, 3);
82
Akron63d963b2019-07-05 15:35:51 +020083$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
Akronb5b70d02019-12-17 07:58:03 +0100102# 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
Akron23ab0472019-12-17 16:55:55 +0100116is($t->app->ua->inactivity_timeout, 99);
117is($t->app->ua->connect_timeout, 66);
118
119# Set proxy timeout
Akronb5b70d02019-12-17 07:58:03 +0100120$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 ;
Akron63d963b2019-07-05 15:35:51 +0200126
Akrond00b4272020-02-05 17:00:33 +0100127$t->get_ok('/api/v1.0/redirect-target-a')
128 ->status_is(200)
Akron337f15d2021-01-14 12:57:21 +0100129 ->header_is('Access-Control-Allow-Origin', '*')
130 ->header_is('Access-Control-Allow-Methods', 'GET, OPTIONS')
Akrond00b4272020-02-05 17:00:33 +0100131 ->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)
Akron337f15d2021-01-14 12:57:21 +0100143 ->header_is('Access-Control-Allow-Origin', '*')
Akrond00b4272020-02-05 17:00:33 +0100144 ->content_is('Redirect Target!')
145 ;
146
Akron337f15d2021-01-14 12:57:21 +0100147# 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
Akron63d963b2019-07-05 15:35:51 +0200157done_testing;
158__END__