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>
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Data::Dumper));
8 use_ok 'PublicInbox::NNTP';
9 use_ok 'PublicInbox::Inbox';
13 my (undef, $s) = split(/ = /, Data::Dumper::Dumper($_[0]), 2);
21 PublicInbox::NNTP::wildmat2re($_[0]);
22 my $new = quote_str($_[0]);
28 my ($orig, $new) = wm_prepare($wm);
29 like($str, $wm, "$orig matches '$str' using $new");
33 my ($str, $wm, $check_ex) = @_;
37 like($str, $re, "normal re with $wm matches, but ...");
39 my ($orig, $new) = wm_prepare($wm);
40 unlike($str, $wm, "$orig does not match '$str' using $new");
43 wildmat_like('[foo]', '[\[foo\]]');
44 wildmat_like('any', '*');
45 wildmat_unlike('bar.foo.bar', 'foo.*');
48 wildmat_unlike('HI', '(?{"HI"})', 1);
49 wildmat_unlike('HI', '[(?{"HI"})]', 1);
56 PublicInbox::NNTP::ngpat2re($pat);
57 like($str, $pat, "'$orig' matches '$str' using $pat");
60 ngpat_like('any', '*');
61 ngpat_like('a.s.r', 'a.t,a.s.r');
62 ngpat_like('a.s.r', 'a.t,a.s.*');
66 use POSIX qw(strftime);
68 my ($date, $time, $gmt) = @_;
69 my $m = join(' ', @_);
70 my $ts = PublicInbox::NNTP::parse_time(@_);
73 if (length($date) == 8) {
74 ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
76 ($d, $t) = split(' ', strftime('%g%m%d %H%M%S', @t));
78 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
81 my $x1 = time_roundtrip(qw(20141109 060606 GMT));
82 my $x2 = time_roundtrip(qw(141109 060606 GMT));
83 my $x3 = time_roundtrip(qw(930724 060606 GMT));
86 skip('YYMMDD test needs updating', 2) if (time > 0x7fffffff);
87 # our world probably ends in 2038, but if not we'll try to
88 # remember to update the test then
89 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
90 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
94 { # test setting NNTP headers in HEAD and ARTICLE requests
96 my $u = 'https://example.com/a/';
97 my $ng = PublicInbox::Inbox->new({ name => 'test',
98 inboxdir => 'test.git',
99 address => 'a@example.com',
100 -primary_address => 'a@example.com',
102 domain => 'example.com',
103 url => '//example.com/a'});
104 is($ng->base_url, $u, 'URL expanded');
106 my $mime = Email::MIME->new("Message-ID: <$mid>\r\n\r\n");
107 my $hdr = $mime->header_obj;
108 my $mock_self = { nntpd => { grouplist => [],
109 servername => 'example.com' } };
110 PublicInbox::NNTP::set_nntp_headers($mock_self, $hdr, $ng, 1, $mid);
111 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
112 'Message-ID unchanged');
113 is_deeply([ $mime->header('Archived-At') ], [ "<${u}a\@b/>" ],
115 is_deeply([ $mime->header('List-Archive') ], [ "<$u>" ],
116 'List-Archive: set');
117 is_deeply([ $mime->header('List-Post') ], [ '<mailto:a@example.com>' ],
119 is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
121 is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
124 $ng->{-base_url} = 'http://mirror.example.com/m/';
125 PublicInbox::NNTP::set_nntp_headers($mock_self, $hdr, $ng, 2, $mid);
126 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
127 'Message-ID unchanged');
128 is_deeply([ $mime->header('Archived-At') ],
129 [ "<${u}a\@b/>", '<http://mirror.example.com/m/a@b/>' ],
130 'Archived-At: appended');
131 is_deeply([ $mime->header('Xref') ], [ 'example.com test:2' ],
132 'Old Xref: clobbered');