]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
config: fix NewsWWW fallback for newsgroups in HTTP URLs
[public-inbox.git] / t / plack.t
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use File::Temp qw/tempdir/;
8 my $psgi = "examples/public-inbox.psgi";
9 my $tmpdir = tempdir('pi-plack-XXXXXX', TMPDIR => 1, CLEANUP => 1);
10 my $pi_config = "$tmpdir/config";
11 my $maindir = "$tmpdir/main.git";
12 my $addr = 'test-public@example.com';
13 my $cfgpfx = "publicinbox.test";
14 my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test
15         Mail::Thread URI::Escape);
16 foreach my $mod (@mods) {
17         eval "require $mod";
18         plan skip_all => "$mod missing for plack.t" if $@;
19 }
20 use_ok 'PublicInbox::Import';
21 use_ok 'PublicInbox::Git';
22
23 foreach my $mod (@mods) { use_ok $mod; }
24 {
25         ok(-f $psgi, "psgi example file found");
26         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
27         open my $fh, '>', "$maindir/description" or die "open: $!\n";
28         print $fh "test for public-inbox\n";
29         close $fh or die "close: $!\n";
30         my %cfg = (
31                 "$cfgpfx.address" => $addr,
32                 "$cfgpfx.mainrepo" => $maindir,
33                 "$cfgpfx.url" => 'http://example.com/test/',
34                 "$cfgpfx.newsgroup" => 'inbox.test',
35         );
36         while (my ($k,$v) = each %cfg) {
37                 is(0, system(qw(git config --file), $pi_config, $k, $v),
38                         "setup $k");
39         }
40
41         # ensure successful message delivery
42         {
43                 my $mime = Email::MIME->new(<<EOF);
44 From: Me <me\@example.com>
45 To: You <you\@example.com>
46 Cc: $addr
47 Message-Id: <blah\@example.com>
48 Subject: hihi
49 Date: Thu, 01 Jan 1970 00:00:00 +0000
50
51 zzzzzz
52 EOF
53                 my $git = PublicInbox::Git->new($maindir);
54                 my $im = PublicInbox::Import->new($git, 'test', $addr);
55                 $im->add($mime);
56                 $im->done;
57                 my $rev = `git --git-dir="$maindir" rev-list HEAD`;
58                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
59         }
60         my $app = eval {
61                 local $ENV{PI_CONFIG} = $pi_config;
62                 require $psgi;
63         };
64
65         # redirect with newsgroup
66         test_psgi($app, sub {
67                 my ($cb) = @_;
68                 my $from = 'http://example.com/inbox.test';
69                 my $to = 'http://example.com/test/';
70                 my $res = $cb->(GET($from));
71                 is($res->code, 301, 'is permanent redirect');
72                 is($to, $res->header('Location'), 'redirect location matches');
73                 $from .= '/';
74                 is($res->code, 301, 'is permanent redirect');
75                 is($to, $res->header('Location'), 'redirect location matches');
76         });
77
78         # redirect with trailing /
79         test_psgi($app, sub {
80                 my ($cb) = @_;
81                 my $from = 'http://example.com/test';
82                 my $to = "$from/";
83                 my $res = $cb->(GET($from));
84                 is(301, $res->code, 'is permanent redirect');
85                 is($to, $res->header('Location'), 'redirect location matches');
86         });
87
88         my $pfx = 'http://example.com/test';
89         foreach my $t (qw(t T)) {
90                 test_psgi($app, sub {
91                         my ($cb) = @_;
92                         my $u = $pfx . "/blah%40example.com/$t";
93                         my $res = $cb->(GET($u));
94                         is(301, $res->code, "redirect for missing /");
95                         my $location = $res->header('Location');
96                         like($location, qr!/\Q$t\E/#u\z!,
97                                 'redirected with missing /');
98                 });
99         }
100         foreach my $t (qw(f)) {
101                 test_psgi($app, sub {
102                         my ($cb) = @_;
103                         my $u = $pfx . "/blah%40example.com/$t";
104                         my $res = $cb->(GET($u));
105                         is(301, $res->code, "redirect for legacy /f");
106                         my $location = $res->header('Location');
107                         like($location, qr!/blah%40example\.com/\z!,
108                                 'redirected with missing /');
109                 });
110         }
111
112         test_psgi($app, sub {
113                 my ($cb) = @_;
114                 my $atomurl = 'http://example.com/test/new.atom';
115                 my $res = $cb->(GET('http://example.com/test/'));
116                 is(200, $res->code, 'success response received');
117                 like($res->content, qr!href="\Q$atomurl\E"!,
118                         'atom URL generated');
119                 like($res->content, qr!href="blah%40example\.com/"!,
120                         'index generated');
121         });
122
123         test_psgi($app, sub {
124                 my ($cb) = @_;
125                 my $res = $cb->(GET($pfx . '/atom.xml'));
126                 is(200, $res->code, 'success response received for atom');
127                 like($res->content,
128                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
129                         'atom feed generated correct URL');
130         });
131
132         test_psgi($app, sub {
133                 my ($cb) = @_;
134                 my $path = '/blah%40example.com/';
135                 my $res = $cb->(GET($pfx . $path));
136                 is(200, $res->code, "success for $path");
137                 like($res->content, qr!<title>hihi - Me</title>!,
138                         "HTML returned");
139
140                 $path .= 'f/';
141                 $res = $cb->(GET($pfx . $path));
142                 is(301, $res->code, "redirect for $path");
143                 my $location = $res->header('Location');
144                 like($location, qr!/blah%40example\.com/\z!,
145                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
146         });
147
148         test_psgi($app, sub {
149                 my ($cb) = @_;
150                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
151                 is(200, $res->code, 'success response received for /*/raw');
152                 like($res->content, qr!^From !sm, "mbox returned");
153         });
154
155         # legacy redirects
156         foreach my $t (qw(m f)) {
157                 test_psgi($app, sub {
158                         my ($cb) = @_;
159                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
160                         is(301, $res->code, "redirect for old $t .txt link");
161                         my $location = $res->header('Location');
162                         like($location, qr!/blah%40example\.com/raw\z!,
163                                 ".txt redirected to /raw");
164                 });
165         }
166
167         my %umap = (
168                 'm' => '',
169                 'f' => '',
170                 't' => 't/',
171         );
172         while (my ($t, $e) = each %umap) {
173                 test_psgi($app, sub {
174                         my ($cb) = @_;
175                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
176                         is(301, $res->code, "redirect for old $t .html link");
177                         my $location = $res->header('Location');
178                         like($location,
179                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
180                                 ".html redirected to new location");
181                 });
182         }
183         foreach my $sfx (qw(mbox mbox.gz)) {
184                 test_psgi($app, sub {
185                         my ($cb) = @_;
186                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
187                         is(301, $res->code, 'redirect for old thread link');
188                         my $location = $res->header('Location');
189                         like($location,
190                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
191                              "$sfx redirected to /mbox.gz");
192                 });
193         }
194 }
195
196 done_testing();