c19958ea282e7bf04058710440fc2c433860430d
[metaproxy-moved-to-github.git] / src / test_filter_log.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "filter_log.hpp"
24 #include "util.hpp"
25 #include "router_chain.hpp"
26 #include "session.hpp"
27 #include "package.hpp"
28
29 #define BOOST_AUTO_TEST_MAIN
30 #define BOOST_TEST_DYN_LINK
31 #include <boost/test/auto_unit_test.hpp>
32
33 using namespace boost::unit_test;
34 namespace mp = metaproxy_1;
35
36 class FilterBounceInit: public mp::filter::Base {
37 public:
38     void process(mp::Package & package) const {
39         
40         if (package.session().is_closed())
41         {
42             // std::cout << "Got Close.\n";
43         }
44        
45         Z_GDU *gdu = package.request().get();
46         if (gdu)
47         {
48             // std::cout << "Got PDU. Sending init response\n";
49             mp::odr odr;
50             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
51             
52             apdu->u.initResponse->implementationName = "YP2/YAZ";
53             
54             package.response() = apdu;
55         }
56         return package.move();
57     };
58 };
59
60
61 BOOST_AUTO_TEST_CASE( test_filter_log_1 )
62 {
63     try 
64     {
65         mp::filter::Log lf;
66     }
67     catch ( ... ) {
68         BOOST_CHECK (false);
69     }
70 }
71
72 BOOST_AUTO_TEST_CASE( test_filter_log_2 )
73 {
74     try 
75     {
76         mp::RouterChain router;
77         
78         mp::filter::Log lf;
79         FilterBounceInit bf;
80         
81         router.append(lf);
82         router.append(bf);
83         
84         // Create package with Z39.50 init request in it
85         mp::Package pack;
86         
87         mp::odr odr;
88         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
89         
90         pack.request() = apdu;
91         // Done creating query. 
92         
93         // Put it in router
94         pack.router(router).move(); 
95         
96         // Inspect that we got Z39.50 init response
97         yazpp_1::GDU *gdu = &pack.response();
98         
99         Z_GDU *z_gdu = gdu->get();
100         BOOST_CHECK(z_gdu);
101         if (z_gdu) {
102             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
103             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
104         }
105     }
106     catch ( ... ) {
107         BOOST_CHECK (false);
108     }
109 }
110
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * indent-tabs-mode: nil
115  * c-file-style: "stroustrup"
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */