Implemented yp2::odr which is a noncopyable wrapper for YAZ' ODR.
[metaproxy-moved-to-github.git] / src / test_filter_z3950_client.cpp
1 /* $Id: test_filter_z3950_client.cpp,v 1.6 2005-10-30 17:13:36 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "filter_z3950_client.hpp"
12 #include "util.hpp"
13
14 #include "router_chain.hpp"
15 #include "session.hpp"
16 #include "package.hpp"
17
18 #define BOOST_AUTO_TEST_MAIN
19 #include <boost/test/auto_unit_test.hpp>
20
21 #include <yaz/zgdu.h>
22 #include <yaz/otherinfo.h>
23 using namespace boost::unit_test;
24
25
26 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_1 )
27 {
28     try 
29     {
30         yp2::filter::Z3950Client zc; // can we construct OK?
31     }
32     catch ( ... ) {
33         BOOST_CHECK (false);
34     }
35 }
36
37 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_2 )
38 {
39     try 
40     {
41         yp2::RouterChain router;
42         
43         yp2::filter::Z3950Client zc;
44         
45         router.append(zc);
46         
47         // Create package with Z39.50 init request in it
48         yp2::Package pack;
49         
50         yp2::odr odr;
51         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
52         
53         BOOST_CHECK(apdu);
54         
55         pack.request() = apdu;
56         
57         // Put it in router
58         pack.router(router).move(); 
59         
60         // Inspect that we got Z39.50 init Response - a Z39.50 session MUST
61         // specify a virtual host
62         yazpp_1::GDU *gdu = &pack.response();
63         
64         BOOST_CHECK(pack.session().is_closed()); 
65         
66         Z_GDU *z_gdu = gdu->get();
67         BOOST_CHECK(z_gdu);
68         if (z_gdu) {
69             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
70             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
71         }
72     }
73     catch ( ... ) {
74         BOOST_CHECK (false);
75     }
76 }
77
78 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_3 )
79 {
80     try 
81     {
82         yp2::RouterChain router;
83         
84         yp2::filter::Z3950Client zc;
85
86         router.append(zc);
87         
88         // Create package with Z39.50 present request in it
89         yp2::Package pack;
90         
91         yp2::odr odr;
92         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
93         
94         BOOST_CHECK(apdu);
95         
96         pack.request() = apdu;
97         
98         // Put it in router
99         pack.router(router).move(); 
100         
101         // Inspect that we got Z39.50 close - a Z39.50 session must start
102         // with an init !
103         yazpp_1::GDU *gdu = &pack.response();
104         
105         BOOST_CHECK(pack.session().is_closed()); 
106         
107         Z_GDU *z_gdu = gdu->get();
108         BOOST_CHECK(z_gdu);
109         if (z_gdu) {
110             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
111             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
112         }
113     }
114     catch ( ... ) {
115         BOOST_CHECK (false);
116     }
117 }
118
119 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_4 )
120 {
121     try 
122     {
123         yp2::RouterChain router;
124         
125         yp2::filter::Z3950Client zc;
126         
127         router.append(zc);
128         
129         // Create package with Z39.50 init request in it
130         yp2::Package pack;
131         
132         yp2::odr odr;
133         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
134         
135         const char *vhost = "localhost:9999";
136         if (vhost)
137             yaz_oi_set_string_oidval(&apdu->u.initRequest->otherInfo,
138                                      odr, VAL_PROXY, 1, vhost);
139         BOOST_CHECK(apdu);
140         
141         pack.request() = apdu;
142         
143         // Put it in router
144         pack.router(router).move(); 
145         
146         if (pack.session().is_closed())
147         {
148             // OK, server was not up!
149         }
150         else
151         {
152             // Inspect that we got Z39.50 init response
153             yazpp_1::GDU *gdu = &pack.response();
154             Z_GDU *z_gdu = gdu->get();
155             BOOST_CHECK(z_gdu);
156             if (z_gdu) {
157                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
158                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
159             }
160         }
161     }
162     catch ( ... ) {
163         BOOST_CHECK (false);
164     }
165 }
166
167
168 /*
169  * Local variables:
170  * c-basic-offset: 4
171  * indent-tabs-mode: nil
172  * c-file-style: "stroustrup"
173  * End:
174  * vim: shiftwidth=4 tabstop=8 expandtab
175  */