File: daemon.t

package info (click to toggle)
libpod-webserver-perl 3.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 160 kB
  • sloc: perl: 440; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use strict;
use warnings;

use IO::Socket;

use Test::More tests => 8;

use Net::EmptyPort 'empty_port';

BEGIN {
    chdir "t" if -e "t";
    if($ENV{PERL_CORE}) {
        @INC = '../lib';
    } else {
        push @INC, '../lib';
    }
	$ENV{'PODWEBSERVERPORT'} = empty_port() if (! $ENV{'PODWEBSERVERPORT'});
}
# When run with the single argument 'client', the test script should run
# a dummy client and exit.
my $mode = shift || '';
my $port = $ENV{'PODWEBSERVERPORT'};
if ($mode eq 'client') {
    my $sock = IO::Socket::INET->new("localhost:$port")
      or die "Couldn't connect to localhost:$port: $!";
    send ($sock,"GET Pod/Simple HTTP/1.0\15\12", 0x4);
    exit;
}

use Pod::Webserver;
ok 1;

my $ws = Pod::Webserver->new();
ok ($ws);
$ws->dir_exclude([]);
$ws->dir_include([@INC]);
$ws->verbose(0);
$ws->httpd_timeout(10);
$ws->httpd_port($port);
$ws->prep_for_daemon;
my $daemon;
eval { $daemon = $ws->new_daemon; };
if ($@) {
    die $@ . "Try setting the PODWEBSERVERPORT environment variable to
              another port"; }

ok ($daemon);
my $sock = $daemon->{__sock};
#shutdown ($sock, 2);

# Create a dummy client in another process.
use Config;
my $perl = $Config{'perlpath'};
open(my $fh, "$perl daemon.t client |") or die "Can't exec client: $!";

# Accept a request from the dummy client.
my $conn = $daemon->accept;
ok ($conn);
my $req = $conn->get_request;
ok ($req);
ok ($req->url, 'Pod/Simple');
ok ($req->method, 'GET');
$conn->close;
close $fh;

# Test the response from the daemon.
my $testfile = 'dummysocket.txt';
open($fh, '>', $testfile);
$conn = Pod::Webserver::Connection->new(*$fh);
$ws->_serve_thing($conn, $req);
$conn->close;

my $captured_response;
{
    open(my $fh1, $testfile);
    local $/ = '';
    $captured_response = <$fh1>;
    close $fh1;
    unlink $testfile;
}
ok ($captured_response, qr/Pod::Simple/);

shutdown ($sock, 2);
exit;