]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/psgi_v2.t
No ext_urls
[public-inbox.git] / t / psgi_v2.t
index 487317b6134046d3a98b80a6caa3f5cf5bb38e8f..5b197a9f65b9d2d14d8e659bbb392f19dd7425c5 100644 (file)
@@ -1,5 +1,5 @@
 #!perl -w
-# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use v5.10.1;
@@ -9,10 +9,40 @@ use PublicInbox::Eml;
 use PublicInbox::Config;
 use PublicInbox::MID qw(mids);
 require_mods(qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
-               URI::Escape Plack::Builder));
+               URI::Escape Plack::Builder HTTP::Date));
 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
 use_ok 'PublicInbox::WWW';
 my ($tmpdir, $for_destroy) = tmpdir();
+my $enc_dup = 'ref-20150309094050.GO3427@x1.example';
+
+my $dibx = create_inbox 'v2-dup', version => 2, indexlevel => 'medium',
+                       tmpdir => "$tmpdir/dup", sub {
+       my ($im, $ibx) = @_;
+       my $common = <<"";
+Date: Mon, 9 Mar 2015 09:40:50 +0000
+From: x\@example.com
+To: y\@example.com
+Subject: re
+Message-ID: <$enc_dup>
+MIME-Version: 1.0
+
+       $im->add(PublicInbox::Eml->new($common.<<EOM)) or BAIL_OUT;
+Content-Type: text/plain; charset=utf-8
+Content-Disposition: inline
+Content-Transfer-Encoding: 8bit
+
+cr_mismatch
+pipe \x{e2}\x{94}\x{82} or not
+EOM
+       $im->add(PublicInbox::Eml->new($common.<<EOM)) or BAIL_OUT;
+Content-Type: text/plain; charset="windows-1252"
+Content-Transfer-Encoding: quoted-printable
+
+cr_mismatch\r
+pipe =E2=94=82 or not
+EOM
+};
+
 my $eml = PublicInbox::Eml->new(<<'EOF');
 From oldbug-pre-a0c07cba0e5d8b6a Fri Oct  2 00:00:00 1993
 From: a@example.com
@@ -20,11 +50,12 @@ To: test@example.com
 Subject: this is a subject
 Message-ID: <a-mid@b>
 Date: Fri, 02 Oct 1993 00:00:00 +0000
+Content-Type: text/plain; charset=iso-8859-1
 
 hello world
 EOF
 my $new_mid;
-my $ibx = create_inbox 'v2', version => 2, indexlevel => 'medium',
+my $ibx = create_inbox 'v2-1', version => 2, indexlevel => 'medium',
                        tmpdir => "$tmpdir/v2", sub {
        my ($im, $ibx) = @_;
        $im->add($eml) or BAIL_OUT;
@@ -52,31 +83,13 @@ my $cfgpath = "$ibx->{inboxdir}/pi_config";
 [publicinbox "v2test"]
        inboxdir = $ibx->{inboxdir}
        address = $ibx->{-primary_address}
+[publicinbox "dup"]
+       inboxdir = $dibx->{inboxdir}
+       address = $dibx->{-primary_address}
 EOF
        close $fh or BAIL_OUT;
 }
 
-my $run_httpd = sub {
-       my ($client, $skip) = @_;
-       SKIP: {
-               require_mods(qw(Plack::Test::ExternalServer), $skip);
-               my $env = { PI_CONFIG => $cfgpath };
-               my $sock = tcp_server() or die;
-               my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
-               my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
-               my $td = start_script($cmd, $env, { 3 => $sock });
-               my ($h, $p) = tcp_host_port($sock);
-               local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
-               Plack::Test::ExternalServer::test_psgi(client => $client);
-               $td->join('TERM');
-               open my $fh, '<', $err or BAIL_OUT $!;
-               my $e = do { local $/; <$fh> };
-               if ($e =~ s/^Plack::Middleware::ReverseProxy missing,\n//gms) {
-                       $e =~ s/^URL generation for redirects .*\n//gms;
-               }
-               is($e, '', 'no errors');
-       }
-};
 my $msg = $ibx->msg_by_mid('a-mid@b');
 like($$msg, qr/\AFrom oldbug/s,
        '"From_" line stored to test old bug workaround');
