moved RouterChain class out of router.hpp file into own router_chain.hpp class
[metaproxy-moved-to-github.git] / src / test_filter_backend_test.cpp
1 /* $Id: test_filter_backend_test.cpp,v 1.2 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 "test_util.hpp"
12 #include "filter_backend_test.hpp"
13 #include "filter_log.hpp"
14
15 #include "router_chain.hpp"
16 #include "session.hpp"
17 #include "package.hpp"
18
19 #include <yaz/zgdu.h>
20 #include <yaz/pquery.h>
21 #include <yaz/otherinfo.h>
22
23 #define BOOST_AUTO_TEST_MAIN
24 #include <boost/test/auto_unit_test.hpp>
25 using namespace boost::unit_test;
26
27 BOOST_AUTO_TEST_CASE( test_filter_backend_test_1 )
28 {
29     try 
30     {
31         yp2::filter::Backend_test btest;
32     }
33     catch ( ... ) {
34         BOOST_CHECK (false);
35     }
36 }
37
38 BOOST_AUTO_TEST_CASE( test_filter_backend_test_2 )
39 {
40     try 
41     {
42         yp2::RouterChain router;
43         
44         yp2::filter::Backend_test btest;
45         router.rule(btest);
46         
47         yp2::Package pack;
48         
49         ODR odr = odr_createmem(ODR_ENCODE);
50         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
51         
52         BOOST_CHECK(apdu);
53         
54         pack.request() = apdu;
55         odr_destroy(odr);
56         
57         // Put it in router
58         pack.router(router).move(); 
59         
60         // Inspect that we got Z39.50 init Response OK.
61         yazpp_1::GDU *gdu = &pack.response();
62         
63         BOOST_CHECK(!pack.session().is_closed()); 
64         
65         Z_GDU *z_gdu = gdu->get();
66         BOOST_CHECK(z_gdu);
67         if (z_gdu) {
68             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
69             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
70         }
71     }
72     catch ( ... ) {
73         BOOST_CHECK (false);
74     }
75 }
76
77 BOOST_AUTO_TEST_CASE( test_filter_backend_test_3 )
78 {
79     try 
80     {
81         yp2::RouterChain router;
82         
83         yp2::filter::Backend_test btest;
84         router.rule(btest);
85         
86         yp2::Package pack;
87         
88         // send search request as first request.. That should fail with
89         // a close from the backend
90         ODR odr = odr_createmem(ODR_ENCODE);
91         Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
92
93         yp2::util::pqf(odr, apdu, "computer");
94         
95         apdu->u.searchRequest->num_databaseNames = 1;
96         apdu->u.searchRequest->databaseNames = (char**)
97             odr_malloc(odr, sizeof(char *));
98         apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default");
99         
100         BOOST_CHECK(apdu);
101         
102         pack.request() = apdu;
103         odr_destroy(odr);
104         
105         // Put it in router
106         pack.router(router).move(); 
107         
108         // Inspect that we got Z39.50 close
109         yazpp_1::GDU *gdu = &pack.response();
110         
111         BOOST_CHECK(pack.session().is_closed()); 
112         
113         Z_GDU *z_gdu = gdu->get();
114         BOOST_CHECK(z_gdu);
115         if (z_gdu) {
116             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
117             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
118         }
119     }
120     catch ( ... ) {
121         BOOST_CHECK (false);
122     }
123 }
124
125 BOOST_AUTO_TEST_CASE( test_filter_backend_test_4 )
126 {
127     try 
128     {
129         yp2::RouterChain router;
130         
131         yp2::filter::Backend_test btest;
132         router.rule(btest);
133         
134         yp2::Package pack;
135         
136         // send present request as first request.. That should fail with
137         // a close from the backend
138         ODR odr = odr_createmem(ODR_ENCODE);
139         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
140
141         BOOST_CHECK(apdu);
142         
143         pack.request() = apdu;
144         odr_destroy(odr);
145         
146         // Put it in router
147         pack.router(router).move(); 
148         
149         // Inspect that we got Z39.50 close
150         yazpp_1::GDU *gdu = &pack.response();
151         
152         BOOST_CHECK(pack.session().is_closed()); 
153         
154         Z_GDU *z_gdu = gdu->get();
155         BOOST_CHECK(z_gdu);
156         if (z_gdu) {
157             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
158             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
159         }
160     }
161     catch ( ... ) {
162         BOOST_CHECK (false);
163     }
164 }
165
166
167 /*
168  * Local variables:
169  * c-basic-offset: 4
170  * indent-tabs-mode: nil
171  * c-file-style: "stroustrup"
172  * End:
173  * vim: shiftwidth=4 tabstop=8 expandtab
174  */