blob: 2b166234080834c2cfb299256b6c5ef4b1852663 [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)
61 ->content_is('Fake server available')
62 ;
63
Akrona130fa52019-08-07 20:12:26 +020064# Proxy renders
65is($rendered, 2);
66
Akron63d963b2019-07-05 15:35:51 +020067$t->get_ok('/api/v1.0/search?ql=cosmas3')
68 ->status_is(400)
69 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020070 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020071 ;
72
Akrona130fa52019-08-07 20:12:26 +020073# Proxy renders
74is($rendered, 3);
75
Akron63d963b2019-07-05 15:35:51 +020076$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
77 username => 'test',
78 password => 'pass'
79})
80 ->status_is(400)
81 ->json_is('/errors/0/1','Grant Type unknown')
82 ;
83
84$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
85 grant_type => 'password',
86 client_id => 2,
87 client_secret => 'k414m4r-s3cr3t',
88 username => 'test',
89 password => 'pass'
90})
91 ->status_is(200)
92 ->json_is('/token_type', 'Bearer')
93 ;
94
Akronb5b70d02019-12-17 07:58:03 +010095# Create long-running route
96$fake_app->routes->get('/v1.0/longwait')->to(
97 cb => sub {
98 my $c = shift;
99
100 $c->render_later;
101 Mojo::IOLoop->timer(
102 5 => sub {
103 return $c->render(text => 'okay');
104 }
105 );
106 });
107
108# Set proxy timeout
Akron23ab0472019-12-17 16:55:55 +0100109is($t->app->ua->inactivity_timeout, 99);
110is($t->app->ua->connect_timeout, 66);
111
112# Set proxy timeout
Akronb5b70d02019-12-17 07:58:03 +0100113$t->app->ua->inactivity_timeout(1);
114
115$t->get_ok('/api/v1.0/longwait')
116 ->status_is(400)
117 ->content_is('Proxy error: Inactivity timeout')
118 ;
Akron63d963b2019-07-05 15:35:51 +0200119
Akrond00b4272020-02-05 17:00:33 +0100120$t->get_ok('/api/v1.0/redirect-target-a')
121 ->status_is(200)
122 ->content_is('Redirect Target!')
123 ;
124
125$t->get_ok('/api/v1.0/redirect')
126 ->status_is(308)
127 ->content_is('')
128 ;
129
130$t->ua->max_redirects(2);
131
132$t->get_ok('/api/v1.0/redirect')
133 ->status_is(200)
134 ->content_is('Redirect Target!')
135 ;
136
Akron63d963b2019-07-05 15:35:51 +0200137done_testing;
138__END__