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