changed call to RouterChain.append instead of RouterChain.rule
[metaproxy-moved-to-github.git] / src / test_filter_log.cpp
1 /* $Id: test_filter_log.cpp,v 1.4 2005-10-26 10:55:26 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_log.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 FilterBounceInit: 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_log_1 )
49 {
50     try 
51     {
52         yp2::filter::Log lf;
53     }
54     catch ( ... ) {
55         BOOST_CHECK (false);
56     }
57 }
58
59 BOOST_AUTO_TEST_CASE( test_filter_log_2 )
60 {
61     try 
62     {
63         yp2::RouterChain router;
64         
65         yp2::filter::Log lf;
66         FilterBounceInit bf;
67         
68         router.append(lf);
69         router.append(bf);
70         
71         // Create package with Z39.50 init request in it
72         yp2::Package pack;
73         
74         ODR odr = odr_createmem(ODR_ENCODE);
75         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
76         
77         pack.request() = apdu;
78         odr_destroy(odr);
79         // Done creating query. 
80         
81         // Put it in router
82         pack.router(router).move(); 
83         
84         // Inspect that we got Z39.50 init response
85         yazpp_1::GDU *gdu = &pack.response();
86         
87         Z_GDU *z_gdu = gdu->get();
88         BOOST_CHECK(z_gdu);
89         if (z_gdu) {
90             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
91             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
92         }
93     }
94     catch ( ... ) {
95         BOOST_CHECK (false);
96     }
97 }
98
99 /*
100  * Local variables:
101  * c-basic-offset: 4
102  * indent-tabs-mode: nil
103  * c-file-style: "stroustrup"
104  * End:
105  * vim: shiftwidth=4 tabstop=8 expandtab
106  */