]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
www: $MESSAGE_ID/raw endpoint supports "duplicates"
[public-inbox.git] / t / psgi_v2.t
1 # Copyright (C) 2018 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 File::Temp qw/tempdir/;
7 use PublicInbox::MIME;
8 use PublicInbox::Config;
9 use PublicInbox::WWW;
10 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
11                 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
15 }
16 use_ok($_) for @mods;
17 use_ok 'PublicInbox::V2Writable';
18 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $ibx = {
20         mainrepo => $mainrepo,
21         name => 'test-v2writable',
22         version => 2,
23         -primary_address => 'test@example.com',
24 };
25 $ibx = PublicInbox::Inbox->new($ibx);
26 my $new_mid;
27
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 $im->{parallel} = 0;
30
31 my $mime = PublicInbox::MIME->create(
32         header => [
33                 From => 'a@example.com',
34                 To => 'test@example.com',
35                 Subject => 'this is a subject',
36                 'Message-ID' => '<a-mid@b>',
37                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
38         ],
39         body => "hello world\n",
40 );
41 ok($im->add($mime), 'added one message');
42 $mime->body_set("hello world!\n");
43
44 my @warn;
45 local $SIG{__WARN__} = sub { push @warn, @_ };
46 ok($im->add($mime), 'added duplicate-but-different message');
47 is(scalar(@warn), 1, 'got one warning');
48 my @mids = $mime->header_obj->header_raw('Message-Id');
49 $new_mid = PublicInbox::MID::mid_clean($mids[0]);
50 $im->done;
51
52 my $cfgpfx = "publicinbox.v2test";
53 my %cfg = (
54         "$cfgpfx.address" => $ibx->{-primary_address},
55         "$cfgpfx.mainrepo" => $mainrepo,
56 );
57
58 my $config = PublicInbox::Config->new({ %cfg });
59 my $www = PublicInbox::WWW->new($config);
60 my ($res, $raw, @from_);
61 test_psgi(sub { $www->call(@_) }, sub {
62         my ($cb) = @_;
63         $res = $cb->(GET('/v2test/a-mid@b/raw'));
64         $raw = $res->content;
65         like($raw, qr/^hello world$/m, 'got first message');
66         like($raw, qr/^hello world!$/m, 'got second message');
67         @from_ = ($raw =~ m/^From /mg);
68         is(scalar(@from_), 2, 'two From_ lines');
69
70         $res = $cb->(GET("/v2test/$new_mid/raw"));
71         $raw = $res->content;
72         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
73         @from_ = ($raw =~ m/^From /mg);
74         is(scalar(@from_), 1, 'only one From_ line');
75 });
76
77 $mime->header_set('Message-Id', 'a-mid@b');
78 $mime->body_set("hello ghosts\n");
79 ok($im->add($mime), 'added 3rd duplicate-but-different message');
80 is(scalar(@warn), 2, 'got another warning');
81 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
82 is($warn[0], $warn[1], 'both warnings are the same');
83
84 @mids = $mime->header_obj->header_raw('Message-Id');
85 my $third = PublicInbox::MID::mid_clean($mids[0]);
86 $im->done;
87
88 # need to reload...
89 $config = PublicInbox::Config->new({ %cfg });
90 $www = PublicInbox::WWW->new($config);
91 test_psgi(sub { $www->call(@_) }, sub {
92         my ($cb) = @_;
93         $res = $cb->(GET("/v2test/$third/raw"));
94         $raw = $res->content;
95         like($raw, qr/^hello ghosts$/m, 'got third message');
96         @from_ = ($raw =~ m/^From /mg);
97         is(scalar(@from_), 1, 'one From_ line');
98
99         $res = $cb->(GET('/v2test/a-mid@b/raw'));
100         $raw = $res->content;
101         like($raw, qr/^hello world$/m, 'got first message');
102         like($raw, qr/^hello world!$/m, 'got second message');
103         like($raw, qr/^hello ghosts$/m, 'got third message');
104         @from_ = ($raw =~ m/^From /mg);
105         is(scalar(@from_), 3, 'three From_ lines');
106 });
107
108 done_testing();
109
110 1;