Implemented yp2::odr which is a noncopyable wrapper for YAZ' ODR.
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* $Id: test_filter_frontend_net.cpp,v 1.12 2005-10-30 17:13:36 adam 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 "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
23 class FilterInit: public yp2::filter::Base {
24 public:
25     void process(yp2::Package & package) const {
26         
27         if (package.session().is_closed())
28         {
29             // std::cout << "Got Close.\n";
30         }
31        
32         Z_GDU *gdu = package.request().get();
33         if (gdu)
34         {
35             // std::cout << "Got PDU. Sending init response\n";
36             yp2::odr odr;
37             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
38             
39             apdu->u.initResponse->implementationName = "YP2/YAZ";
40             
41             package.response() = apdu;
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             yp2::odr odr;
79             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
80             
81             pack.request() = apdu;
82             // Done creating query. 
83
84             // Put it in router
85             pack.router(router).move(); 
86
87             // Inspect that we got Z39.50 init response
88             yazpp_1::GDU *gdu = &pack.response();
89
90             Z_GDU *z_gdu = gdu->get();
91             BOOST_CHECK(z_gdu);
92             if (z_gdu) {
93                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
94                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
95             }
96         }
97     }
98     catch ( ... ) {
99         BOOST_CHECK (false);
100     }
101 }
102
103 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
104 {
105     try 
106     {
107         {
108             yp2::RouterChain router;
109
110             // put in frontend first
111             yp2::filter::FrontendNet filter_front;
112
113             std::vector <std::string> ports;
114             ports.insert(ports.begin(), "unix:socket");
115             filter_front.ports() = ports;
116             filter_front.listen_duration() = 1;  // listen a short time only
117             router.append(filter_front);
118
119             // put in a backend
120             FilterInit filter_init;
121             router.append(filter_init);
122
123             yp2::Package pack;
124             
125             pack.router(router).move(); 
126         }
127         BOOST_CHECK(true);
128     }
129     catch ( ... ) {
130         BOOST_CHECK (false);
131     }
132 }
133
134 /*
135  * Local variables:
136  * c-basic-offset: 4
137  * indent-tabs-mode: nil
138  * c-file-style: "stroustrup"
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */