]> Sergey Matveev's repositories - public-inbox.git/blob - t/plack.t
www: implement hybrid flat+thread conversation view
[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         test_psgi($app, sub {
66                 my ($cb) = @_;
67                 foreach my $u (qw(robots.txt favicon.ico .well-known/foo)) {
68                         my $res = $cb->(GET("http://example.com/$u"));
69                         is($res->code, 404, "$u is missing");
70                 }
71         });
72
73         # redirect with newsgroup
74         test_psgi($app, sub {
75                 my ($cb) = @_;
76                 my $from = 'http://example.com/inbox.test';
77                 my $to = 'http://example.com/test/';
78                 my $res = $cb->(GET($from));
79                 is($res->code, 301, 'newsgroup name is permanent redirect');
80                 is($to, $res->header('Location'), 'redirect location matches');
81                 $from .= '/';
82                 is($res->code, 301, 'newsgroup name/ is permanent redirect');
83                 is($to, $res->header('Location'), 'redirect location matches');
84         });
85
86         # redirect with trailing /
87         test_psgi($app, sub {
88                 my ($cb) = @_;
89                 my $from = 'http://example.com/test';
90                 my $to = "$from/";
91                 my $res = $cb->(GET($from));
92                 is(301, $res->code, 'is permanent redirect');
93                 is($to, $res->header('Location'), 'redirect location matches');
94         });
95
96         my $pfx = 'http://example.com/test';
97         foreach my $t (qw(t T)) {
98                 test_psgi($app, sub {
99                         my ($cb) = @_;
100                         my $u = $pfx . "/blah%40example.com/$t";
101                         my $res = $cb->(GET($u));
102                         is(301, $res->code, "redirect for missing /");
103                         my $location = $res->header('Location');
104                         like($location, qr!/t/#u\z!,
105                                 'redirected with missing /');
106                 });
107         }
108         foreach my $t (qw(f)) {
109                 test_psgi($app, sub {
110                         my ($cb) = @_;
111                         my $u = $pfx . "/blah%40example.com/$t";
112                         my $res = $cb->(GET($u));
113                         is(301, $res->code, "redirect for legacy /f");
114                         my $location = $res->header('Location');
115                         like($location, qr!/blah%40example\.com/\z!,
116                                 'redirected with missing /');
117                 });
118         }
119
120         test_psgi($app, sub {
121                 my ($cb) = @_;
122                 my $atomurl = 'http://example.com/test/new.atom';
123                 my $res = $cb->(GET('http://example.com/test/'));
124                 is(200, $res->code, 'success response received');
125                 like($res->content, qr!href="\Q$atomurl\E"!,
126                         'atom URL generated');
127                 like($res->content, qr!href="blah%40example\.com/"!,
128                         'index generated');
129         });
130
131         test_psgi($app, sub {
132                 my ($cb) = @_;
133                 my $res = $cb->(GET($pfx . '/atom.xml'));
134                 is(200, $res->code, 'success response received for atom');
135                 like($res->content,
136                         qr!link\s+href="\Q$pfx\E/blah%40example\.com/"!s,
137                         'atom feed generated correct URL');
138         });
139
140         test_psgi($app, sub {
141                 my ($cb) = @_;
142                 my $path = '/blah%40example.com/';
143                 my $res = $cb->(GET($pfx . $path));
144                 is(200, $res->code, "success for $path");
145                 like($res->content, qr!<title>hihi - Me</title>!,
146                         "HTML returned");
147
148                 $path .= 'f/';
149                 $res = $cb->(GET($pfx . $path));
150                 is(301, $res->code, "redirect for $path");
151                 my $location = $res->header('Location');
152                 like($location, qr!/blah%40example\.com/\z!,
153                         '/$MESSAGE_ID/f/ redirected to /$MESSAGE_ID/');
154         });
155
156         test_psgi($app, sub {
157                 my ($cb) = @_;
158                 my $res = $cb->(GET($pfx . '/blah%40example.com/raw'));
159                 is(200, $res->code, 'success response received for /*/raw');
160                 like($res->content, qr!^From !sm, "mbox returned");
161         });
162
163         # legacy redirects
164         foreach my $t (qw(m f)) {
165                 test_psgi($app, sub {
166                         my ($cb) = @_;
167                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.txt"));
168                         is(301, $res->code, "redirect for old $t .txt link");
169                         my $location = $res->header('Location');
170                         like($location, qr!/blah%40example\.com/raw\z!,
171                                 ".txt redirected to /raw");
172                 });
173         }
174
175         my %umap = (
176                 'm' => '',
177                 'f' => '',
178                 't' => 't/',
179         );
180         while (my ($t, $e) = each %umap) {
181                 test_psgi($app, sub {
182                         my ($cb) = @_;
183                         my $res = $cb->(GET($pfx . "/$t/blah%40example.com.html"));
184                         is(301, $res->code, "redirect for old $t .html link");
185                         my $location = $res->header('Location');
186                         like($location,
187                                 qr!/blah%40example\.com/$e(?:#u)?\z!,
188                                 ".html redirected to new location");
189                 });
190         }
191         foreach my $sfx (qw(mbox mbox.gz)) {
192                 test_psgi($app, sub {
193                         my ($cb) = @_;
194                         my $res = $cb->(GET($pfx . "/t/blah%40example.com.$sfx"));
195                         is(301, $res->code, 'redirect for old thread link');
196                         my $location = $res->header('Location');
197                         like($location,
198                              qr!/blah%40example\.com/t\.mbox(?:\.gz)?\z!,
199                              "$sfx redirected to /mbox.gz");
200                 });
201         }
202 }
203
204 done_testing();