9969b21911ebc6bb50df50e05f2a30112df57371
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* $Id: test_filter_frontend_net.cpp,v 1.11 2005-10-29 22:23:36 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     const std::string type() const {
46         return "FilterInit";
47     };
48 };
49
50
51 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_1 )
52 {
53     try 
54     {
55         {
56             yp2::filter::FrontendNet nf;
57         }
58     }
59     catch ( ... ) {
60         BOOST_CHECK (false);
61     }
62 }
63
64 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
65 {
66     try 
67     {
68         {
69             yp2::RouterChain router;
70
71             FilterInit tf;
72
73             router.append(tf);
74
75             // Create package with Z39.50 init request in it
76             yp2::Package pack;
77
78             ODR odr = odr_createmem(ODR_ENCODE);
79             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
80             
81             pack.request() = apdu;
82             odr_destroy(odr);
83             // Done creating query. 
84
85             // Put it in router
86             pack.router(router).move(); 
87
88             // Inspect that we got Z39.50 init response
89             yazpp_1::GDU *gdu = &pack.response();
90
91             Z_GDU *z_gdu = gdu->get();
92             BOOST_CHECK(z_gdu);
93             if (z_gdu) {
94                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
95                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
96             }
97         }
98     }
99     catch ( ... ) {
100         BOOST_CHECK (false);
101     }
102 }
103
104 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
105 {
106     try 
107     {
108         {
109             yp2::RouterChain router;
110
111             // put in frontend first
112             yp2::filter::FrontendNet filter_front;
113
114             std::vector <std::string> ports;
115             ports.insert(ports.begin(), "unix:socket");
116             filter_front.ports() = ports;
117             filter_front.listen_duration() = 1;  // listen a short time only
118             router.append(filter_front);
119
120             // put in a backend
121             FilterInit filter_init;
122             router.append(filter_init);
123
124             yp2::Package pack;
125             
126             pack.router(router).move(); 
127         }
128         BOOST_CHECK(true);
129     }
130     catch ( ... ) {
131         BOOST_CHECK (false);
132     }
133 }
134
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * indent-tabs-mode: nil
139  * c-file-style: "stroustrup"
140  * End:
141  * vim: shiftwidth=4 tabstop=8 expandtab
142  */