@Before public void init() throws Exception { _handler = new RewriteHandler(); _handler.setServer(_server); _handler.setHandler( new AbstractHandler() { @Override public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(201); request.setAttribute("target", target); request.setAttribute("URI", request.getRequestURI()); request.setAttribute("info", request.getPathInfo()); } }); _handler.start(); _rule1 = new RewritePatternRule(); _rule1.setPattern("/aaa/*"); _rule1.setReplacement("/bbb"); _rule2 = new RewritePatternRule(); _rule2.setPattern("/bbb/*"); _rule2.setReplacement("/ccc"); _rule3 = new RewritePatternRule(); _rule3.setPattern("/ccc/*"); _rule3.setReplacement("/ddd"); _rule4 = new RewriteRegexRule(); _rule4.setRegex("/xxx/(.*)"); _rule4.setReplacement("/$1/zzz"); _handler.setRules(new Rule[] {_rule1, _rule2, _rule3, _rule4}); start(false); }
@Test public void test() throws Exception { _response.setStatus(200); _request.setHandled(false); _handler.setOriginalPathAttribute("/before"); _handler.setRewriteRequestURI(false); _handler.setRewritePathInfo(false); _request.setRequestURI("/foo/bar"); _request.setPathInfo("/foo/bar"); _handler.handle("/foo/bar", _request, _request, _response); assertEquals(201, _response.getStatus()); assertEquals("/foo/bar", _request.getAttribute("target")); assertEquals("/foo/bar", _request.getAttribute("URI")); assertEquals("/foo/bar", _request.getAttribute("info")); assertEquals(null, _request.getAttribute("before")); _response.setStatus(200); _request.setHandled(false); _handler.setOriginalPathAttribute(null); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); _handler.handle("/aaa/bar", _request, _request, _response); assertEquals(201, _response.getStatus()); assertEquals("/ddd/bar", _request.getAttribute("target")); assertEquals("/aaa/bar", _request.getAttribute("URI")); assertEquals("/aaa/bar", _request.getAttribute("info")); assertEquals(null, _request.getAttribute("before")); _response.setStatus(200); _request.setHandled(false); _handler.setOriginalPathAttribute("before"); _handler.setRewriteRequestURI(true); _handler.setRewritePathInfo(true); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); _handler.handle("/aaa/bar", _request, _request, _response); assertEquals(201, _response.getStatus()); assertEquals("/ddd/bar", _request.getAttribute("target")); assertEquals("/ddd/bar", _request.getAttribute("URI")); assertEquals("/ddd/bar", _request.getAttribute("info")); assertEquals("/aaa/bar", _request.getAttribute("before")); _response.setStatus(200); _request.setHandled(false); _rule2.setTerminating(true); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); _handler.handle("/aaa/bar", _request, _request, _response); assertEquals(201, _response.getStatus()); assertEquals("/ccc/bar", _request.getAttribute("target")); assertEquals("/ccc/bar", _request.getAttribute("URI")); assertEquals("/ccc/bar", _request.getAttribute("info")); assertEquals("/aaa/bar", _request.getAttribute("before")); _response.setStatus(200); _request.setHandled(false); _rule2.setHandling(true); _request.setAttribute("before", null); _request.setAttribute("target", null); _request.setAttribute("URI", null); _request.setAttribute("info", null); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); _handler.handle("/aaa/bar", _request, _request, _response); assertEquals(200, _response.getStatus()); assertEquals(null, _request.getAttribute("target")); assertEquals(null, _request.getAttribute("URI")); assertEquals(null, _request.getAttribute("info")); assertEquals("/aaa/bar", _request.getAttribute("before")); assertTrue(_request.isHandled()); }