@@ -89,6 +102,8 @@ my $client0 = sub {
        like($res->content, qr!\$INBOX_DIR/description missing!,
                'got v2 description missing message');
        $res = $cb->(GET('/v2test/a-mid@b/raw'));
+       is($res->header('Content-Type'), 'text/plain; charset=iso-8859-1',
+               'charset from message used');
        $raw = $res->content;
        unlike($raw, qr/^From oldbug/sm, 'buggy "From_" line omitted');
        like($raw, qr/^hello world$/m, 'got first message');
@@ -113,9 +128,15 @@ my $client0 = sub {
        @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
        is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
                'new.html ordering is chronological');
+
+       $res = $cb->(GET('/v2test/new.atom'));
+       my @dates = ($res->content =~ m!title><updated>([^<]+)</updated>!g);
+       is_deeply(\@dates, [ "1993-10-02T00:01:00Z", "1993-10-02T00:00:00Z" ],
+               'Date headers made it through');
 };
 test_psgi(sub { $www->call(@_) }, $client0);
-$run_httpd->($client0, 9);
+my $env = { TMPDIR => $tmpdir, PI_CONFIG => $cfgpath };
+test_httpd($env, $client0, 9);
 
 $eml->header_set('Message-ID', 'a-mid@b');
 $eml->body_set("hello ghosts\n");
@@ -133,6 +154,14 @@ $im->done;
 
 my $client1 = sub {
        my ($cb) = @_;
+       $res = $cb->(GET('/v2test/_/text/config/raw'));
+       my $lm = $res->header('Last-Modified');
+       ok($lm, 'Last-Modified set w/ ->mm');
+       $lm = HTTP::Date::str2time($lm);
+       is($lm, $ibx->mm->created_at,
+               'Last-Modified for text/config/raw matches ->created_at');
+       delete $ibx->{mm};
+
        $res = $cb->(GET("/v2test/$third/raw"));
        $raw = $res->content;
        like($raw, qr/^hello ghosts$/m, 'got third message');
@@ -213,6 +242,8 @@ my $client1 = sub {
        local $SIG{__WARN__} = 'DEFAULT';
        $res = $cb->(GET('/v2test/a-mid@b/'));
        $raw = $res->content;
+       like($raw, qr/WARNING: multiple messages have this Message-ID/,
+               'warned about duplicate Message-IDs');
        like($raw, qr/^hello world$/m, 'got first message');
        like($raw, qr/^hello world!$/m, 'got second message');
        like($raw, qr/^hello ghosts$/m, 'got third message');
@@ -222,10 +253,18 @@ my $client1 = sub {
                like($raw, qr!>\Q$mid\E</a>!s, "Message-ID $mid shown");
        }
        like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
+
+       $res = $cb->(GET("/dup/$enc_dup/d/"));
+       is($res->code, 200, '/d/ (diff) endpoint works');
+       $raw = $res->content;
+       like($raw, qr!</span> cr_mismatch\n!s,
+               'cr_mismatch is only diff context');
+       like($raw, qr!>\-pipe !s, 'pipe diff del line');
+       like($raw, qr!>\+pipe !s, 'pipe diff ins line');
 };
 
 test_psgi(sub { $www->call(@_) }, $client1);
-$run_httpd->($client1, 38);
+test_httpd($env, $client1, 38);
 
 {
        my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
@@ -267,7 +306,7 @@ my $client2 = sub {
 };
 
 test_psgi(sub { $www->call(@_) }, $client2);
-$run_httpd->($client2, 8);
+test_httpd($env, $client2, 8);
 {
        # ensure conflicted attachments can be resolved
        local $SIG{__WARN__} = sub {};
@@ -298,6 +337,6 @@ my $client3 = sub {
        is_deeply(\@warn, [], 'no warnings on YYYYMMDD only');
 };
 test_psgi(sub { $www->call(@_) }, $client3);
-$run_httpd->($client3, 4);
+test_httpd($env, $client3, 4);
 
 done_testing;