Another and hopefully, last, YAZ OID DB update
[metaproxy-moved-to-github.git] / src / test_filter_z3950_client.cpp
1 /* $Id: test_filter_z3950_client.cpp,v 1.12 2007-04-16 21:54:52 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             yaz_oi_set_string_oid(&apdu->u.initRequest->otherInfo,
141                                   odr, yaz_oid_userinfo_proxy, 1, vhost);
142         }
143         BOOST_CHECK(apdu);
144         
145         pack.request() = apdu;
146         
147         // Put it in router
148         pack.router(router).move(); 
149         
150         if (pack.session().is_closed())
151         {
152             // OK, server was not up!
153         }
154         else
155         {
156             // Inspect that we got Z39.50 init response
157             yazpp_1::GDU *gdu = &pack.response();
158             Z_GDU *z_gdu = gdu->get();
159             BOOST_CHECK(z_gdu);
160             if (z_gdu) {
161                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
162                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
163             }
164         }
165     }
166     catch ( ... ) {
167         BOOST_CHECK (false);
168     }
169 }
170
171
172 /*
173  * Local variables:
174  * c-basic-offset: 4
175  * indent-tabs-mode: nil
176  * c-file-style: "stroustrup"
177  * End:
178  * vim: shiftwidth=4 tabstop=8 expandtab
179  */