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