Bump year in copyright msg in source
[metaproxy-moved-to-github.git] / src / test_filter_z3950_client.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "filter_z3950_client.hpp"
24 #include "util.hpp"
25
26 #include "router_chain.hpp"
27 #include "session.hpp"
28 #include "package.hpp"
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/auto_unit_test.hpp>
33
34 #include <yaz/zgdu.h>
35 #include <yaz/otherinfo.h>
36 #include <yaz/oid_db.h>
37
38 using namespace boost::unit_test;
39 namespace mp = metaproxy_1;
40
41 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_1 )
42 {
43     try 
44     {
45         mp::filter::Z3950Client zc; // can we construct OK?
46     }
47     catch ( ... ) {
48         BOOST_CHECK (false);
49     }
50 }
51
52 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_2 )
53 {
54     try 
55     {
56         mp::RouterChain router;
57         
58         mp::filter::Z3950Client zc;
59         
60         router.append(zc);
61         
62         // Create package with Z39.50 init request in it
63         mp::Package pack;
64         
65         mp::odr odr;
66         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
67         
68         BOOST_CHECK(apdu);
69         
70         pack.request() = apdu;
71         
72         // Put it in router
73         pack.router(router).move(); 
74         
75         // Inspect that we got Z39.50 init Response - a Z39.50 session MUST
76         // specify a virtual host
77         yazpp_1::GDU *gdu = &pack.response();
78         
79         BOOST_CHECK(pack.session().is_closed()); 
80         
81         Z_GDU *z_gdu = gdu->get();
82         BOOST_CHECK(z_gdu);
83         if (z_gdu) {
84             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
85             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
86         }
87     }
88     catch ( ... ) {
89         BOOST_CHECK (false);
90     }
91 }
92
93 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_3 )
94 {
95     try 
96     {
97         mp::RouterChain router;
98         
99         mp::filter::Z3950Client zc;
100
101         router.append(zc);
102         
103         // Create package with Z39.50 present request in it
104         mp::Package pack;
105         
106         mp::odr odr;
107         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
108         
109         BOOST_CHECK(apdu);
110         
111         pack.request() = apdu;
112         
113         // Put it in router
114         pack.router(router).move(); 
115         
116         // Inspect that we got Z39.50 close - a Z39.50 session must start
117         // with an init !
118         yazpp_1::GDU *gdu = &pack.response();
119         
120         BOOST_CHECK(pack.session().is_closed()); 
121         
122         Z_GDU *z_gdu = gdu->get();
123         BOOST_CHECK(z_gdu);
124         if (z_gdu) {
125             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
126             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
127         }
128     }
129     catch ( ... ) {
130         BOOST_CHECK (false);
131     }
132 }
133
134 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_4 )
135 {
136     try 
137     {
138         mp::RouterChain router;
139         
140         mp::filter::Z3950Client zc;
141         
142         router.append(zc);
143         
144         // Create package with Z39.50 init request in it
145         mp::Package pack;
146         
147         mp::odr odr;
148         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
149         
150         const char *vhost = "localhost:9999";
151         if (vhost)
152         {
153             yaz_oi_set_string_oid(&apdu->u.initRequest->otherInfo,
154                                   odr, yaz_oid_userinfo_proxy, 1, vhost);
155         }
156         BOOST_CHECK(apdu);
157         
158         pack.request() = apdu;
159         
160         // Put it in router
161         pack.router(router).move(); 
162         
163         if (pack.session().is_closed())
164         {
165             // OK, server was not up!
166         }
167         else
168         {
169             // Inspect that we got Z39.50 init response
170             yazpp_1::GDU *gdu = &pack.response();
171             Z_GDU *z_gdu = gdu->get();
172             BOOST_CHECK(z_gdu);
173             if (z_gdu) {
174                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
175                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
176             }
177         }
178     }
179     catch ( ... ) {
180         BOOST_CHECK (false);
181     }
182 }
183
184
185 /*
186  * Local variables:
187  * c-basic-offset: 4
188  * c-file-style: "Stroustrup"
189  * indent-tabs-mode: nil
190  * End:
191  * vim: shiftwidth=4 tabstop=8 expandtab
192  */
193