moved RouterChain class out of router.hpp file into own router_chain.hpp class
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* $Id: test_filter_frontend_net.cpp,v 1.9 2005-10-26 10:21:03 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "filter_frontend_net.hpp"
12
13 #include "router_chain.hpp"
14 #include "session.hpp"
15 #include "package.hpp"
16
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
19
20 using namespace boost::unit_test;
21
22 class FilterInit: public yp2::filter::Base {
23 public:
24     void process(yp2::Package & package) const {
25         
26         if (package.session().is_closed())
27         {
28             // std::cout << "Got Close.\n";
29         }
30        
31         Z_GDU *gdu = package.request().get();
32         if (gdu)
33         {
34             // std::cout << "Got PDU. Sending init response\n";
35             ODR odr = odr_createmem(ODR_ENCODE);
36             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
37             
38             apdu->u.initResponse->implementationName = "YP2/YAZ";
39             
40             package.response() = apdu;
41             odr_destroy(odr);
42         }
43         return package.move();
44     };
45 };
46
47
48 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_1 )
49 {
50     try 
51     {
52         {
53             yp2::filter::FrontendNet nf;
54         }
55     }
56     catch ( ... ) {
57         BOOST_CHECK (false);
58     }
59 }
60
61 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
62 {
63     try 
64     {
65         {
66             yp2::RouterChain router;
67
68             FilterInit tf;
69
70             router.rule(tf);
71
72             // Create package with Z39.50 init request in it
73             yp2::Package pack;
74
75             ODR odr = odr_createmem(ODR_ENCODE);
76             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
77             
78             pack.request() = apdu;
79             odr_destroy(odr);
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_TEST_CASE( test_filter_frontend_net_3 )
102 {
103     try 
104     {
105         {
106             yp2::RouterChain router;
107
108             // put in frontend first
109             yp2::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.rule(filter_front);
116
117             // put in a backend
118             FilterInit filter_init;
119             router.rule(filter_init);
120
121             yp2::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  */