blob: 1230e7d1a0b17625ecff3b1fe09d6b0ab39c9562 [file] [log] [blame]
Akronb80341d2018-10-15 19:46:23 +02001use Mojo::Base -strict;
2use Test::Mojo;
3use Test::More;
4use Mojo::File qw/path/;
5
6
7#####################
8# Start Fake server #
9#####################
Akron63d963b2019-07-05 15:35:51 +020010my $mount_point = '/realapi/';
Akronb80341d2018-10-15 19:46:23 +020011$ENV{KALAMAR_API} = $mount_point;
12
Akron33526592026-09-02 13:27:46 +020013my $t = Test::Mojo->new('Kalamar' => {
14 Kalamar => {
15 defaults => {
16 match_info_extended => 'max'
17 }
18 }
19});
Akronb80341d2018-10-15 19:46:23 +020020
21# Mount fake backend
22# Get the fixture path
Akron73f36082018-10-25 15:34:59 +020023my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
Akronb80341d2018-10-15 19:46:23 +020024my $fake_backend = $t->app->plugin(
25 Mount => {
26 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020027 $fixtures_path->child('mock.pl')
Akronb80341d2018-10-15 19:46:23 +020028 }
29);
30# Configure fake backend
31$fake_backend->pattern->defaults->{app}->log($t->app->log);
32
Akron33526592026-09-02 13:27:46 +020033# The config is exposed on rendered pages
34$t->get_ok('/')
35 ->status_is(200)
36 ->element_exists('body[data-match-info-extended="max"]')
37 ;
38
Akronb80341d2018-10-15 19:46:23 +020039# Query passed
Akron3b3a4342021-05-31 11:14:48 +020040$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=false&foundry=*&_format=json')
Akronb80341d2018-10-15 19:46:23 +020041 ->status_is(200)
Akron69481a42021-03-22 10:31:16 +010042 ->content_type_is('application/json;charset=UTF-8')
Akronb80341d2018-10-15 19:46:23 +020043 ->json_is('/textSigle', 'WPD15/232/39681')
Akronb8d0b402018-10-18 23:51:52 +020044 ->json_like('/snippet', qr!<span class=\"context-left\">!)
Akron8ea84292018-10-24 13:41:52 +020045 ->header_isnt('X-Kalamar-Cache', 'true')
Akronb80341d2018-10-15 19:46:23 +020046 ;
47
Akron910828a2025-06-27 15:38:48 +020048$t->get_ok('/corpus/GOE/AGF/02286/p75682-75683?_format=json&response-pipe=glemm')
Akronb8d0b402018-10-18 23:51:52 +020049 ->status_is(200)
50 ->json_is('/textSigle', 'GOE/AGF/02286')
51 ->json_is('/title','Materialien zur Geschichte der Farbenlehre')
Akron910828a2025-06-27 15:38:48 +020052 ->json_is('/meta/responsePipes','glemm')
Akronb8d0b402018-10-18 23:51:52 +020053 ;
54
Akron33526592026-09-02 13:27:46 +020055# Extended match-info request is accepted and forwarded
56$t->get_ok('/corpus/GOE/AGF/02286/p75682-75683?extended=max&_format=json')
57 ->status_is(200)
58 ->json_is('/textSigle', 'GOE/AGF/02286')
59 ;
60
Akronb8d0b402018-10-18 23:51:52 +020061# TODO:
62# It's surprising, that it doesn't return a 404!
Akron3b3a4342021-05-31 11:14:48 +020063$t->get_ok('/corpus/notfound/X/X/p0-1?_format=json')
Akronb8d0b402018-10-18 23:51:52 +020064 ->status_is(200)
65 ->json_is('/textSigle', 'NOTFOUND/X/X')
66 ->json_is('/corpusID', undef)
67 ;
68
Akron3b3a4342021-05-31 11:14:48 +020069$t->get_ok('/corpus/fail/x/x/p0-0?_format=json')
Akron3c390c42020-03-30 09:06:21 +020070 ->status_is(400)
Akronb8d0b402018-10-18 23:51:52 +020071 ->json_is('/notifications/0/0', 'error')
Akron2e2098e2018-10-24 20:16:24 +020072 ->json_like('/notifications/0/1', qr!Unable to load query response from .+?response_matchinfo_fail_x_x_p0-0\.json!)
Akronb8d0b402018-10-18 23:51:52 +020073 ;
74
75# TODO:
76# Should probably return a 4xx!
Akron3b3a4342021-05-31 11:14:48 +020077$t->get_ok('/corpus/GOE/AGF/02286/p-2-0?_format=json')
Akron3c390c42020-03-30 09:06:21 +020078 ->status_is(400)
Akronb8d0b402018-10-18 23:51:52 +020079 ->json_is('/notifications/0/0', 'error')
80 ->json_is('/notifications/0/1', '730: Invalid match identifier')
81 ;
82
Akron3b3a4342021-05-31 11:14:48 +020083$t->get_ok('/corpus/notfound2/X/X/p0-1?_format=json')
Akron7093b812018-10-19 17:28:21 +020084 ->status_is(404)
Akronb8d0b402018-10-18 23:51:52 +020085 ->json_is('/notifications/0/0', 'error')
86 ->json_is('/notifications/0/1', '404: Not Found')
87 ;
88
Akron3b3a4342021-05-31 11:14:48 +020089$t->get_ok('/corpus/brokenerr/X/X/p0-1?_format=json')
Akrond0ec3082018-10-19 18:53:09 +020090 ->status_is(409)
91 ->json_is('/notifications/0/0', 'error')
92 ->json_is('/notifications/0/1', 'Message structure failed')
93 ;
94
Akron3b3a4342021-05-31 11:14:48 +020095$t->get_ok('/corpus/brokenwarn/X/X/p0-1?_format=json')
Akrond0ec3082018-10-19 18:53:09 +020096 ->status_is(200)
Akrona3c353c2019-02-14 23:50:00 +010097 ->json_is('/notifications/0/0', 'warn')
Akrond0ec3082018-10-19 18:53:09 +020098 ->json_is('/notifications/0/1', '1: Warning 1')
99 ->json_is('/notifications/1/0', 'error')
100 ->json_is('/notifications/1/1', 'Message structure failed')
101 ;
102
Akron3b3a4342021-05-31 11:14:48 +0200103$t->get_ok('/corpus/brokenerr2/X/X/p0-1?_format=json')
Akrond0ec3082018-10-19 18:53:09 +0200104 ->status_is(417)
105 ->json_is('/notifications/0/0', 'error')
106 ->json_is('/notifications/0/1', 'Message structure failed')
107 ;
108
Akronf21eb492018-10-22 15:17:56 +0200109# Get from cache
Akron3b3a4342021-05-31 11:14:48 +0200110$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=false&foundry=*&_format=json')
Akronf21eb492018-10-22 15:17:56 +0200111 ->status_is(200)
112 ->json_is('/textSigle', 'WPD15/232/39681')
113 ->json_like('/snippet', qr!<span class=\"context-left\">!)
Akron8ea84292018-10-24 13:41:52 +0200114 ->header_is('X-Kalamar-Cache', 'true')
Akronf21eb492018-10-22 15:17:56 +0200115 ;
116
Akronc4be8192018-10-25 16:07:53 +0200117# Check for validation error
Akron3b3a4342021-05-31 11:14:48 +0200118$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=no&_format=json')
Akronc4be8192018-10-25 16:07:53 +0200119 ->status_is(400)
120 ->json_is('/notifications/0/1', 'Parameter "spans" invalid')
121 ;
Akronb8d0b402018-10-18 23:51:52 +0200122
Akron3b3a4342021-05-31 11:14:48 +0200123$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=no&_format=html')
Akron69481a42021-03-22 10:31:16 +0100124 ->content_type_is('text/html;charset=UTF-8')
125 ->status_is(400)
126 ->text_is('p.no-results', 'Unable to perform the action.')
127 ->text_is('div.notify', 'Parameter "spans" invalid')
128 ;
129
130$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=no')
131 ->content_type_is('text/html;charset=UTF-8')
Akron3c390c42020-03-30 09:06:21 +0200132 ->status_is(400)
133 ->text_is('p.no-results', 'Unable to perform the action.')
134 ->text_is('div.notify', 'Parameter "spans" invalid')
135 ;
136
Akron910828a2025-06-27 15:38:48 +0200137$t->get_ok('/corpus/WPD15/232/39681/p2133-2134?spans=no')
138 ->content_type_is('text/html;charset=UTF-8')
139 ->status_is(400)
140 ->text_is('p.no-results', 'Unable to perform the action.')
141 ->text_is('div.notify', 'Parameter "spans" invalid')
142 ;
143
Akron3c390c42020-03-30 09:06:21 +0200144
Akronb80341d2018-10-15 19:46:23 +0200145done_testing;
Akron7093b812018-10-19 17:28:21 +0200146__END__