1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 foreach my $mod (qw(DBD::SQLite Data::Dumper)) {
9 plan skip_all => "$mod missing for nntp.t" if $@;
12 use_ok 'PublicInbox::NNTP';
13 use_ok 'PublicInbox::Inbox';
17 my (undef, $s) = split(/ = /, Data::Dumper::Dumper($_[0]), 2);
25 PublicInbox::NNTP::wildmat2re($_[0]);
26 my $new = quote_str($_[0]);
32 my ($orig, $new) = wm_prepare($wm);
33 like($str, $wm, "$orig matches '$str' using $new");
37 my ($str, $wm, $check_ex) = @_;
41 like($str, $re, "normal re with $wm matches, but ...");
43 my ($orig, $new) = wm_prepare($wm);
44 unlike($str, $wm, "$orig does not match '$str' using $new");
47 wildmat_like('[foo]', '[\[foo\]]');
48 wildmat_like('any', '*');
49 wildmat_unlike('bar.foo.bar', 'foo.*');
52 wildmat_unlike('HI', '(?{"HI"})', 1);
53 wildmat_unlike('HI', '[(?{"HI"})]', 1);
60 PublicInbox::NNTP::ngpat2re($pat);
61 like($str, $pat, "'$orig' matches '$str' using $pat");
64 ngpat_like('any', '*');
65 ngpat_like('a.s.r', 'a.t,a.s.r');
66 ngpat_like('a.s.r', 'a.t,a.s.*');
70 use POSIX qw(strftime);
72 my ($date, $time, $gmt) = @_;
73 my $m = join(' ', @_);
74 my $ts = PublicInbox::NNTP::parse_time(@_);
77 if (length($date) == 8) {
78 ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
80 ($d, $t) = split(' ', strftime('%g%m%d %H%M%S', @t));
82 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
85 my $x1 = time_roundtrip(qw(20141109 060606 GMT));
86 my $x2 = time_roundtrip(qw(141109 060606 GMT));
87 my $x3 = time_roundtrip(qw(930724 060606 GMT));
90 skip('YYMMDD test needs updating', 2) if (time > 0x7fffffff);
91 # our world probably ends in 2038, but if not we'll try to
92 # remember to update the test then
93 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
94 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
98 { # test setting NNTP headers in HEAD and ARTICLE requests
100 my $u = 'https://example.com/a/';
101 my $ng = PublicInbox::Inbox->new({ name => 'test',
102 inboxdir => 'test.git',
103 address => 'a@example.com',
104 -primary_address => 'a@example.com',
106 domain => 'example.com',
107 url => '//example.com/a'});
108 is($ng->base_url, $u, 'URL expanded');
110 my $mime = Email::MIME->new("Message-ID: <$mid>\r\n\r\n");
111 my $hdr = $mime->header_obj;
112 my $mock_self = { nntpd => { grouplist => [],
113 servername => 'example.com' } };
114 PublicInbox::NNTP::set_nntp_headers($mock_self, $hdr, $ng, 1, $mid);
115 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
116 'Message-ID unchanged');
117 is_deeply([ $mime->header('Archived-At') ], [ "<${u}a\@b/>" ],
119 is_deeply([ $mime->header('List-Archive') ], [ "<$u>" ],
120 'List-Archive: set');
121 is_deeply([ $mime->header('List-Post') ], [ '<mailto:a@example.com>' ],
123 is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
125 is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
128 $ng->{-base_url} = 'http://mirror.example.com/m/';
129 PublicInbox::NNTP::set_nntp_headers($mock_self, $hdr, $ng, 2, $mid);
130 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
131 'Message-ID unchanged');
132 is_deeply([ $mime->header('Archived-At') ],
133 [ "<${u}a\@b/>", '<http://mirror.example.com/m/a@b/>' ],
134 'Archived-At: appended');
135 is_deeply([ $mime->header('Xref') ], [ 'example.com test:2' ],
136 'Old Xref: clobbered');