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