Added Z3950 Client Filter, AKA Z39.50 backend.
[metaproxy-moved-to-github.git] / src / test_filter_z3950_client.cpp
1 /* $Id: test_filter_z3950_client.cpp,v 1.1 2005-10-16 16:05:44 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "filter_z3950_client.hpp"
12
13 #include "router.hpp"
14 #include "session.hpp"
15 #include "package.hpp"
16
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
19
20 #include <yaz/zgdu.h>
21 #include <yaz/otherinfo.h>
22 using namespace boost::unit_test;
23
24
25 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_1 )
26 {
27     try 
28     {
29         {
30             yp2::filter::Z3950Client zc;
31         }
32     }
33     catch ( ... ) {
34         BOOST_CHECK (false);
35     }
36 }
37
38 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_2 )
39 {
40     try 
41     {
42         {
43             yp2::RouterChain router;
44
45             yp2::filter::Z3950Client zc;
46
47             router.rule(zc);
48
49             // Create package with Z39.50 init request in it
50             yp2::Package pack;
51
52             ODR odr = odr_createmem(ODR_ENCODE);
53             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
54
55             BOOST_CHECK(apdu);
56
57             pack.request() = apdu;
58             odr_destroy(odr);
59
60             // Put it in router
61             pack.router(router).move(); 
62
63             // Inspect that we got Z39.50 init Response - a Z39.50 session
64             // specify a virtual host
65             yazpp_1::GDU *gdu = &pack.response();
66
67             BOOST_CHECK(pack.session().is_closed()); 
68
69             Z_GDU *z_gdu = gdu->get();
70             BOOST_CHECK(z_gdu);
71             if (z_gdu) {
72                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
73                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
74             }
75         }
76     }
77     catch ( ... ) {
78         BOOST_CHECK (false);
79     }
80 }
81
82 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_3 )
83 {
84     try 
85     {
86         {
87             yp2::RouterChain router;
88
89             yp2::filter::Z3950Client zc;
90
91             router.rule(zc);
92
93             // Create package with Z39.50 present request in it
94             yp2::Package pack;
95
96             ODR odr = odr_createmem(ODR_ENCODE);
97             Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
98
99             BOOST_CHECK(apdu);
100
101             pack.request() = apdu;
102             odr_destroy(odr);
103
104             // Put it in router
105             pack.router(router).move(); 
106
107             // Inspect that we got Z39.50 close - a Z39.50 session must start
108             // with an init !
109             yazpp_1::GDU *gdu = &pack.response();
110
111             BOOST_CHECK(pack.session().is_closed()); 
112
113             Z_GDU *z_gdu = gdu->get();
114             BOOST_CHECK(z_gdu);
115             if (z_gdu) {
116                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
117                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
118             }
119         }
120     }
121     catch ( ... ) {
122         BOOST_CHECK (false);
123     }
124 }
125
126 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_4 )
127 {
128     try 
129     {
130         {
131             yp2::RouterChain router;
132
133             yp2::filter::Z3950Client zc;
134
135             router.rule(zc);
136
137             // Create package with Z39.50 init request in it
138             yp2::Package pack;
139
140             ODR odr = odr_createmem(ODR_ENCODE);
141             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
142             
143             const char *vhost = "localhost:9999";
144             if (vhost)
145                 yaz_oi_set_string_oidval(&apdu->u.initRequest->otherInfo,
146                                          odr, VAL_PROXY, 1, vhost);
147
148             BOOST_CHECK(apdu);
149
150             pack.request() = apdu;
151             odr_destroy(odr);
152
153             // Put it in router
154             pack.router(router).move(); 
155
156             // Inspect that we got Z39.50 close - a Z39.50 session must start
157             // with an init !
158             yazpp_1::GDU *gdu = &pack.response();
159
160             BOOST_CHECK(!pack.session().is_closed()); 
161
162             Z_GDU *z_gdu = gdu->get();
163             BOOST_CHECK(z_gdu);
164             if (z_gdu) {
165                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
166                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
167             }
168         }
169     }
170     catch ( ... ) {
171         BOOST_CHECK (false);
172     }
173 }
174
175
176 /*
177  * Local variables:
178  * c-basic-offset: 4
179  * indent-tabs-mode: nil
180  * c-file-style: "stroustrup"
181  * End:
182  * vim: shiftwidth=4 tabstop=8 expandtab
183  */