]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
2d12ba6a2c6a886e7db9019024a2dd13a47c26d5
[public-inbox.git] / t / psgi_search.t
1 # Copyright (C) 2017-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Eml;
7 use PublicInbox::Config;
8 use PublicInbox::Inbox;
9 use PublicInbox::InboxWritable;
10 use bytes (); # only for bytes::length
11 use PublicInbox::TestCommon;
12 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
13                 URI::Escape Plack::Builder);
14 require_mods(@mods);
15 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
16 use_ok 'PublicInbox::WWW';
17 use_ok 'PublicInbox::SearchIdx';
18 my ($tmpdir, $for_destroy) = tmpdir();
19
20 my $ibx = PublicInbox::Inbox->new({
21         inboxdir => $tmpdir,
22         address => 'git@vger.kernel.org',
23         name => 'test',
24 });
25 $ibx = PublicInbox::InboxWritable->new($ibx);
26 $ibx->init_inbox(1);
27 my $im = $ibx->importer(0);
28 my $digits = '10010260936330';
29 my $ua = 'Pine.LNX.4.10';
30 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
31 my $mime = PublicInbox::Eml->new(<<EOF);
32 Subject: test
33 Message-ID: <$mid>
34 From: Ævar Arnfjörð Bjarmason <avarab\@example>
35 To: git\@vger.kernel.org
36
37 EOF
38 $im->add($mime);
39
40 $mime = PublicInbox::Eml->new(<<'EOF');
41 Subject:
42 Message-ID: <blank-subject@example.com>
43 From: blank subject <blank-subject@example.com>
44 To: git@vger.kernel.org
45
46 EOF
47 $im->add($mime);
48
49 $mime = PublicInbox::Eml->new(<<'EOF');
50 Message-ID: <no-subject-at-all@example.com>
51 From: no subject at all <no-subject-at-all@example.com>
52 To: git@vger.kernel.org
53
54 EOF
55 $im->add($mime);
56
57 $im->done;
58 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
59
60 my $cfgpfx = "publicinbox.test";
61 my $config = PublicInbox::Config->new(\<<EOF);
62 $cfgpfx.address=git\@vger.kernel.org
63 $cfgpfx.inboxdir=$tmpdir
64 EOF
65 my $www = PublicInbox::WWW->new($config);
66 test_psgi(sub { $www->call(@_) }, sub {
67         my ($cb) = @_;
68         my $res;
69         $res = $cb->(GET('/test/?q=%C3%86var'));
70         my $html = $res->content;
71         like($html, qr/<title>&#198;var - /, 'HTML escaped in title');
72         my @res = ($html =~ m/\?q=(.+var)\b/g);
73         ok(scalar(@res), 'saw query strings');
74         my %uniq = map { $_ => 1 } @res;
75         is(1, scalar keys %uniq, 'all query values identical in HTML');
76         is('%C3%86var', (keys %uniq)[0], 'matches original query');
77         ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason') >= 0,
78                 "displayed Ævar's name properly in HTML");
79
80         my $warn = [];
81         local $SIG{__WARN__} = sub { push @$warn, @_ };
82         $res = $cb->(GET('/test/?q=s:test&l=5e'));
83         is($res->code, 200, 'successful search result');
84         is_deeply([], $warn, 'no warnings from non-numeric comparison');
85
86         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
87         is($res->code, 404, 'failed search result gives 404');
88         is_deeply([], $warn, 'no warnings');
89
90         my $mid_re = qr/\Q$mid\E/o;
91         while (length($digits) > 8) {
92                 $res = $cb->(GET("/test/$ua.$digits/"));
93                 is($res->code, 300, 'partial match found while truncated');
94                 like($res->content, qr/\b1 partial match found\b/);
95                 like($res->content, $mid_re, 'found mid in response');
96                 chop($digits);
97         }
98
99         $res = $cb->(GET('/test/'));
100         $html = $res->content;
101         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
102                 'subject-less message linked from "/$INBOX/"');
103         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
104                 'blank subject message linked from "/$INBOX/"');
105
106         $res = $cb->(GET('/test/?q=tc:git'));
107         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
108                 'subject-less message linked from "/$INBOX/?q=..."');
109         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
110                 'blank subject message linked from "/$INBOX/?q=..."');
111         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
112         like($res->header('Content-Disposition'),
113                 qr/filename=no-subject\.txt/);
114         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
115         like($res->header('Content-Disposition'),
116                 qr/filename=no-subject\.mbox\.gz/);
117 });
118
119 done_testing();
120
121 1;