d8edc86c25be64ee040ba52f14ce619205d62420
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* $Id: test_filter_frontend_net.cpp,v 1.18 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 /* $Id: test_filter_frontend_net.cpp,v 1.18 2007-05-09 21:23:09 adam Exp $
22    Copyright (c) 2005-2007, Index Data.
23
24    See the LICENSE file for details
25  */
26
27 #include "config.hpp"
28 #include <iostream>
29 #include <stdexcept>
30
31 #include "util.hpp"
32 #include "filter_frontend_net.hpp"
33
34 #include "router_chain.hpp"
35 #include "session.hpp"
36 #include "package.hpp"
37
38 #define BOOST_AUTO_TEST_MAIN
39 #include <boost/test/auto_unit_test.hpp>
40
41 using namespace boost::unit_test;
42 namespace mp = metaproxy_1;
43
44 class FilterInit: public mp::filter::Base {
45 public:
46     void process(mp::Package & package) const {
47         
48         if (package.session().is_closed())
49         {
50             // std::cout << "Got Close.\n";
51         }
52        
53         Z_GDU *gdu = package.request().get();
54         if (gdu)
55         {
56             // std::cout << "Got PDU. Sending init response\n";
57             mp::odr odr;
58             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
59             
60             apdu->u.initResponse->implementationName = "YP2/YAZ";
61             
62             package.response() = apdu;
63         }
64         return package.move();
65     };
66 };
67
68
69 BOOST_AUTO_UNIT_TEST( test_filter_frontend_net_1 )
70 {
71     try 
72     {
73         {
74             mp::filter::FrontendNet nf;
75         }
76     }
77     catch ( ... ) {
78         BOOST_CHECK (false);
79     }
80 }
81
82 BOOST_AUTO_UNIT_TEST( test_filter_frontend_net_2 )
83 {
84     try 
85     {
86         {
87             mp::RouterChain router;
88
89             FilterInit tf;
90
91             router.append(tf);
92
93             // Create package with Z39.50 init request in it
94             mp::Package pack;
95
96             mp::odr odr;
97             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
98             
99             pack.request() = apdu;
100             // Done creating query. 
101
102             // Put it in router
103             pack.router(router).move(); 
104
105             // Inspect that we got Z39.50 init response
106             yazpp_1::GDU *gdu = &pack.response();
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_initResponse);
113             }
114         }
115     }
116     catch ( ... ) {
117         BOOST_CHECK (false);
118     }
119 }
120
121 BOOST_AUTO_UNIT_TEST( test_filter_frontend_net_3 )
122 {
123     try 
124     {
125         {
126             mp::RouterChain router;
127
128             // put in frontend first
129             mp::filter::FrontendNet filter_front;
130
131             std::vector <std::string> ports;
132             ports.insert(ports.begin(), "unix:socket");
133             filter_front.ports() = ports;
134             filter_front.listen_duration() = 1;  // listen a short time only
135             router.append(filter_front);
136
137             // put in a backend
138             FilterInit filter_init;
139             router.append(filter_init);
140
141             mp::Package pack;
142             
143             pack.router(router).move(); 
144         }
145         BOOST_CHECK(true);
146     }
147     catch ( ... ) {
148         BOOST_CHECK (false);
149     }
150 }
151
152 /*
153  * Local variables:
154  * c-basic-offset: 4
155  * indent-tabs-mode: nil
156  * c-file-style: "stroustrup"
157  * End:
158  * vim: shiftwidth=4 tabstop=8 expandtab
159  */