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