]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
smsg: handle wide characters in raw mail headers
[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
32 # n.b. these headers are not properly RFC2047-encoded
33 my $mime = PublicInbox::Eml->new(<<EOF);
34 Subject: test Ævar
35 Message-ID: <$mid>
36 From: Ævar Arnfjörð Bjarmason <avarab\@example>
37 To: git\@vger.kernel.org
38
39 EOF
40 $im->add($mime);
41
42 $mime = PublicInbox::Eml->new(<<'EOF');
43 Subject:
44 Message-ID: <blank-subject@example.com>
45 From: blank subject <blank-subject@example.com>
46 To: git@vger.kernel.org
47
48 EOF
49 $im->add($mime);
50
51 $mime = PublicInbox::Eml->new(<<'EOF');
52 Message-ID: <no-subject-at-all@example.com>
53 From: no subject at all <no-subject-at-all@example.com>
54 To: git@vger.kernel.org
55
56 EOF
57 $im->add($mime);
58
59 $im->done;
60 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
61
62 my $cfgpfx = "publicinbox.test";
63 my $config = PublicInbox::Config->new(\<<EOF);
64 $cfgpfx.address=git\@vger.kernel.org
65 $cfgpfx.inboxdir=$tmpdir
66 EOF
67 my $www = PublicInbox::WWW->new($config);
68 test_psgi(sub { $www->call(@_) }, sub {
69         my ($cb) = @_;
70         my $res;
71         $res = $cb->(GET('/test/?q=%C3%86var'));
72         my $html = $res->content;
73         like($html, qr/<title>&#198;var - /, 'HTML escaped in title');
74         my @res = ($html =~ m/\?q=(.+var)\b/g);
75         ok(scalar(@res), 'saw query strings');
76         my %uniq = map { $_ => 1 } @res;
77         is(1, scalar keys %uniq, 'all query values identical in HTML');
78         is('%C3%86var', (keys %uniq)[0], 'matches original query');
79         ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason') >= 0,
80                 "displayed Ævar's name properly in HTML");
81
82         my $warn = [];
83         local $SIG{__WARN__} = sub { push @$warn, @_ };
84         $res = $cb->(GET('/test/?q=s:test&l=5e'));
85         is($res->code, 200, 'successful search result');
86         is_deeply([], $warn, 'no warnings from non-numeric comparison');
87
88         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
89         is($res->code, 404, 'failed search result gives 404');
90         is_deeply([], $warn, 'no warnings');
91
92         my $mid_re = qr/\Q$mid\E/o;
93         while (length($digits) > 8) {
94                 $res = $cb->(GET("/test/$ua.$digits/"));
95                 is($res->code, 300, 'partial match found while truncated');
96                 like($res->content, qr/\b1 partial match found\b/);
97                 like($res->content, $mid_re, 'found mid in response');
98                 chop($digits);
99         }
100
101         $res = $cb->(GET('/test/'));
102         $html = $res->content;
103         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
104                 'subject-less message linked from "/$INBOX/"');
105         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
106                 'blank subject message linked from "/$INBOX/"');
107         like($html, qr/test &#198;var/,
108                 "displayed Ævar's name properly in topic view");
109
110         $res = $cb->(GET('/test/?q=tc:git'));
111         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
112                 'subject-less message linked from "/$INBOX/?q=..."');
113         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
114                 'blank subject message linked from "/$INBOX/?q=..."');
115         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
116         like($res->header('Content-Disposition'),
117                 qr/filename=no-subject\.txt/);
118         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
119         like($res->header('Content-Disposition'),
120                 qr/filename=no-subject\.mbox\.gz/);
121 });
122
123 done_testing();
124
125 1;