filter factory in working shape, see example in test_filer_factory.cpp
[metaproxy-moved-to-github.git] / src / test_filter_log.cpp
1 /* $Id: test_filter_log.cpp,v 1.5 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_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     const std::string type() const {
46         return "FilterBounceInit";
47     };
48 };
49
50
51 BOOST_AUTO_TEST_CASE( test_filter_log_1 )
52 {
53     try 
54     {
55         yp2::filter::Log lf;
56     }
57     catch ( ... ) {
58         BOOST_CHECK (false);
59     }
60 }
61
62 BOOST_AUTO_TEST_CASE( test_filter_log_2 )
63 {
64     try 
65     {
66         yp2::RouterChain router;
67         
68         yp2::filter::Log lf;
69         FilterBounceInit bf;
70         
71         router.append(lf);
72         router.append(bf);
73         
74         // Create package with Z39.50 init request in it
75         yp2::Package pack;
76         
77         ODR odr = odr_createmem(ODR_ENCODE);
78         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
79         
80         pack.request() = apdu;
81         odr_destroy(odr);
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     catch ( ... ) {
98         BOOST_CHECK (false);
99     }
100 }
101
102 /*
103  * Local variables:
104  * c-basic-offset: 4
105  * indent-tabs-mode: nil
106  * c-file-style: "stroustrup"
107  * End:
108  * vim: shiftwidth=4 tabstop=8 expandtab
109  */