use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testMultipleIdenticalRewriteOutputRules.
@Test
public void testMultipleIdenticalRewriteOutputRules() throws IOException, URISyntaxException {
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(environment, request, response);
UrlRewriteProcessor processor = new UrlRewriteProcessor();
UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load("xml", getTestResourceReader("rewrite-with-same-rules.xml", "UTF-8"));
processor.initialize(environment, config);
Template inputUrl = Parser.parseLiteral("scheme://input-mock-host:42/test-input-path");
Template outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, null);
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
// Should always pick the first one.
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-1://output-mock-host-1:42/test-input-path"));
inputUrl = Parser.parseLiteral("mock-scheme://input-mock-host:42/no-query");
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, null);
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("mock-scheme://output-mock-host-3:42/no-query"));
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-4");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("mock-scheme://output-mock-host-4:42/no-query"));
processor.destroy();
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testIdenticalRewriteOutputRulesWithScopes.
@Test
public void testIdenticalRewriteOutputRulesWithScopes() throws IOException, URISyntaxException {
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
ArrayList<String> roles = new ArrayList<>();
roles.add("service-1");
EasyMock.expect(environment.resolve("service.role")).andReturn(roles).anyTimes();
EasyMock.replay(environment, request, response);
UrlRewriteProcessor processor = new UrlRewriteProcessor();
UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load("xml", getTestResourceReader("rewrite-with-same-rules-different-scope.xml", "UTF-8"));
processor.initialize(environment, config);
Template inputUrl = Parser.parseLiteral("scheme://input-mock-host:42/test-input-path");
Template outputUrl = processor.rewrite(environment, inputUrl, UrlRewriter.Direction.OUT, null);
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-2://output-mock-host-2:42/test-input-path"));
inputUrl = Parser.parseLiteral("mock-scheme://input-mock-host:42/no-query");
outputUrl = processor.rewrite(environment, inputUrl, UrlRewriter.Direction.OUT, null);
roles.remove(0);
roles.add("service-2");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("mock-scheme://output-mock-host-5:42/no-query"));
outputUrl = processor.rewrite(environment, inputUrl, UrlRewriter.Direction.OUT, "service-2/test-rule-4");
// no scope information should pick the first one
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("mock-scheme://output-mock-host-4:42/no-query"));
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "service-2/test-rule-4");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("mock-scheme://output-mock-host-4:42/no-query"));
// Test the IN direction
inputUrl = Parser.parseLiteral("scheme://input-mock-host:42/test-input-path");
outputUrl = processor.rewrite(environment, inputUrl, UrlRewriter.Direction.IN, null);
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("input-mock-scheme-2://input-mock-host-2:42/test-input-path"));
processor.destroy();
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class HostmapFunctionProcessorTest method testQueryToPathRewriteWithFunction.
@Test
public void testQueryToPathRewriteWithFunction() throws Exception {
URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hdfs-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-internal-host")).anyTimes();
EasyMock.replay(environment, resolver);
UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
rule.pattern("{*}://{host}:{*}/{**}?{qp1}&{qp2}&{**}");
UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
rewrite.template("{*}://test-static-host:{*}/{qp1}/{qp2}/{**}?server={$hostmap(host)}&{**}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
Template input = Parser.parseLiteral("test-scheme://test-external-host:42/test-path/test-file?qp1=qp1-val&qp2=qp2-val&test-name-1=test-value-1&test-name-2=test-value-2");
Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-rule");
// System.out.println( output );
assertThat(output, notNullValue());
assertThat(output.getHost().getFirstValue().getPattern(), is("test-static-host"));
assertThat(output.getQuery().get("server").getFirstValue().getPattern(), is("test-external-host"));
assertThat(output.getQuery().get("server").getValues().size(), is(1));
assertThat(output.getQuery().get("test-name-1").getFirstValue().getPattern(), is("test-value-1"));
assertThat(output.getQuery().get("test-name-1").getValues().size(), is(1));
assertThat(output.getQuery().get("test-name-2").getFirstValue().getPattern(), is("test-value-2"));
assertThat(output.getQuery().get("test-name-2").getValues().size(), is(1));
assertThat(output.getQuery().size(), is(3));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class HostmapFunctionProcessorTest method testEmptyHostmapUseCase.
@Test
public void testEmptyHostmapUseCase() throws Exception {
URL configUrl = TestUtils.getResourceUrl(this.getClass(), "empty-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("{*}://{$hostmap(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("test-inbound-host"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class HostmapFunctionProcessorTest method testUnmappedUseCase.
@Test
public void testUnmappedUseCase() 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-unmapped-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("{*}://{$hostmap(host)}:{*}/{**}?{**}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
Template input = Parser.parseLiteral("test-scheme://test-inbound-unmapped-host:42/test-path/test-file?test-name-1=test-value-1&test-name-2=test-value-2");
Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
// System.out.println( output );
assertThat(output, notNullValue());
assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-unmapped-host"));
}
Aggregations