1
2
3
4
5
6
7 package nl.mineleni.cbsviewer.servlet;
8
9 import static javax.servlet.http.HttpServletResponse.SC_BAD_GATEWAY;
10 import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
11 import static nl.mineleni.cbsviewer.servlet.AbstractBaseServlet.USER_ID;
12 import static nl.mineleni.cbsviewer.servlet.AbstractBaseServlet.USER_PASSWORD;
13 import static nl.mineleni.cbsviewer.servlet.ReverseProxyServlet.ALLOWED_HOSTS;
14 import static nl.mineleni.cbsviewer.servlet.ReverseProxyServlet.ERR_MSG_MISSING_CONFIG;
15 import static nl.mineleni.cbsviewer.servlet.ReverseProxyServlet.FORCE_XML_MIME;
16 import static org.hamcrest.Matchers.greaterThan;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertSame;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.jmock.Expectations;
35 import org.jmock.integration.junit4.JUnitRuleMockery;
36 import org.junit.Before;
37 import org.junit.Test;
38
39
40
41
42
43
44
45 public class ReverseProxyServletTest {
46
47 private ReverseProxyServlet pxyServlet;
48
49
50 private final JUnitRuleMockery mockery = new JUnitRuleMockery();
51
52
53 private HttpServletRequest request;
54
55
56 private ServletConfig servletConfig;
57
58
59 private ServletContext servletContext;
60
61
62 private HttpServletResponse response;
63
64
65 private static final String SERVERNAME_DOESNOTEXIST = "blaat.non-existentserver1.com";
66
67
68 private static final String SERVERNAME_DOESNOTEXIST_URL = "http://"
69 + SERVERNAME_DOESNOTEXIST + "/";
70
71
72 private static final String UNALLOWEDSERVERNAME = "notallowed.minlnv.nl";
73
74
75 private static final String UNALLOWEDSERVERNAME_URL = "http://"
76 + UNALLOWEDSERVERNAME + "/test/notallowed";
77
78
79 public static final String SERVERNAME = "dbr0425s.dbr.agro.nl:8080";
80
81
82 private static final String SERVERNAME_URL = "http://" + SERVERNAME
83 + "/dashboard/";
84
85
86 private static final String INVALID_URL = "/lnv-common-gis/";
87
88
89
90
91
92
93
94 @SuppressWarnings("serial")
95 @Before
96 public void setUp() throws Exception {
97 this.servletConfig = this.mockery.mock(ServletConfig.class);
98 this.servletContext = this.mockery.mock(ServletContext.class);
99 this.response = this.mockery.mock(HttpServletResponse.class);
100 this.request = this.mockery.mock(HttpServletRequest.class);
101
102
103 this.mockery.checking(new Expectations() {
104 {
105
106 this.allowing(ReverseProxyServletTest.this.servletConfig)
107 .getInitParameter(USER_ID);
108 this.will(returnValue("userID"));
109 this.allowing(ReverseProxyServletTest.this.servletConfig)
110 .getInitParameter(USER_PASSWORD);
111 this.will(returnValue("passID"));
112
113 this.allowing(ReverseProxyServletTest.this.servletConfig)
114 .getInitParameter(FORCE_XML_MIME);
115 this.will(returnValue("true"));
116 this.oneOf(ReverseProxyServletTest.this.servletConfig)
117 .getInitParameter(ALLOWED_HOSTS);
118 this.will(returnValue(SERVERNAME_DOESNOTEXIST + ";"
119 + SERVERNAME));
120 this.allowing(ReverseProxyServletTest.this.servletConfig)
121 .getInitParameter("featureInfoType");
122 this.will(returnValue(""));
123 }
124 });
125
126
127 this.pxyServlet = new ReverseProxyServlet() {
128
129 @Override
130 public ServletConfig getServletConfig() {
131 return ReverseProxyServletTest.this.servletConfig;
132 }
133
134
135 @Override
136 public ServletContext getServletContext() {
137 return ReverseProxyServletTest.this.servletContext;
138 }
139 };
140 try {
141 this.pxyServlet.init(this.servletConfig);
142 } catch (final ServletException e) {
143 fail("Servlet Exception voor init() in test setup. "
144 + e.getLocalizedMessage());
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157 public void testDoGetHttpServletRequestHttpServletResponse1()
158 throws IOException {
159
160 this.mockery.checking(new Expectations() {
161 {
162 this.oneOf(ReverseProxyServletTest.this.request)
163 .getQueryString();
164 this.will(returnValue(SERVERNAME_URL));
165 this.allowing(ReverseProxyServletTest.this.response)
166 .setContentType("text/xml");
167 this.oneOf(ReverseProxyServletTest.this.response)
168 .setContentLength((this.with(greaterThan(1))));
169 this.oneOf(ReverseProxyServletTest.this.response).getWriter();
170 this.will(returnValue((new PrintWriter(System.out))));
171 this.oneOf(ReverseProxyServletTest.this.response).flushBuffer();
172 }
173 });
174 try {
175 this.pxyServlet.doGet(this.request, this.response);
176 } catch (final ServletException e) {
177 fail("Servlet Exception voor doGet test. (#1)" + e);
178 }
179 }
180
181
182
183
184
185
186
187
188
189
190 public void testDoGetHttpServletRequestHttpServletResponse2()
191 throws IOException {
192
193 this.mockery.checking(new Expectations() {
194 {
195 this.oneOf(ReverseProxyServletTest.this.request)
196 .getQueryString();
197 this.will(returnValue(INVALID_URL));
198 }
199 });
200 try {
201 this.pxyServlet.doGet(this.request, this.response);
202 fail("Dit is een overwachte failure. (#2)");
203 } catch (final ServletException e) {
204
205 assertEquals(
206 "javax.servlet.ServletException: only HTTP(S) protocol supported",
207 e.getMessage());
208
209
210 }
211 }
212
213
214
215
216
217
218
219
220
221 public void testDoGetHttpServletRequestHttpServletResponse3()
222 throws IOException {
223
224 this.mockery.checking(new Expectations() {
225 {
226 this.oneOf(ReverseProxyServletTest.this.request)
227 .getQueryString();
228 this.will(returnValue(SERVERNAME_DOESNOTEXIST_URL));
229
230
231
232 this.oneOf(ReverseProxyServletTest.this.response).sendError(
233 SC_BAD_GATEWAY, "HTTP/1.1 502 Bad Gateway");
234 }
235 });
236 try {
237 this.pxyServlet.doGet(this.request, this.response);
238 } catch (final ServletException e) {
239 fail("(#3)" + e.getMessage());
240 }
241 }
242
243
244
245
246
247
248
249
250
251 public void testDoGetHttpServletRequestHttpServletResponse4()
252 throws IOException {
253
254 this.mockery.checking(new Expectations() {
255 {
256 this.oneOf(ReverseProxyServletTest.this.request)
257 .getQueryString();
258 this.will(returnValue(UNALLOWEDSERVERNAME_URL));
259 this.oneOf(ReverseProxyServletTest.this.response).sendError(
260 SC_FORBIDDEN,
261 UNALLOWEDSERVERNAME_URL
262 + " is not in the list of allowed servers");
263 this.oneOf(ReverseProxyServletTest.this.response).flushBuffer();
264 }
265 });
266 try {
267 this.pxyServlet.doGet(this.request, this.response);
268 } catch (final ServletException e) {
269 fail("Servlet Exception voor doGet test. (#4)" + e);
270 }
271 }
272
273
274
275
276
277
278 @Test
279 public final void testInitServletConfig() {
280
281 this.mockery.checking(new Expectations() {
282 {
283 this.oneOf(ReverseProxyServletTest.this.servletConfig)
284 .getInitParameter(ALLOWED_HOSTS);
285 this.will(returnValue(null));
286 }
287 });
288 try {
289
290 this.pxyServlet.init(this.servletConfig);
291 fail("Een overwachte fail conditie is opgetreden. (#2)");
292 } catch (final ServletException e) {
293 assertSame(ERR_MSG_MISSING_CONFIG, e.getMessage());
294 }
295 }
296
297
298
299
300
301
302
303
304 @SuppressWarnings("javadoc")
305
306 @Test
307 public void testCheckUrlAllowed() {
308
309 try {
310
311
312 final Method method = this.pxyServlet.getClass().getSuperclass()
313 .getDeclaredMethod("checkUrlAllowed", String.class);
314 method.setAccessible(true);
315 assertFalse((Boolean) method.invoke(this.pxyServlet,
316 UNALLOWEDSERVERNAME));
317 assertTrue((Boolean) method.invoke(this.pxyServlet, SERVERNAME_URL));
318 } catch (final SecurityException e) {
319 fail("Reflection call getDeclaredMethod failed:" + e);
320 } catch (final NoSuchMethodException e) {
321 fail("Reflection call getDeclaredMethod failed:" + e);
322 } catch (final IllegalArgumentException e) {
323 fail("Reflection call invoke failed:" + e);
324 } catch (final IllegalAccessException e) {
325 fail("Reflection call invoke failed:" + e);
326 } catch (final InvocationTargetException e) {
327 fail("Reflection call invoke failed:" + e);
328 }
329 }
330 }