Recognise TIMEOUT errors as well as CONNECT errors in the attempt to connect to no...
[ZOOM-Perl-moved-to-github.git] / t / 1-Net-Z3950-ZOOM.t
1 # $Id: 1-Net-Z3950-ZOOM.t,v 1.17 2008-05-14 13:32:55 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 1-Net-Z3950-ZOOM.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 23;
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
10
11 my $msg = Net::Z3950::ZOOM::diag_str(Net::Z3950::ZOOM::ERROR_INVALID_QUERY);
12 ok($msg eq "Invalid query", "diagnostic string lookup works");
13
14 $msg = Net::Z3950::ZOOM::diag_srw_str(27);
15 ok($msg eq "Empty term unsupported", "SRW diagnostic string lookup works");
16
17 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
18
19 my $host = "no.such.host";
20 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
21 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
22 # For some reason, Red Hat signals this as a TIMEOUT rather than a CONNECT
23 ok(($errcode == Net::Z3950::ZOOM::ERROR_CONNECT ||
24     $errcode == Net::Z3950::ZOOM::ERROR_TIMEOUT) && $addinfo eq $host,
25    "connection to non-existent host '$host' fails: errcode=$errcode, addinfo=$addinfo");
26
27 $host = "z3950.indexdata.com/gils";
28 $conn = Net::Z3950::ZOOM::connection_new($host, 0);
29 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
30 ok($errcode == 0, "connection to '$host'");
31
32 Net::Z3950::ZOOM::connection_destroy($conn);
33 ok(1, "destroyed connection");
34
35 my $options = Net::Z3950::ZOOM::options_create();
36 $conn = Net::Z3950::ZOOM::connection_create($options);
37 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
38 ok($errcode == 0, "unconnected connection object created");
39 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
40 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
41 ok($errcode == 0, "delayed connection to '$host'");
42
43 my $val1 = "foo";
44 my $val2 = "$val1\0bar";
45 Net::Z3950::ZOOM::connection_option_set($conn, xyz => $val2);
46 my $val = Net::Z3950::ZOOM::connection_option_get($conn, "xyz");
47 ok($val eq $val1, "option_set() treats value as NUL-terminated");
48 Net::Z3950::ZOOM::connection_option_setl($conn, xyz => $val2, length($val2));
49 my $vallen = 0;
50 $val = Net::Z3950::ZOOM::connection_option_getl($conn, "xyz", $vallen);
51 ok($val eq $val2, "option_setl() treats value as opaque chunk, val='$val' len=$vallen");
52
53 my $syntax = "usmarc";
54 Net::Z3950::ZOOM::connection_option_set($conn,
55                                         preferredRecordSyntax => $syntax);
56 $val = Net::Z3950::ZOOM::connection_option_get($conn, "preferredRecordSyntax");
57 ok($val eq $syntax, "preferred record syntax set to '$val'");
58
59 my $query = '@attr @and 1=4 minerals';
60 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
61 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
62 ok($errcode == Net::Z3950::ZOOM::ERROR_INVALID_QUERY,
63    "search for invalid query '$query' fails");
64
65 my($xcode, $xmsg, $xinfo, $xset) = (undef, "dummy", "dummy", "dummy");
66 $xcode = Net::Z3950::ZOOM::connection_error_x($conn, $xmsg, $xinfo, $xset);
67 ok($xcode == $errcode && $xmsg eq $errmsg && $xinfo eq $addinfo &&
68    $xset eq "ZOOM", "error_x() consistent with error()");
69 ok(Net::Z3950::ZOOM::connection_errcode($conn) == $errcode,
70    "errcode() consistent with error()");
71 ok(Net::Z3950::ZOOM::connection_errmsg($conn) eq $errmsg,
72    "errmsg() consistent with error()");
73 ok(Net::Z3950::ZOOM::connection_addinfo($conn) eq $addinfo,
74    "addinfo() consistent with error()");
75 ok(Net::Z3950::ZOOM::connection_diagset($conn) eq $xset,
76    "diagset() consistent with error()");
77
78 $query = '@attr 1=4 minerals';
79 $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
80 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
81 ok($errcode == 0, "search for '$query'");
82
83 my $n = Net::Z3950::ZOOM::resultset_size($rs);
84 ok($n == 1, "found 1 record as expected");
85
86 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
87 my $data = Net::Z3950::ZOOM::record_get($rec, "render");
88 ok($data =~ /^245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS$/m,
89    "rendered record has expected title");
90 my $raw = Net::Z3950::ZOOM::record_get($rec, "raw");
91 ok($raw =~ /^00966n/, "raw record contains expected header");
92
93 Net::Z3950::ZOOM::resultset_destroy($rs);
94 ok(1, "destroyed result-set");
95 Net::Z3950::ZOOM::connection_destroy($conn);
96 ok(1, "destroyed connection");