Search in sources :

Example 16 with Template

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 17 with Template

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 18 with Template

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));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 19 with Template

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 20 with Template

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Aggregations

Template (org.apache.knox.gateway.util.urltemplate.Template)50 Test (org.junit.Test)23 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)15 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 Capture (org.easymock.Capture)5 Host (org.apache.knox.gateway.util.urltemplate.Host)4 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)4 Query (org.apache.knox.gateway.util.urltemplate.Query)4 URI (java.net.URI)3 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3