GPL v2.
[metaproxy-moved-to-github.git] / src / test_filter_bounce.cpp
1 /* $Id: test_filter_bounce.cpp,v 1.3 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include "filter_bounce.hpp"
24 #include "util.hpp"
25 #include "gduutil.hpp"
26 //#include "sru_util.hpp"
27 #include "router_chain.hpp"
28 #include "session.hpp"
29 #include "package.hpp"
30
31 #include <iostream>
32 #include <stdexcept>
33
34 #define BOOST_AUTO_TEST_MAIN
35 #include <boost/test/auto_unit_test.hpp>
36
37
38
39
40 using namespace boost::unit_test;
41
42 namespace mp = metaproxy_1;
43 using namespace mp::util;
44
45 void check_bounce_z3950(mp::RouterChain &router, int request, int response)
46 {
47
48     bool print = false;
49     if (print)
50         std::cout << "check_bounce_z3950\n";
51
52     // Create package with Z39.50 init request in it
53     mp::Package pack;
54         
55     mp::odr odr;
56     Z_APDU *apdu = zget_APDU(odr, request);
57
58     pack.request() = apdu;
59
60     // Put it in router
61     pack.router(router).move(); 
62     
63     // Inspect bounced back request
64     yazpp_1::GDU *gdu_req = &pack.request();
65     yazpp_1::GDU *gdu_res = &pack.response();
66     
67     Z_GDU *z_gdu_req = gdu_req->get();
68     Z_GDU *z_gdu_res = gdu_res->get();
69
70     BOOST_CHECK(z_gdu_req);
71     if (z_gdu_req) {
72         if (print)
73             std::cout << "Z_GDU " << *(z_gdu_req) << "\n";
74         BOOST_CHECK_EQUAL(z_gdu_req->which, Z_GDU_Z3950);
75         BOOST_CHECK_EQUAL(z_gdu_req->u.z3950->which, request);
76     }
77
78     BOOST_CHECK(z_gdu_res);
79     if (z_gdu_res) {
80         if (print)
81             std::cout << "Z_GDU " << *(z_gdu_res) << "\n";
82         BOOST_CHECK_EQUAL(z_gdu_res->which, Z_GDU_Z3950);
83         BOOST_CHECK_EQUAL(z_gdu_res->u.z3950->which, response);
84     }
85 }
86
87 void check_bounce_http(mp::RouterChain &router)
88 {
89
90     bool print = false;
91     if (print)
92         std::cout << "check_bounce_http\n";
93
94     // Create package with Z39.50 init request in it
95     mp::Package pack;
96         
97     mp::odr odr;
98     Z_GDU *gdu = z_get_HTTP_Request(odr);
99     //z_get_HTTP_Request_host_path(odr, host, path);
100     pack.request() = gdu;
101
102     // Put it in router
103     pack.router(router).move(); 
104     
105     // Inspect bounced back request
106     yazpp_1::GDU *gdu_req = &pack.request();
107     yazpp_1::GDU *gdu_res = &pack.response();
108     
109     Z_GDU *z_gdu_req = gdu_req->get();
110     Z_GDU *z_gdu_res = gdu_res->get();
111
112     BOOST_CHECK(z_gdu_req);
113     if (z_gdu_req) {
114         if (print)
115             std::cout << "Z_GDU " << *(z_gdu_req) << "\n";
116         BOOST_CHECK_EQUAL(z_gdu_req->which, Z_GDU_HTTP_Request);
117     }
118
119     BOOST_CHECK(z_gdu_res);
120     if (z_gdu_res) {
121         if (print)
122             std::cout << "Z_GDU " << *(z_gdu_res) << "\n";
123         BOOST_CHECK_EQUAL(z_gdu_res->which,  Z_GDU_HTTP_Response);
124     }
125 }
126
127 BOOST_AUTO_UNIT_TEST( test_filter_bounce_1 )
128 {
129     try 
130     {
131         mp::filter::Bounce f_bounce;
132     }
133     catch ( ... ) {
134         BOOST_CHECK (false);
135     }
136 }
137
138 BOOST_AUTO_UNIT_TEST( test_filter_bounce_2 )
139 {
140     try 
141     {
142         mp::RouterChain router;        
143         mp::filter::Bounce f_bounce;
144         router.append(f_bounce);
145
146         check_bounce_z3950(router, 
147                            Z_APDU_initRequest, Z_APDU_close);
148         //check_bounce_z3950(router, 
149         //                   Z_APDU_searchRequest, Z_APDU_close);
150         check_bounce_z3950(router, 
151                            Z_APDU_presentRequest, Z_APDU_close);
152         check_bounce_z3950(router, 
153                            Z_APDU_deleteResultSetRequest, Z_APDU_close);
154         //check_bounce_z3950(router, 
155         //                   Z_APDU_accessControlRequest, Z_APDU_close);
156         check_bounce_z3950(router, 
157                            Z_APDU_resourceControlRequest, Z_APDU_close);
158         check_bounce_z3950(router, 
159                            Z_APDU_triggerResourceControlRequest, Z_APDU_close);
160         check_bounce_z3950(router, 
161                            Z_APDU_resourceReportRequest, Z_APDU_close);
162         //check_bounce_z3950(router, 
163         //                   Z_APDU_scanRequest, Z_APDU_close);
164         //check_bounce_z3950(router, 
165         //                   Z_APDU_sortRequest, Z_APDU_close);
166         check_bounce_z3950(router, 
167                            Z_APDU_segmentRequest, Z_APDU_close);
168         //check_bounce_z3950(router, 
169         //                   Z_APDU_extendedServicesRequest, Z_APDU_close);
170         check_bounce_z3950(router, 
171                            Z_APDU_close , Z_APDU_close);
172         //check_bounce_z3950(router, 
173         //                   Z_APDU_duplicateDetectionRequest, Z_APDU_close);
174
175
176     }
177     catch ( ... ) {
178         BOOST_CHECK (false);
179     }
180 }
181
182 BOOST_AUTO_UNIT_TEST( test_filter_bounce_3 )
183 {
184     try 
185     {
186         mp::RouterChain router;        
187         mp::filter::Bounce f_bounce;
188         router.append(f_bounce);
189
190         check_bounce_http(router);
191
192     }
193     catch ( ... ) {
194         BOOST_CHECK (false);
195     }
196 }
197
198 /*
199  * Local variables:
200  * c-basic-offset: 4
201  * indent-tabs-mode: nil
202  * c-file-style: "stroustrup"
203  * End:
204  * vim: shiftwidth=4 tabstop=8 expandtab
205  */