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