1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
8 foreach my $mod (qw(DBD::SQLite Search::Xapian Danga::Socket)) {
10 plan skip_all => "$mod missing for nntp.t" if $@;
13 use_ok 'PublicInbox::NNTP';
14 use_ok 'PublicInbox::Inbox';
18 my (undef, $s) = split(/ = /, Dumper($_[0]), 2);
26 PublicInbox::NNTP::wildmat2re($_[0]);
27 my $new = quote_str($_[0]);
33 my ($orig, $new) = wm_prepare($wm);
34 like($str, $wm, "$orig matches '$str' using $new");
38 my ($str, $wm, $check_ex) = @_;
42 like($str, $re, "normal re with $wm matches, but ...");
44 my ($orig, $new) = wm_prepare($wm);
45 unlike($str, $wm, "$orig does not match '$str' using $new");
48 wildmat_like('[foo]', '[\[foo\]]');
49 wildmat_like('any', '*');
50 wildmat_unlike('bar.foo.bar', 'foo.*');
53 wildmat_unlike('HI', '(?{"HI"})', 1);
54 wildmat_unlike('HI', '[(?{"HI"})]', 1);
61 PublicInbox::NNTP::ngpat2re($pat);
62 like($str, $pat, "'$orig' matches '$str' using $pat");
65 ngpat_like('any', '*');
66 ngpat_like('a.s.r', 'a.t,a.s.r');
67 ngpat_like('a.s.r', 'a.t,a.s.*');
71 use POSIX qw(strftime);
73 my ($date, $time, $gmt) = @_;
74 my $m = join(' ', @_);
75 my $ts = PublicInbox::NNTP::parse_time(@_);
78 if (length($date) == 8) {
79 ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
81 ($d, $t) = split(' ', strftime('%g%m%d %H%M%S', @t));
83 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
86 my $x1 = time_roundtrip(qw(20141109 060606 GMT));
87 my $x2 = time_roundtrip(qw(141109 060606 GMT));
88 my $x3 = time_roundtrip(qw(930724 060606 GMT));
91 skip('YYMMDD test needs updating', 2) if (time > 0x7fffffff);
92 # our world probably ends in 2038, but if not we'll try to
93 # remember to update the test then
94 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
95 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
99 { # test setting NNTP headers in HEAD and ARTICLE requests
101 my $u = 'https://example.com/a/';
102 my $ng = PublicInbox::Inbox->new({ name => 'test',
103 mainrepo => 'test.git',
104 address => 'a@example.com',
105 -primary_address => 'a@example.com',
107 domain => 'example.com',
108 url => '//example.com/a'});
109 is($ng->base_url, $u, 'URL expanded');
111 my $mime = Email::MIME->new("Message-ID: <$mid>\r\n\r\n");
112 PublicInbox::NNTP::set_nntp_headers($mime->header_obj, $ng, 1, $mid);
113 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
114 'Message-ID unchanged');
115 is_deeply([ $mime->header('Archived-At') ], [ "<${u}a\@b/>" ],
117 is_deeply([ $mime->header('List-Archive') ], [ "<$u>" ],
118 'List-Archive: set');
119 is_deeply([ $mime->header('List-Post') ], [ '<mailto:a@example.com>' ],
121 is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
123 is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
126 $ng->{-base_url} = 'http://mirror.example.com/m/';
127 PublicInbox::NNTP::set_nntp_headers($mime->header_obj, $ng, 2, $mid);
128 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
129 'Message-ID unchanged');
130 is_deeply([ $mime->header('Archived-At') ],
131 [ "<${u}a\@b/>", '<http://mirror.example.com/m/a@b/>' ],
132 'Archived-At: appended');
133 is_deeply([ $mime->header('Xref') ], [ 'example.com test:2' ],
134 'Old Xref: clobbered');