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