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