moved RouterChain class out of router.hpp file into own router_chain.hpp class
[metaproxy-moved-to-github.git] / src / test_filter_z3950_client.cpp
1 /* $Id: test_filter_z3950_client.cpp,v 1.4 2005-10-26 10:21:03 marc 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_chain.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         yp2::filter::Z3950Client zc; // can we construct OK?
30     }
31     catch ( ... ) {
32         BOOST_CHECK (false);
33     }
34 }
35
36 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_2 )
37 {
38     try 
39     {
40         yp2::RouterChain router;
41         
42         yp2::filter::Z3950Client zc;
43         
44         router.rule(zc);
45         
46         // Create package with Z39.50 init request in it
47         yp2::Package pack;
48         
49         ODR odr = odr_createmem(ODR_ENCODE);
50         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
51         
52         BOOST_CHECK(apdu);
53         
54         pack.request() = apdu;
55         odr_destroy(odr);
56         
57         // Put it in router
58         pack.router(router).move(); 
59         
60         // Inspect that we got Z39.50 init Response - a Z39.50 session MUST
61         // specify a virtual host
62         yazpp_1::GDU *gdu = &pack.response();
63         
64         BOOST_CHECK(pack.session().is_closed()); 
65         
66         Z_GDU *z_gdu = gdu->get();
67         BOOST_CHECK(z_gdu);
68         if (z_gdu) {
69             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
70             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
71         }
72     }
73     catch ( ... ) {
74         BOOST_CHECK (false);
75     }
76 }
77
78 BOOST_AUTO_TEST_CASE( test_filter_z3950_client_3 )
79 {
80     try 
81     {
82         yp2::RouterChain router;
83         
84         yp2::filter::Z3950Client zc;
85
86         router.rule(zc);
87         
88         // Create package with Z39.50 present request in it
89         yp2::Package pack;
90         
91         ODR odr = odr_createmem(ODR_ENCODE);
92         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
93         
94         BOOST_CHECK(apdu);
95         
96         pack.request() = apdu;
97         odr_destroy(odr);
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_TEST_CASE( test_filter_z3950_client_4 )
121 {
122     try 
123     {
124         yp2::RouterChain router;
125         
126         yp2::filter::Z3950Client zc;
127         
128         router.rule(zc);
129         
130         // Create package with Z39.50 init request in it
131         yp2::Package pack;
132         
133         ODR odr = odr_createmem(ODR_ENCODE);
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         odr_destroy(odr);
144         
145         // Put it in router
146         pack.router(router).move(); 
147         
148         if (pack.session().is_closed())
149         {
150             // OK, server was not up!
151         }
152         else
153         {
154             // Inspect that we got Z39.50 init response
155             yazpp_1::GDU *gdu = &pack.response();
156             Z_GDU *z_gdu = gdu->get();
157             BOOST_CHECK(z_gdu);
158             if (z_gdu) {
159                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
160                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
161             }
162         }
163     }
164     catch ( ... ) {
165         BOOST_CHECK (false);
166     }
167 }
168
169
170 /*
171  * Local variables:
172  * c-basic-offset: 4
173  * indent-tabs-mode: nil
174  * c-file-style: "stroustrup"
175  * End:
176  * vim: shiftwidth=4 tabstop=8 expandtab
177  */