blob: 41b0acfd8ec898748b0b08493929423dbafda07c [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'],
17 experimental_proxy => 1
Akron63d963b2019-07-05 15:35:51 +020018 }
19});
20
21# Mount fake backend
22# Get the fixture path
23my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
24my $fake_backend = $t->app->plugin(
25 Mount => {
26 $mount_point =>
27 $fixtures_path->child('mock.pl')
28 }
29);
30# Configure fake backend
Akronb5b70d02019-12-17 07:58:03 +010031my $fake_app = $fake_backend->pattern->defaults->{app};
32$fake_app->log($t->app->log);
Akron63d963b2019-07-05 15:35:51 +020033
34# Globally set server
35$t->app->ua->server->app($t->app);
36
Akrona130fa52019-08-07 20:12:26 +020037my $rendered = 0;
38$t->app->hook(
39 after_render => sub {
40 $rendered++;
41 }
42);
43
44$t->get_ok('/doc')
45 ->status_is(200)
46 ->text_like('title', qr!KorAP!)
47 ;
48
49is($rendered, 1);
50
Akron63d963b2019-07-05 15:35:51 +020051$t->get_ok('/realapi/v1.0')
52 ->status_is(200)
53 ->content_is('Fake server available')
54 ;
55
Akrona130fa52019-08-07 20:12:26 +020056is($rendered, 1);
57
Akron63d963b2019-07-05 15:35:51 +020058$t->get_ok('/api/v1.0/')
59 ->status_is(200)
60 ->content_is('Fake server available')
61 ;
62
Akrona130fa52019-08-07 20:12:26 +020063# Proxy renders
64is($rendered, 2);
65
Akron63d963b2019-07-05 15:35:51 +020066$t->get_ok('/api/v1.0/search?ql=cosmas3')
67 ->status_is(400)
68 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020069 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020070 ;
71
Akrona130fa52019-08-07 20:12:26 +020072# Proxy renders
73is($rendered, 3);
74
Akron63d963b2019-07-05 15:35:51 +020075$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
76 username => 'test',
77 password => 'pass'
78})
79 ->status_is(400)
80 ->json_is('/errors/0/1','Grant Type unknown')
81 ;
82
83$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
84 grant_type => 'password',
85 client_id => 2,
86 client_secret => 'k414m4r-s3cr3t',
87 username => 'test',
88 password => 'pass'
89})
90 ->status_is(200)
91 ->json_is('/token_type', 'Bearer')
92 ;
93
Akronb5b70d02019-12-17 07:58:03 +010094# Create long-running route
95$fake_app->routes->get('/v1.0/longwait')->to(
96 cb => sub {
97 my $c = shift;
98
99 $c->render_later;
100 Mojo::IOLoop->timer(
101 5 => sub {
102 return $c->render(text => 'okay');
103 }
104 );
105 });
106
107# Set proxy timeout
108$t->app->ua->inactivity_timeout(1);
109
110$t->get_ok('/api/v1.0/longwait')
111 ->status_is(400)
112 ->content_is('Proxy error: Inactivity timeout')
113 ;
Akron63d963b2019-07-05 15:35:51 +0200114
115done_testing;
116__END__