use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.
the class HostmapFunctionProcessorTest method testInvalidFunctionNameUseCase.
@Test
public void testInvalidFunctionNameUseCase() throws Exception {
URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt");
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")).andReturn(configUrl).anyTimes();
Resolver resolver = EasyMock.createNiceMock(Resolver.class);
EasyMock.expect(resolver.resolve("host")).andReturn(Arrays.asList("test-inbound-host")).anyTimes();
EasyMock.replay(environment, resolver);
UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
rule.pattern("{*}://{host}:{*}/{**}?{**}");
UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
rewrite.template("{*}://{$invalid-function(host)}:{*}/{**}?{**}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
// System.out.println( output );
assertThat(output, notNullValue());
assertThat(output.getHost().getFirstValue().getPattern(), is("$invalid-function(host)"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.
the class InboundUrlFunctionProcessorTest method testQueryParam.
@Test
public void testQueryParam() throws Exception {
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
Resolver resolver = EasyMock.createNiceMock(Resolver.class);
EasyMock.expect(resolver.resolve("query.param.host")).andReturn(Lists.newArrayList("http://foo:50075")).anyTimes();
EasyMock.replay(gatewayServices, environment, resolver);
UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
rule.pattern("{*}://{*}:{*}/{**}/?{**}");
UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
rewrite.template("{$inboundurl[host]}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
Template input = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-location");
assertThat(output.toString(), is("http://foo:50075"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.
the class UrlRewriteResponseTest method testResolve.
@Test
public void testResolve() throws Exception {
UrlRewriteProcessor rewriter = EasyMock.createNiceMock(UrlRewriteProcessor.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getServletContextName()).andReturn("test-cluster-name").anyTimes();
EasyMock.expect(context.getInitParameter("test-init-param-name")).andReturn("test-init-param-value").anyTimes();
EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriter).anyTimes();
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("test-filter-init-param-name")).andReturn("test-filter-init-param-value").anyTimes();
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(rewriter, context, config, request, response);
UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);
List<String> names = rewriteResponse.resolve("test-filter-init-param-name");
assertThat(names.size(), is(1));
assertThat(names.get(0), is("test-filter-init-param-value"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.
the class UrlRewriteResponseTest method testResolveGatewayParams.
@Test
public void testResolveGatewayParams() throws Exception {
UrlRewriteProcessor rewriter = EasyMock.createNiceMock(UrlRewriteProcessor.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriter).anyTimes();
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getScheme()).andReturn("mock-scheme").anyTimes();
EasyMock.expect(request.getLocalName()).andReturn("mock-host").anyTimes();
EasyMock.expect(request.getLocalPort()).andReturn(42).anyTimes();
EasyMock.expect(request.getContextPath()).andReturn("/mock-path").anyTimes();
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(rewriter, context, config, request, response);
UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);
List<String> url = rewriteResponse.resolve("gateway.url");
assertThat(url, hasItems(new String[] { "mock-scheme://mock-host:42/mock-path" }));
List<String> scheme = rewriteResponse.resolve("gateway.scheme");
assertThat(scheme, hasItems(new String[] { "mock-scheme" }));
List<String> host = rewriteResponse.resolve("gateway.host");
assertThat(host, hasItems(new String[] { "mock-host" }));
List<String> port = rewriteResponse.resolve("gateway.port");
assertThat(port, hasItems(new String[] { "42" }));
List<String> addr = rewriteResponse.resolve("gateway.addr");
assertThat(addr, hasItems(new String[] { "mock-host:42" }));
List<String> address = rewriteResponse.resolve("gateway.addr");
assertThat(address, hasItems(new String[] { "mock-host:42" }));
List<String> path = rewriteResponse.resolve("gateway.path");
assertThat(path, hasItems(new String[] { "/mock-path" }));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.
the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralTemplateValuesBugKnox394.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralTemplateValuesBugKnox394() throws Exception {
LOG_ENTER();
// This is a unique part of this test.
String testResource = "oozie-request-with-var.xml";
// Mock out the service url registry which is required for several url rewrite functions to work.
ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
// Mock out the gateway services registry which is required for several url rewrite functions to work.
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
rulesReader.close();
rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
InputStream stream = rewriteRequest.getInputStream();
Document document = XmlUtils.readXml(stream);
assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${appPath}/workflow.xml")));
LOG_EXIT();
}
Aggregations