blob: 7c54127e3ab4a834829b3c1f8cff22290bc8f4eb [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 experimental_proxy => 1,
18 proxy_inactivity_timeout => 99,
19 proxy_connect_timeout => 66,
Akron63d963b2019-07-05 15:35:51 +020020 }
21});
22
23# Mount fake backend
24# Get the fixture path
25my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
26my $fake_backend = $t->app->plugin(
27 Mount => {
28 $mount_point =>
29 $fixtures_path->child('mock.pl')
30 }
31);
32# Configure fake backend
Akronb5b70d02019-12-17 07:58:03 +010033my $fake_app = $fake_backend->pattern->defaults->{app};
34$fake_app->log($t->app->log);
Akron63d963b2019-07-05 15:35:51 +020035
36# Globally set server
37$t->app->ua->server->app($t->app);
38
Akrona130fa52019-08-07 20:12:26 +020039my $rendered = 0;
40$t->app->hook(
41 after_render => sub {
42 $rendered++;
43 }
44);
45
46$t->get_ok('/doc')
47 ->status_is(200)
48 ->text_like('title', qr!KorAP!)
49 ;
50
51is($rendered, 1);
52
Akron63d963b2019-07-05 15:35:51 +020053$t->get_ok('/realapi/v1.0')
54 ->status_is(200)
55 ->content_is('Fake server available')
56 ;
57
Akrona130fa52019-08-07 20:12:26 +020058is($rendered, 1);
59
Akron63d963b2019-07-05 15:35:51 +020060$t->get_ok('/api/v1.0/')
61 ->status_is(200)
62 ->content_is('Fake server available')
63 ;
64
Akrona130fa52019-08-07 20:12:26 +020065# Proxy renders
66is($rendered, 2);
67
Akron63d963b2019-07-05 15:35:51 +020068$t->get_ok('/api/v1.0/search?ql=cosmas3')
69 ->status_is(400)
70 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020071 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020072 ;
73
Akrona130fa52019-08-07 20:12:26 +020074# Proxy renders
75is($rendered, 3);
76
Akron63d963b2019-07-05 15:35:51 +020077$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
78 username => 'test',
79 password => 'pass'
80})
81 ->status_is(400)
82 ->json_is('/errors/0/1','Grant Type unknown')
83 ;
84
85$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
86 grant_type => 'password',
87 client_id => 2,
88 client_secret => 'k414m4r-s3cr3t',
89 username => 'test',
90 password => 'pass'
91})
92 ->status_is(200)
93 ->json_is('/token_type', 'Bearer')
94 ;
95
Akronb5b70d02019-12-17 07:58:03 +010096# Create long-running route
97$fake_app->routes->get('/v1.0/longwait')->to(
98 cb => sub {
99 my $c = shift;
100
101 $c->render_later;
102 Mojo::IOLoop->timer(
103 5 => sub {
104 return $c->render(text => 'okay');
105 }
106 );
107 });
108
109# Set proxy timeout
Akron23ab0472019-12-17 16:55:55 +0100110is($t->app->ua->inactivity_timeout, 99);
111is($t->app->ua->connect_timeout, 66);
112
113# Set proxy timeout
Akronb5b70d02019-12-17 07:58:03 +0100114$t->app->ua->inactivity_timeout(1);
115
116$t->get_ok('/api/v1.0/longwait')
117 ->status_is(400)
118 ->content_is('Proxy error: Inactivity timeout')
119 ;
Akron63d963b2019-07-05 15:35:51 +0200120
121done_testing;
122__END__