You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by mo...@apache.org on 2017/09/01 13:17:16 UTC
[18/64] [partial] knox git commit: KNOX-998 - Refactoring save 1
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReaderTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReaderTest.java b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReaderTest.java
deleted file mode 100644
index 82948a3..0000000
--- a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReaderTest.java
+++ /dev/null
@@ -1,1003 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.gateway.filter.rewrite.impl.xml;
-
-import org.apache.commons.digester3.Digester;
-import org.apache.commons.digester3.ExtendedBaseRules;
-import org.apache.commons.digester3.binder.DigesterLoader;
-import org.apache.commons.io.IOUtils;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptorFactory;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteStepDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteStepFlow;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteCheckDescriptorExt;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteControlDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptorExt;
-import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
-import org.apache.hadoop.test.TestUtils;
-import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
-import org.xml.sax.SAXException;
-import org.xmlmatchers.namespace.SimpleNamespaceContext;
-
-import javax.xml.namespace.QName;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.stream.XMLStreamException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.StringReader;
-import java.nio.charset.Charset;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
-import static org.hamcrest.CoreMatchers.hasItem;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.fail;
-import static org.xmlmatchers.XmlMatchers.hasXPath;
-import static org.xmlmatchers.transform.XmlConverters.the;
-
-public class XmlFilterReaderTest {
-
- public static class NoopXmlFilterReader extends XmlFilterReader {
- public NoopXmlFilterReader( Reader reader, UrlRewriteFilterContentDescriptor config ) throws IOException, ParserConfigurationException, XMLStreamException {
- super( reader, config );
- }
-
- @Override
- protected String filterText( QName elementName, String text, String ruleName ) {
- return text;
- }
-
- @Override
- protected String filterAttribute( QName elementName, QName attributeName, String attributeValue, String ruleName ) {
- return attributeValue;
- }
- }
-
- public static class MapXmlFilterReader extends XmlFilterReader {
- private Map<String,String> map;
-
- public MapXmlFilterReader( Reader reader, Map<String,String> map ) throws IOException, ParserConfigurationException, XMLStreamException {
- super( reader, null );
- this.map = map;
- }
-
- @Override
- protected String filterAttribute( QName elementName, QName attributeName, String attributeValue, String ruleName ) {
- return map.get( attributeValue.trim() );
- }
-
- @Override
- protected String filterText( QName elementName, String text, String ruleName ) {
- return map.get( text.trim() );
- }
- }
-
- @Test
- public void testSimple() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<root/>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- assertThat( the( outputHtml ), hasXPath( "/root" ) );
- }
-
- @Test
- public void testSimpleStreaming() throws IOException, ParserConfigurationException, XMLStreamException {
- UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
- UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
-
- String inputXml = "<root/>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- assertThat( the( outputHtml ), hasXPath( "/root" ) );
- }
-
-// @Test
-// public void testSimpleScoped() throws IOException, ParserConfigurationException, XMLStreamException {
-// UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
-// UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
-// UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
-//
-// String inputXml = "<root/>";
-// StringReader inputReader = new StringReader( inputXml );
-// XmlStaxFilterReader filterReader = new NoopXmlFilterReader( inputReader );
-// String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
-// assertThat( the( outputHtml ), hasXPath( "/root" ) );
-// }
-
- @Test
- public void testSimpleBuffered() throws IOException, ParserConfigurationException, XMLStreamException {
- UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
- UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
- UrlRewriteFilterBufferDescriptor scopeConfig = contentConfig.addBuffer( "/root" );
-
- String input = "<root/>";
- //System.out.println( "INPUT=" + input );
- StringReader inputReader = new StringReader( input );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
- String output = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( "OUTPUT=" + output );
- assertThat( the( output ), hasXPath( "/root" ) );
- }
-
- @Test
- public void testSimpleNested() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<root><child1><child11/><child12/></child1><child2><child21/><child22/></child2></root>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- assertThat( the( outputHtml ), hasXPath( "/root" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child1" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child1/child11" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child1/child12" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child2" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child2/child21" ) );
- assertThat( the( outputHtml ), hasXPath( "/root/child2/child22" ) );
- }
-
- @Test
- public void testSimpleWithNamespace() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<ns:root xmlns:ns='http://hortonworks.com/xml/ns'></ns:root>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
-
- //System.out.println( outputHtml );
- SimpleNamespaceContext ns = new SimpleNamespaceContext();
- ns.bind( "ns", "http://hortonworks.com/xml/ns" );
- assertThat( the( outputHtml ), hasXPath( "/ns:root", ns ) );
- }
-
- @Test
- public void testSimpleTextNode() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<root>text</root>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( "OUTPUT=" + outputXml );
- assertThat( the( outputXml ), hasXPath( "/root/text()", equalTo( "text" ) ) );
- }
-
- @Test
- public void testSimpleAttribute() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<root name='value'/>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputHtml );
- assertThat( the( outputXml ), hasXPath( "/root/@name", equalTo( "value" ) ) );
- }
-
- @Test
- public void testSimpleTextNodeBuffered() throws IOException, ParserConfigurationException, XMLStreamException {
- UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
- UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
- UrlRewriteFilterBufferDescriptor scopeConfig = contentConfig.addBuffer( "/root" );
-
- String inputXml = "<root>text</root>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputHtml );
- assertThat( the( outputHtml ), hasXPath( "/root/text()", equalTo( "text" ) ) );
- }
-
- @Test
- public void testSimpleAttributeBuffered() throws IOException, ParserConfigurationException, XMLStreamException {
- UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
- UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
- UrlRewriteFilterBufferDescriptor scopeConfig = contentConfig.addBuffer( "/root" );
-
- String inputXml = "<root name='value'/>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputHtml );
- assertThat( the( outputHtml ), hasXPath( "/root/@name", equalTo( "value" ) ) );
- }
-
- @Test
- public void testMappedText() throws IOException, ParserConfigurationException, XMLStreamException {
- Map<String,String> map = new HashMap<>();
- map.put( "input-text", "output-text" );
- String inputXml = "<root>input-text</root>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new MapXmlFilterReader( inputReader, map );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputHtml );
- assertThat( the( outputHtml ), hasXPath( "/root/text()", equalTo( "output-text" ) ) );
- }
-
- @Test
- public void testMappedAttribute() throws IOException, ParserConfigurationException, XMLStreamException {
- Map<String,String> map = new HashMap<>();
- map.put( "input-text", "output-text" );
- String inputXml = "<root attribute='input-text'/>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new MapXmlFilterReader( inputReader, map );
- String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputHtml );
- assertThat( the( outputHtml ), hasXPath( "/root/@attribute", equalTo( "output-text" ) ) );
- }
-
- @Test
- public void testCombined() throws IOException, ParserConfigurationException, XMLStreamException {
- Map<String,String> map = new HashMap<>();
- map.put( "attr1-input", "attr1-output" );
- map.put( "attr2-input", "attr2-output" );
- map.put( "attr3-input", "attr3-output" );
- map.put( "attr4-input", "attr4-output" );
- map.put( "attr5-input", "attr5-output" );
- map.put( "attr6-input", "attr6-output" );
- map.put( "attr7-input", "attr7-output" );
- map.put( "root-input1", "root-output1" );
- map.put( "root-input2", "root-output2" );
- map.put( "root-input3", "root-output3" );
- map.put( "child1-input", "child1-output" );
- map.put( "child2-input", "child2-output" );
-
- String inputXml =
- "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
- "<!-- Comment -->\n" +
- "<ns1:root xmlns:ns1='http://hortonworks.com/xml/ns1' attr1='attr1-input' ns1:attr2='attr2-input'>\n" +
- " root-input1\n" +
- " <child1 attr3='attr3-input' ns1:attr4='attr4-input'>\n" +
- " child1-input\n" +
- " </child1>\n" +
- " root-input2\n" +
- " <ns2:child2 xmlns:ns2='http://hortonworks.com/xml/ns2' attr5='attr5-input' ns1:attr6='attr6-input' ns2:attr7='attr7-input'>\n" +
- " child2-input\n" +
- " </ns2:child2>\n" +
- " root-input3\n" +
- "</ns1:root>";
- //System.out.println( inputXml );
-
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new MapXmlFilterReader( inputReader, map );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( outputXml );
- //System.out.flush();
-
- SimpleNamespaceContext ns = new SimpleNamespaceContext();
- ns.bind( "n1", "http://hortonworks.com/xml/ns1" );
- ns.bind( "n2", "http://hortonworks.com/xml/ns2" );
-
- assertThat( the( outputXml ), hasXPath( "/n1:root", ns ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/@attr1", ns, equalTo( "attr1-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/@n1:attr2", ns, equalTo( "attr2-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/text()[1]", ns, equalTo( "root-output1" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/text()[2]", ns, equalTo( "root-output2" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/text()[3]", ns, equalTo( "root-output3" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/child1", ns ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/child1/@attr3", ns, equalTo( "attr3-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/child1/@n1:attr4", ns, equalTo( "attr4-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/child1/text()", ns, equalTo( "child1-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2", ns ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@attr5", ns, equalTo( "attr5-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@n1:attr6", ns, equalTo( "attr6-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@n2:attr7", ns, equalTo( "attr7-output" ) ) );
- assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/text()", ns, equalTo( "child2-output" ) ) );
- }
-
- public static class XmlRewriteRulesDescriptorDigesterTest {
-
- private static DigesterLoader loader = newLoader( new XmlRewriteRulesDigester() );
- private static Digester digester = loader.newDigester( new ExtendedBaseRules() );
-
- @Before
- public void setupTest() {
- digester.setValidating( false );
- }
-
- @Test
- public void testRuleParsing() throws IOException, SAXException {
- Reader reader = new StringReader( "<rules/>" );
- UrlRewriteRulesDescriptor config = digester.parse( reader );
- assertThat( config.getRules().isEmpty(), is( true ) );
-
- reader = new StringReader( "<rules><rule></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- UrlRewriteRuleDescriptor rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), nullValue() );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule name=\"test-name\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), is( "test-name" ) );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule scope=\"test-scope\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), nullValue() );
- assertThat( rule.scope(), is( "test-scope" ) );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule name=\"test-name\" scope=\"test-scope\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), is( "test-name" ) );
- assertThat( rule.scope(), is( "test-scope" ) );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule pattern=\"test-pattern\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), nullValue() );
- assertThat( rule.pattern(), is( "test-pattern" ) );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule dir=\"request\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), nullValue() );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions().size(), is( 1 ) );
- assertThat( rule.directions(), contains( UrlRewriter.Direction.IN ) );
- assertThat( rule.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule flow=\"all\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule, notNullValue() );
- assertThat( rule.name(), nullValue() );
- assertThat( rule.pattern(), nullValue() );
- assertThat( rule.directions(), nullValue() );
- assertThat( rule.flow(), Matchers.is( UrlRewriteStepFlow.ALL ) );
- }
-
- @Test
- public void testDirectionParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
-
- reader = new StringReader( "<rules><rule dir=\"request\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"Request\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"in\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"req\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"Req\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"REQ\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"inbound\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"Inbound\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"INBOUND\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"in\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"In\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"IN\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"i\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
- reader = new StringReader( "<rules><rule dir=\"I\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
-
-
- reader = new StringReader( "<rules><rule dir=\"response\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"Response\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"out\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"res\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"Res\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"RES\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"outbound\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"Outbound\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"OUTBOUND\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"out\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"Out\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"OUT\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"o\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
- reader = new StringReader( "<rules><rule dir=\"O\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.OUT ) );
-
-
- reader = new StringReader( "<rules><rule dir=\"request,response\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), hasItem( UrlRewriter.Direction.IN ) );
- assertThat( config.getRules().get( 0 ).directions(), hasItem( UrlRewriter.Direction.OUT ) );
- }
-
- @Test
- public void testFlowParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
-
- reader = new StringReader( "<rules><rule dir=\"request\"></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config.getRules().get( 0 ).directions(), contains( UrlRewriter.Direction.IN ) );
- }
-
- @Test
- public void testMatchParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
- UrlRewriteRuleDescriptor rule;
- UrlRewriteMatchDescriptorExt match;
- List<? extends UrlRewriteStepDescriptor> steps;
-
- reader = new StringReader( "<rules><rule><match></match></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- match = (UrlRewriteMatchDescriptorExt)rule.steps().get( 0 );
- assertThat( match, notNullValue() );
- //assertThat( match.type(), nullValue() );
- assertThat( match.operation(), nullValue() );
- assertThat( match.pattern(), nullValue() );
-
- reader = new StringReader( "<rules><rule><match type=\"test-type\" op=\"test-op\" pattern=\"test-pattern\"></match></rule></rules>" );
- config = digester.parse( reader );
- match = (UrlRewriteMatchDescriptorExt)config.getRules().get( 0 ).steps().get( 0 );
- //assertThat( match.type(), is("test-type") );
- assertThat( match.operation(), is( "test-op" ) );
- assertThat( match.pattern(), is( "test-pattern" ) );
-
- reader = new StringReader( "<rules><rule name=\"test\"><match><match pattern=\"test-pattern\"></match></match></rule></rules>" );
- config = digester.parse( reader );
- steps = ((UrlRewriteMatchDescriptor)config.getRule( "test" ).steps().get( 0 )).steps();
- assertThat( steps, notNullValue() );
- assertThat( steps.size(), is( 1 ) );
- assertThat( steps.get( 0 ), notNullValue() );
- match = (UrlRewriteMatchDescriptorExt)steps.get( 0 );
- assertThat( match.pattern(), is( "test-pattern" ) );
- }
-
- @Test
- public void testCheckParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
- UrlRewriteRuleDescriptor rule;
- List<UrlRewriteStepDescriptor> steps;
- UrlRewriteCheckDescriptorExt step;
-
- reader = new StringReader( "<rules><rule><check></check></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- step = (UrlRewriteCheckDescriptorExt)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- //assertThat( step.type(), nullValue() );
- assertThat( step.operation(), nullValue() );
- assertThat( step.input(), nullValue() );
- assertThat( step.value(), nullValue() );
-
- reader = new StringReader( "<rules><rule><check type=\"test-type\" op=\"test-op\" input=\"test-input\" value=\"test-value\"></check></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- step = (UrlRewriteCheckDescriptorExt)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- //assertThat( step.type(), is( "test-type" ) );
- assertThat( step.operation(), is( "test-op" ) );
- assertThat( step.input(), is( "test-input" ) );
- assertThat( step.value(), is( "test-value" ) );
- }
-
- @Test
- public void testActionParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
- UrlRewriteRuleDescriptor rule;
- UrlRewriteActionDescriptorBase step;
-
- reader = new StringReader( "<rules><rule><action></action></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- step = (UrlRewriteActionDescriptorBase)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- //assertThat( step.type(), nullValue() );
- assertThat( step.parameter(), nullValue() );
-
- reader = new StringReader( "<rules><rule><action type=\"test-type\" param=\"test-param\"></action></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- step = (UrlRewriteActionDescriptorBase)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- //assertThat( step.type(), is( "test-type" ) );
- assertThat( step.parameter(), is( "test-param" ) );
- }
-
- @Test
- public void testControlParsing() throws IOException, SAXException {
- Reader reader;
- UrlRewriteRulesDescriptor config;
- UrlRewriteRuleDescriptor rule;
- List<UrlRewriteStepDescriptor> steps;
-
- reader = new StringReader( "<rules><rule><control></control></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- UrlRewriteControlDescriptor step = (UrlRewriteControlDescriptor)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- assertThat( step.flow(), nullValue() );
-
- reader = new StringReader( "<rules><rule><control flow=\"or\"></control></rule></rules>" );
- config = digester.parse( reader );
- assertThat( config, notNullValue() );
- assertThat( config.getRules(), notNullValue() );
- assertThat( config.getRules().size(), is( 1 ) );
- rule = config.getRules().get( 0 );
- assertThat( rule.steps(), notNullValue() );
- assertThat( rule.steps().size(), is( 1 ) );
- step = (UrlRewriteControlDescriptor)rule.steps().get( 0 );
- assertThat( step, notNullValue() );
- assertThat( step.flow(), is( UrlRewriteStepFlow.OR ) );
- }
- }
-
- @Test
- public void testTagNameLetterCase() throws Exception {
- String inputXml = "<Root/>";
- StringReader inputReader = new StringReader( inputXml );
-
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- assertThat( the( outputXml ), hasXPath( "/Root" ) );
- }
-
- @Test
- public void testXmlWithHtmlTagNames() throws Exception {
- String inputXml = "<root><br><table name=\"table1\"/><table name=\"table2\"/></br></root>";
- StringReader inputReader = new StringReader( inputXml );
-
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- assertThat( the( outputXml ), hasXPath( "/root/br/table[1]/@name", equalTo( "table1" ) ) );
- assertThat( the( outputXml ), hasXPath( "/root/br/table[2]/@name", equalTo( "table2" ) ) );
- }
-
- @Test
- public void testStreamedApplyForElements() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-elements.xml" );
- 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/xml" );
- UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply( "/properties/property/value/text()", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/name/text()", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/value/text()", equalTo( "text:test-rule-2{test-value-1}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/name/text()", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/value/text()", equalTo( "text:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/name/text()", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/value/text()", equalTo( "text:test-rule-2{test-value-3}" ) ) );
- }
-
- @Test
- public void testStreamedApplyForElementsConfigShortcut() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-elements.xml" );
- 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/xml" );
- UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply( "/properties/property/value", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/name/text()", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/value/text()", equalTo( "text:test-rule-2{test-value-1}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/name/text()", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/value/text()", equalTo( "text:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/name/text()", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/value/text()", equalTo( "text:test-rule-2{test-value-3}" ) ) );
- }
-
- @Test
- public void testStreamedApplyForAttributes() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-attributes.xml" );
- 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/xml" );
- UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply( "/properties/property/@value", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/@name", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/@value", equalTo( "attr:test-rule-2{test-value-1}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@name", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@value", equalTo( "attr:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@name", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@value", equalTo( "attr:test-rule-2{test-value-3}" ) ) );
- }
-
- @Test
- public void testBufferedApplyForAttributes() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-attributes.xml" );
- 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/xml" );
- UrlRewriteFilterBufferDescriptor bufferconfig = contentConfig.addBuffer( "/properties/property" );
- UrlRewriteFilterApplyDescriptor applyConfig = bufferconfig.addApply( "@value", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/@name", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/@value", equalTo( "attr:test-rule-2{test-value-1}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@name", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@value", equalTo( "attr:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@name", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@value", equalTo( "attr:test-rule-2{test-value-3}" ) ) );
- }
-
- @Test
- public void testBufferedDetectApplyForElements() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-elements.xml" );
- 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/xml" );
- UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer( "/properties/property" );
- UrlRewriteFilterDetectDescriptor detectConfig = bufferConfig.addDetect( "name", "test-name-2" );
- UrlRewriteFilterApplyDescriptor applyConfig = detectConfig.addApply( "value", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/name/text()", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/value/text()", equalTo( "test-value-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/name/text()", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/value/text()", equalTo( "text:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/name/text()", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/value/text()", equalTo( "test-value-3" ) ) );
- }
-
- @Test
- public void testBufferedDetectApplyForAttributes() throws Exception {
- InputStream stream = TestUtils.getResourceStream( this.getClass(), "properties-attributes.xml" );
- 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/xml" );
- UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer( "/properties/property" );
- UrlRewriteFilterDetectDescriptor detectConfig = bufferConfig.addDetect( "@name", "test-name-2" );
- UrlRewriteFilterApplyDescriptor applyConfig = detectConfig.addApply( "@value", "test-rule-2" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- String output = IOUtils.toString( filter );
-
- //System.out.println( "OUTPUT=" + output );
-
- assertThat( the( output ), hasXPath( "/properties/property[1]/@name", equalTo( "test-name-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[1]/@value", equalTo( "test-value-1" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@name", equalTo( "test-name-2" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[2]/@value", equalTo( "attr:test-rule-2{test-value-2}" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@name", equalTo( "test-name-3" ) ) );
- assertThat( the( output ), hasXPath( "/properties/property[3]/@value", equalTo( "test-value-3" ) ) );
- }
-
- @Test
- public void testInvalidConfigShouldThrowException() throws Exception {
- String input = "<root url='http://mock-host:42/test-input-path-1'><url>http://mock-host:42/test-input-path-2</url></root>";
-
- //System.out.println( "INPUT=" + input );
-
- UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
- UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "*/xml" );
- contentConfig.addApply( "$.url", "test-rule" );
-
- //UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
-
- try {
- XmlFilterReader filter = new TestXmlFilterReader( new StringReader( input ), contentConfig );
- IOUtils.toString( filter );
- fail( "Should have thrown an IllegalArgumentException." );
- } catch ( IOException e ) {
- fail( "Should have thrown an IllegalArgumentException." );
- } catch ( IllegalArgumentException e ) {
- if(System.getProperty("java.vendor").contains("IBM")){
- assertThat( e.getMessage(), containsString( "Extra illegal tokens: 'url'" ) );
- }else {
- assertThat( e.getMessage(), containsString( "$.url" ) );
- }
- }
- }
-
- @Test
- public void testDefaultNamespace() throws IOException, ParserConfigurationException, XMLStreamException {
- String input = "<root xmlns=\"ns\"><node attribute=\"attr\">nodevalue</node></root>";
- StringReader inputReader = new StringReader( input );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String output = IOUtils.toString( filterReader );
- //check default namespace URI
- assertThat( the( output ), hasXPath( "/*/namespace::*[name()='']", equalTo( "ns" ) ) );
- assertThat( the( output ), hasXPath( "/*[namespace-uri()='ns' and name()='root']/*[namespace-uri()='ns' and name()='node']", equalTo( "nodevalue" ) ) );
- assertThat( the( output ), hasXPath( "/*[namespace-uri()='ns' and name()='root']/*[namespace-uri()='ns' and name()='node']/@attribute", equalTo( "attr" ) ) );
- }
-
- @Test
- public void testEscapeCharactersBugKnox616() throws Exception {
- String input, output;
- StringReader reader;
- XmlFilterReader filter;
-
- input = "<tag/>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( output, containsString( "<tag/>" ) );
-
- input = "<tag></tag>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( output, containsString( "<tag/>" ) );
-
- input = "<tag><</tag>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( the( output ), hasXPath( "/tag" ) );
- assertThat( output, containsString( "<tag><</tag>" ) );
-
- input = "<tag>&</tag>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( output, containsString( "<tag>&</tag>" ) );
-
- input = "<document><empty/><![CDATA[<xyz>wibble</xyz>]]></document>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( output, containsString( "<?xml version=\"1.0\" standalone=\"no\"?><document><empty/><![CDATA[<xyz>wibble</xyz>]]></document>" ));
-
- input="<?xml version=\"1.0\" standalone=\"no\"?>"+
-"<document>" +
-" <noempty test=\"a\"> </noempty>"+
-" <!-- This is the first comment -->"+
-" <empty/>"+
-" <![CDATA[<xyz>wibble</xyz>]]>"+
-" <here>"+
-" <moreempty/>"+
-" <!-- This is the second comment -->"+
-" <![CDATA[<xyz>noop</xyz>]]>"+
-" </here>"+
-"</document>";
- reader = new StringReader( input );
- filter = new NoopXmlFilterReader( reader, null );
- output = IOUtils.toString( filter );
- assertThat( output, containsString( "<?xml version=\"1.0\" standalone=\"no\"?><document> <noempty test=\"a\"> </noempty> <!-- This is the first comment --> <empty/> <![CDATA[<xyz>wibble</xyz>]]> <here> <moreempty/> <!-- This is the second comment --> <![CDATA[<xyz>noop</xyz>]]> </here></document>"));
- }
-
- @Test
- public void testSpecialTextNodeBugKnox394() throws IOException, ParserConfigurationException, XMLStreamException {
- String inputXml = "<tag>${oozieTemplateMarkup}</tag>";
- StringReader inputReader = new StringReader( inputXml );
- XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
- String outputXml = new String( IOUtils.toCharArray( filterReader ) );
- //System.out.println( "OUTPUT=" + outputXml );
- assertThat( the( outputXml ), hasXPath( "/tag/text()", equalTo( "${oozieTemplateMarkup}" ) ) );
- }
-
-
- private class TestXmlFilterReader extends XmlFilterReader {
-
- protected TestXmlFilterReader( Reader reader, UrlRewriteFilterContentDescriptor contentConfig ) throws IOException, ParserConfigurationException, XMLStreamException {
- super( reader, contentConfig );
- }
-
- @Override
- protected String filterAttribute( QName elementName, QName attributeName, String attributeValue, String ruleName ) {
- return "attr:" + ruleName + "{" + attributeValue + "}";
- }
-
- @Override
- protected String filterText( QName elementName, String text, String ruleName ) {
- return "text:" + ruleName + "{" + text + "}";
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporterTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporterTest.java b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporterTest.java
deleted file mode 100644
index 9ae5bd0..0000000
--- a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporterTest.java
+++ /dev/null
@@ -1,215 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.gateway.filter.rewrite.impl.xml;
-
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptorFactory;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteCheckDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteControlDescriptor;
-import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor;
-import org.junit.Test;
-import org.xmlmatchers.XmlMatchers;
-import org.xmlmatchers.transform.XmlConverters;
-
-import javax.xml.transform.Source;
-import java.io.IOException;
-import java.io.StringWriter;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-public class XmlUrlRewriteRulesExporterTest {
-
- @Test
- public void testEmptyRules() throws IOException {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String xml = writer.toString();
- assertThat( XmlConverters.the( xml ), XmlMatchers.hasXPath( "/rules" ) );
- }
-
- @Test
- public void testSingleNamedRule() throws IOException {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- rules.addRule( "first" ).scope( "test-scope" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "first" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@scope", is( "test-scope" ) ) );
- }
-
- @Test
- public void testMatchStep() throws Exception {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteRuleDescriptor rule = rules.addRule( "test-rule" ).pattern("test-pattern-rule");
- UrlRewriteMatchDescriptor match = rule.addStep( "match" );
- match.operation("test-operation").pattern( "test-pattern-step" ).flow( "all" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "test-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@pattern", is( "test-pattern-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule/match)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/@flow", is( "ALL" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/@oper", is( "test-operation" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/@pattern", is( "test-pattern-step" ) ) );
- }
-
- @Test
- public void testControlStep() throws Exception {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteRuleDescriptor rule = rules.addRule( "test-rule" );
- UrlRewriteControlDescriptor control = rule.addStep( "control" );
- control.flow( "or" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "test-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule/control)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control/@flow", is( "OR" ) ) );
- }
-
- @Test
- public void testCheckStep() throws Exception {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteRuleDescriptor rule = rules.addRule( "test-rule" );
- UrlRewriteCheckDescriptor step = rule.addStep( "check" );
- step.operation("test-operation").input("test-input").value("test-value").flow( "all" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "test-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/@oper", is("test-operation") ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/@input", is("test-input") ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/@value", is("test-value") ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/@flow", is("ALL") ) );
- }
-
- @Test
- public void testRewriteStep() throws Exception {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteRuleDescriptor rule = rules.addRule( "test-rule" );
- UrlRewriteActionRewriteDescriptorExt step = rule.addStep( "rewrite" );
- step.operation("test-operation").parameter( "test-param" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "test-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/rewrite" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/rewrite/@oper", is("test-operation") ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/rewrite/@template", is("test-param") ) );
- }
-
- @Test
- public void testNestedStep() throws Exception {
- UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
- UrlRewriteRuleDescriptor rule = rules.addRule( "test-rule" );
-
- UrlRewriteMatchDescriptor match = rule.addStep( "match" );
- UrlRewriteMatchDescriptor matchMatch = match.addStep( "match" );
- UrlRewriteCheckDescriptor matchCheck = match.addStep( "check" );
- UrlRewriteControlDescriptor matchControl = match.addStep( "control" );
- UrlRewriteActionDescriptor matchRewrite = match.addStep( "rewrite" );
-
- UrlRewriteCheckDescriptor check = rule.addStep( "check" );
- UrlRewriteMatchDescriptor checkMatch = check.addStep( "match" );
- UrlRewriteCheckDescriptor checkCheck = check.addStep( "check" );
- UrlRewriteControlDescriptor checkControl = check.addStep( "control" );
- UrlRewriteActionDescriptor checkRewrite = check.addStep( "rewrite" );
-
- UrlRewriteControlDescriptor control = rule.addStep( "control" );
- UrlRewriteMatchDescriptor controlMatch = control.addStep( "match" );
- UrlRewriteCheckDescriptor controlCheck = control.addStep( "check" );
- UrlRewriteControlDescriptor controlControl = control.addStep( "control" );
- UrlRewriteActionDescriptor controlRewrite = control.addStep( "rewrite" );
-
- UrlRewriteActionDescriptor rewrite = rule.addStep( "rewrite" );
-
- StringWriter writer = new StringWriter();
- UrlRewriteRulesDescriptorFactory.store( rules, "xml", writer );
-
- String str = writer.toString();
- //System.out.println( str );
- Source xml = XmlConverters.the( str );
- assertThat( xml, XmlMatchers.hasXPath( "/rules" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule" ) );
- assertThat( xml, XmlMatchers.hasXPath( "count(/rules/rule)", is( "1" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/@name", is( "test-rule" ) ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/match" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/check" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/control" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/match/rewrite" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/match" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/check" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/control" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/check/rewrite" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control/match" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control/check" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control/control" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/control/rewrite" ) );
- assertThat( xml, XmlMatchers.hasXPath( "/rules/rule/rewrite" ) );
- }
-
-}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/FrontendFunctionDescriptorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/FrontendFunctionDescriptorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/FrontendFunctionDescriptorTest.java
new file mode 100644
index 0000000..fde25f2
--- /dev/null
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/FrontendFunctionDescriptorTest.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.filter.rewrite.api;
+
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.fail;
+
+public class FrontendFunctionDescriptorTest {
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionDescriptor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof FrontendFunctionDescriptor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + FrontendFunctionDescriptor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ FrontendFunctionDescriptor descriptor = new FrontendFunctionDescriptor();
+ assertThat( descriptor.name(), is( "frontend" ) );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
new file mode 100644
index 0000000..ff1093b
--- /dev/null
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
@@ -0,0 +1,412 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.filter.rewrite.api;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.knox.gateway.util.urltemplate.Expander;
+import org.apache.knox.gateway.util.urltemplate.Matcher;
+import org.apache.knox.gateway.util.urltemplate.Parser;
+import org.apache.knox.gateway.util.urltemplate.Template;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+public class UrlRewriteProcessorTest {
+
+ private static URL getTestResourceUrl( String name ) throws FileNotFoundException {
+ name = UrlRewriteProcessorTest.class.getName().replaceAll( "\\.", "/" ) + "/" + name;
+ URL url = ClassLoader.getSystemResource( name );
+ if( url == null ) {
+ throw new FileNotFoundException( name );
+ }
+ return url;
+ }
+
+ private static InputStream getTestResourceStream( String name ) throws IOException {
+ URL url = getTestResourceUrl( name );
+ InputStream stream = url.openStream();
+ return stream;
+ }
+
+ private static Reader getTestResourceReader( String name, String charset ) throws IOException {
+ return new InputStreamReader( getTestResourceStream( name ), charset );
+ }
+
+ @Test
+ public void testBasicPathRewrite() 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.xml", "UTF-8" ) );
+ processor.initialize( environment, config );
+
+ Template inputUrl = Parser.parseLiteral( "test-scheme://test-host:1/test-input-path" );
+ Template outputUrl = processor.rewrite( null, 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( "test-scheme://test-host:1/test-output-path" ) );
+ processor.destroy();
+ }
+
+ @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();
+ }
+
+ @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();
+ }
+
+ @Test
+ public void testRewriteViaRuleNameWithAmbiguousRules() 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( "input-mock-scheme-1://input-mock-host-1:42/test-input-path" );
+ Template outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2" );
+
+ 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" ) );
+
+ outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-1" );
+
+ assertThat( "Expect rewrite to produce a new URL",
+ outputUrl, notNullValue() );
+ assertThat(
+ "Expect rewrite to contain the correct path.",
+ outputUrl.toString(), is( "output-mock-scheme-1://output-mock-host-1:42/test-input-path" ) );
+
+ processor.destroy();
+ }
+
+ @Test
+ public void testRewriteViaRuleWithComplexFlow() throws Exception {
+ 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.xml", "UTF-8" ) );
+ processor.initialize( environment, config );
+
+ Template inputUrl;
+ Template outputUrl;
+
+ inputUrl = Parser.parseLiteral( "test-scheme://test-host:777/test-path" );
+ outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.IN, "test-rule-with-complex-flow" );
+ assertThat(
+ "Expect rewrite to contain the correct path.",
+ outputUrl.toString(), is( "test-scheme-output://test-host-output:42/test-path-output/test-path" ) );
+
+ inputUrl = Parser.parseLiteral( "test-scheme://test-host:42/~/test-path" );
+ outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.IN, "test-rule-with-complex-flow" );
+ assertThat(
+ "Expect rewrite to contain the correct path.",
+ outputUrl.toString(), is( "test-scheme-output://test-host-output:777/test-path-output/test-home/test-path" ) );
+
+ processor.destroy();
+ }
+
+ @Test
+ public void testRewriteViaRuleWithWildcardTemplateAndOptionalQuery() throws Exception {
+ 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.xml", "UTF-8" ) );
+ processor.initialize( environment, config );
+
+ Template inputUrl;
+ Template outputUrl;
+
+ inputUrl = Parser.parseLiteral( "test-scheme-input://test-host-input:42/test-path-input-one/test-path-input-two?test-query-name=test-query-value" );
+ outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2" );
+
+ assertThat(
+ "Expect rewrite to contain the correct path and query.",
+ outputUrl.toString(), is( "test-scheme-output://test-host-output:777/test-path-output/test-path-input-one/test-path-input-two?test-query-name=test-query-value" ) );
+
+ inputUrl = Parser.parseLiteral( "test-scheme-input://test-host-input:42/test-path-input-one/test-path-input-two" );
+ outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2" );
+
+ assertThat(
+ "Expect rewrite to contain the correct path.",
+ outputUrl.toString(), is( "test-scheme-output://test-host-output:777/test-path-output/test-path-input-one/test-path-input-two" ) );
+
+ processor.destroy();
+ }
+
+ /**
+ * Tests the rewrite pattern used for re-writing Solr urls passed through Knox.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSolrRewrite() throws Exception {
+ URI inputUri, outputUri;
+ Matcher<Void> matcher;
+ Matcher<Void>.Match match;
+ Template input, pattern, template;
+
+ inputUri = new URI(
+ "https://hortonworks.sandbox.hdp.24.test:8443/gateway/sandbox/solr/TestCollection/select?q=*.*&wt=json&indent=true");
+
+ input = Parser.parseLiteral(inputUri.toString());
+ pattern = Parser.parseTemplate("*://*:*/**/solr/{collection=**}/{query=**}?{**}");
+ template = Parser.parseTemplate("http://sandbox.hortonworks.com/solr/{collection=**}/{query=**}?{**}");
+
+ matcher = new Matcher<Void>();
+ matcher.add(pattern, null);
+ match = matcher.match(input);
+
+ outputUri = Expander.expand(template, match.getParams(), null);
+
+ final String reWrittenScheme = outputUri.getScheme();
+ assertEquals("http", reWrittenScheme);
+
+ final String reWrittenHost = outputUri.getHost();
+ assertEquals("sandbox.hortonworks.com", reWrittenHost);
+
+ final String reWrittenPath = outputUri.getPath();
+ assertEquals("/solr/TestCollection/select", reWrittenPath);
+
+ // Whole thing is (non-deterministicly ordered around the &s):
+ // "q=*.*&wt=json&indent=true"
+ final String reWrittenQuery = outputUri.getQuery();
+
+ // Check individual parameters are present, and have the right value.
+ final Map<String, String> reWrittenParams = mapUrlParameters(reWrittenQuery);
+ assertTrue(reWrittenParams.containsKey("q"));
+ assertEquals("*.*", reWrittenParams.get("q"));
+ assertTrue(reWrittenParams.containsKey("wt"));
+ assertEquals("json", reWrittenParams.get("wt"));
+ assertEquals("true", reWrittenParams.get("indent"));
+ }
+
+
+ @Test
+ public void testSolrRewriteDefaultPort() throws Exception {
+ URI inputUri, outputUri;
+ Matcher<Void> matcher;
+ Matcher<Void>.Match match;
+ Template input, pattern, template;
+
+ inputUri = new URI(
+ "https://hortonworks.sandbox.hdp.24.test/gateway/sandbox/solr/TestCollection/select?q=*.*&wt=json&indent=true");
+
+ input = Parser.parseLiteral(inputUri.toString());
+ pattern = Parser.parseTemplate("*://*:*/**/solr/{collection=**}/{query=**}?{**}");
+ template = Parser.parseTemplate("http://sandbox.hortonworks.com/solr/{collection=**}/{query=**}?{**}");
+
+ matcher = new Matcher<Void>();
+ matcher.add(pattern, null);
+ match = matcher.match(input);
+
+ outputUri = Expander.expand(template, match.getParams(), null);
+
+ final String reWrittenScheme = outputUri.getScheme();
+ assertEquals("http", reWrittenScheme);
+
+ final String reWrittenHost = outputUri.getHost();
+ assertEquals("sandbox.hortonworks.com", reWrittenHost);
+
+ final String reWrittenPath = outputUri.getPath();
+ assertEquals("/solr/TestCollection/select", reWrittenPath);
+
+ // Whole thing is (non-deterministicly ordered around the &s):
+ // "q=*.*&wt=json&indent=true"
+ final String reWrittenQuery = outputUri.getQuery();
+
+ // Check individual parameters are present, and have the right value.
+ final Map<String, String> reWrittenParams = mapUrlParameters(reWrittenQuery);
+ assertTrue(reWrittenParams.containsKey("q"));
+ assertEquals("*.*", reWrittenParams.get("q"));
+ assertTrue(reWrittenParams.containsKey("wt"));
+ assertEquals("json", reWrittenParams.get("wt"));
+ assertEquals("true", reWrittenParams.get("indent"));
+ }
+
+
+ /**
+ * Turn a string containing URL parameters, e.g.
+ *
+ * <pre>
+ * a=b&c=d&e=f
+ * </pre>
+ *
+ * into a map such as
+ * <table>
+ * <tr>
+ * <th>Key</th>
+ * <th>Value</th>
+ * </tr>
+ * <tr>
+ * <td>a</td>
+ * <td>b</td>
+ * </tr>
+ * <tr>
+ * <td>c</td>
+ * <td>d</td>
+ * </tr>
+ * </table>
+ *
+ * @param urlParameters the URL parameter string. Expected to contain something of the form
+ * "a=b&c=d" etc (i.e. Key=Value separated by &).
+ * @return a map, with the key-values pairs representing the URL parameters.
+ */
+ private Map<String, String> mapUrlParameters(String urlParameters) {
+ final Map<String, String> map = new HashMap<>();
+ for (String pair : urlParameters.split("&")) {
+ String[] kv = pair.split("=");
+ map.put(kv[0], kv[1]);
+ }
+ return map;
+ }
+
+
+}