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