@Test public void testServiceRegistryFunctionsOnJsonRequestBody() throws Exception { Map<String, String> initParams = new HashMap<String, String>(); initParams.put("request.body", "oozie-conf"); setUp("test-user", initParams); String input = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-input-body.json", "UTF-8"); String expect = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-expect-body.json", "UTF-8"); // Setup the server side request/response interaction. interaction .expect() .method("PUT") .requestUrl("http://test-host:42/test-path") .contentType("application/json") .characterEncoding("UTF-8") .content(expect, Charset.forName("UTF-8")); interaction.respond().status(200); interactions.add(interaction); request.setMethod("PUT"); request.setURI("/test-path"); request.setVersion("HTTP/1.1"); request.setHeader("Host", "test-host:42"); request.setContentType("application/json; charset=UTF-8"); request.setContent(input); response.parse(server.getResponses(request.generate())); // Test the results. assertThat(response.getStatus(), Is.is(200)); }
@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")); }
@Test public void testNamesWithDots() throws IOException { InputStream stream = TestUtils.getResourceStream(this.getClass(), "dotted-field-name.json"); String input = IOUtils.toString(stream, Charset.forName("UTF-8")); // System.out.println( "INPUT=" + input ); UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("test-filter"); UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("application/json"); // NOTE: The field names are rewritten first so the values rules need to match the rewritten // name. contentConfig.addApply("$.name<testField>", "test-rule"); contentConfig.addApply("$.name<test_field>", "test-rule"); contentConfig.addApply("$.name<test-field>", "test-rule"); contentConfig.addApply("$['name<test.field>']", "test-rule"); JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig); String output = IOUtils.toString(filter); // System.out.println( "OUTPUT=" + output ); JsonAssert.with(output) .assertThat("$['name<testField>']", is("value:test-rule<testField value>")); JsonAssert.with(output) .assertThat("$['name<test_field>']", is("value:test-rule<test_field value>")); JsonAssert.with(output) .assertThat("$['name<test-field>']", is("value:test-rule<test-field value>")); JsonAssert.with(output) .assertThat("$['name<test.field>']", is("value:test-rule<test.field value>")); }
@Test public void testBasicUseCase() throws Exception { URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt"); HostMapper hm = EasyMock.createNiceMock(HostMapper.class); EasyMock.expect(hm.resolveInboundHostName("test-inbound-host")) .andReturn("test-inbound-rewritten-host") .anyTimes(); HostMapperService hms = EasyMock.createNiceMock(HostMapperService.class); GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class); EasyMock.expect(gatewayServices.getService(GatewayServices.HOST_MAPPING_SERVICE)) .andReturn(hms) .anyTimes(); UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class); EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)) .andReturn(gatewayServices) .anyTimes(); EasyMock.expect(environment.resolve("cluster.name")) .andReturn(Arrays.asList("test-cluster-name")) .anyTimes(); 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(gatewayServices, hm, hms, 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-rewritten-host")); }
@Test public void testUnscopedStreaming() throws IOException { InputStream stream = TestUtils.getResourceStream(this.getClass(), "simple-values.json"); String input = IOUtils.toString(stream, Charset.forName("UTF-8")); // System.out.println( "INPUT=" + input ); UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter=1"); UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json"); UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply("$['test-str']", "test-rule"); JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig); String output = IOUtils.toString(filter); // System.out.println( "OUTPUT=" + output ); JsonAssert.with(output).assertThat("name<test-str>", is("value:null<text>")); }
@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)); }
@Test public void testBufferedApply() throws IOException { InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties.json"); String input = IOUtils.toString(stream, Charset.forName("UTF-8")); // System.out.println( "INPUT=" + input ); UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1"); UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json"); UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$.name<properties>.*.name<property>"); UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.name<property-value>", "test-rule"); // UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) ); JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig); String output = IOUtils.toString(filter); // System.out.println( "OUTPUT=" + output ); JsonAssert.with(output) .assertThat("name<properties>[0].name<property>.name<property-name>", is("test-name-1")); JsonAssert.with(output) .assertThat( "name<properties>[0].name<property>.name<property-value>", is("value:test-rule<test-value-1>")); JsonAssert.with(output) .assertThat("name<properties>[1].name<property>.name<property-name>", is("test-name-2")); JsonAssert.with(output) .assertThat( "name<properties>[1].name<property>.name<property-value>", is("value:test-rule<test-value-2>")); JsonAssert.with(output) .assertThat("name<properties>[2].name<property>.name<property-name>", is("test-name-3")); JsonAssert.with(output) .assertThat( "name<properties>[2].name<property>.name<property-value>", is("value:test-rule<test-value-3>")); }