]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd.t
44df1642cc51699d407b5285e386b4c0ffe506e7
[public-inbox.git] / t / httpd.t
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6
7 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
8                         HTTP::Date HTTP::Status)) {
9         eval "require $mod";
10         plan skip_all => "$mod missing for httpd.t" if $@;
11 }
12 use File::Temp qw/tempdir/;
13 use Cwd qw/getcwd/;
14 use IO::Socket;
15 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
16 require './t/common.perl';
17
18 # FIXME: too much setup
19 my $tmpdir = tempdir('pi-httpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 my $home = "$tmpdir/pi-home";
21 my $err = "$tmpdir/stderr.log";
22 my $out = "$tmpdir/stdout.log";
23 my $maindir = "$tmpdir/main.git";
24 my $group = 'test-httpd';
25 my $addr = $group . '@example.com';
26 my $cfgpfx = "publicinbox.$group";
27 my $httpd = 'blib/script/public-inbox-httpd';
28 my $init = 'blib/script/public-inbox-init';
29
30 my %opts = (
31         LocalAddr => '127.0.0.1',
32         ReuseAddr => 1,
33         Proto => 'tcp',
34         Type => SOCK_STREAM,
35         Listen => 1024,
36 );
37 my $sock = IO::Socket::INET->new(%opts);
38 my $pid;
39 use_ok 'PublicInbox::Git';
40 use_ok 'PublicInbox::Import';
41 use_ok 'Email::MIME';
42 END { kill 'TERM', $pid if defined $pid };
43 {
44         local $ENV{HOME} = $home;
45         ok(!system($init, $group, $maindir, 'http://example.com/', $addr),
46                 'init ran properly');
47
48         # ensure successful message delivery
49         {
50                 my $mime = Email::MIME->new(<<EOF);
51 From: Me <me\@example.com>
52 To: You <you\@example.com>
53 Cc: $addr
54 Message-Id: <nntp\@example.com>
55 Subject: hihi
56 Date: Thu, 01 Jan 1970 06:06:06 +0000
57
58 nntp
59 EOF
60
61                 my $git = PublicInbox::Git->new($maindir);
62                 my $im = PublicInbox::Import->new($git, 'test', $addr);
63                 $im->add($mime);
64                 $im->done($mime);
65         }
66         ok($sock, 'sock created');
67         my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
68         $pid = spawn_listener(undef, $cmd, [$sock]);
69         my $host = $sock->sockhost;
70         my $port = $sock->sockport;
71         my $conn = IO::Socket::INET->new(PeerAddr => $host,
72                                 PeerPort => $port,
73                                 Proto => 'tcp',
74                                 Type => SOCK_STREAM);
75         ok($conn, 'connected');
76         ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
77         {
78                 my $buf;
79                 ok($conn->read($buf, 4096), 'read some bytes');
80                 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
81                 is($conn->read($buf, 1), 0, "EOF");
82         }
83
84         is(system(qw(git clone -q --mirror),
85                         "http://$host:$port/$group", "$tmpdir/clone.git"),
86                 0, 'smart clone successful');
87
88         # ensure dumb cloning works, too:
89         is(system('git', "--git-dir=$maindir",
90                 qw(config http.uploadpack false)),
91                 0, 'disable http.uploadpack');
92         is(system(qw(git clone -q --mirror),
93                         "http://$host:$port/$group", "$tmpdir/dumb.git"),
94                 0, 'clone successful');
95
96         ok(kill('TERM', $pid), 'killed httpd');
97         $pid = undef;
98         waitpid(-1, 0);
99
100         is(system('git', "--git-dir=$tmpdir/clone.git",
101                   qw(fsck --no-verbose)), 0,
102                 'fsck on cloned directory successful');
103 }
104
105 done_testing();
106
107 1;