Moved C declaration to start of block
[simpleserver-moved-to-github.git] / test.pl
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
3
4 ######################### We start with some black magic to print on failure.
5
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8
9 BEGIN { $| = 1; print "1..1\n"; }
10 END {print "not ok 1\n" unless $loaded;}
11 use Net::Z3950::SimpleServer;
12 $loaded = 1;
13 print "ok 1\n";
14
15 ######################### End of black magic.
16
17 # Insert your test code below (better if it prints "ok 13"
18 # (correspondingly "not ok 13") depending on the success of chunk 13
19 # of the test code):
20
21 sub my_init_handler {
22         my $href = shift;
23         my %log = ();
24
25         $log{"init"} = "Ok";
26         $href->{HANDLE} = \%log;
27 }
28
29 sub my_search_handler {
30         my $href = shift;
31         my %log = %{$href->{HANDLE}};
32
33         $log{"search"} = "Ok";
34         $href->{HANDLE} = \%log;
35         $href->{HITS} = 1;
36 }
37
38 sub my_fetch_handler {
39         my $href = shift;
40         my %log = %{$href->{HANDLE}};
41         my $record = "<xml><head>Headline</head><body>I am a record</body></xml>";
42
43         $log{"fetch"} = "Ok";
44         $href->{HANDLE} = \%log;
45         $href->{RECORD} = $record;
46         $href->{LEN} = length($record);
47         $href->{NUMBER} = 1;
48         $href->{BASENAME} = "Test";
49 }
50
51 sub my_close_handler {
52         my @services = ("init", "search", "fetch", "close");
53         my $href = shift;
54         my %log = %{$href->{HANDLE}};
55         my $status;
56         my $service;
57         my $error = 0;
58
59         $log{"close"} = "Ok";
60
61         print "\n-----------------------------------------------\n";
62         print "Available Z39.50 services:\n\n";
63
64         foreach $service (@services) {
65                 print "Called $service: ";
66                 if (defined($status = $log{$service})) {
67                         print "$status\n";
68                 } else {
69                         print "FAILED!!!\n";
70                         $error = 1;
71                 }
72         }
73         if ($error) {
74                 print "make test: Failed due to lack of required Z39.50 service\n";
75         } else {
76                 print "\nEverything is ok!\n";
77         }
78         print "-----------------------------------------------\n";
79 }
80
81
82 if (!defined($pid = fork() )) {
83         die "Cannot fork: $!\n";
84 } elsif ($pid) {                                        ## Parent launches server
85         my $handler = Net::Z3950::SimpleServer->new(
86                 INIT            =>      \&my_init_handler,
87                 CLOSE           =>      \&my_close_handler,
88                 SEARCH          =>      \&my_search_handler,
89                 FETCH           =>      \&my_fetch_handler);
90
91         $handler->launch_server("test.pl", "-1", @ARGV);
92 } else {                                                ## Child starts the client
93         sleep(1);
94         open(CLIENT, "| yaz-client tcp:localhost:9999 > /dev/null")
95                 or die "Couldn't fork client: $!\n";
96         print CLIENT "f test\n";
97         print CLIENT "s\n";
98         print CLIENT "close\n";
99         print CLIENT "quit\n";
100         close(CLIENT) or die "Couldn't close: $!\n";
101 }
102