Search in sources :

Example 1 with MockServletInputStream

use of org.apache.knox.test.mock.MockServletInputStream in project knox by apache.

the class IdentityAssertionHttpServletRequestWrapperTest method testInsertUserNameInPostMethodWithoutEncoding.

@Test
public void testInsertUserNameInPostMethodWithoutEncoding() throws IOException {
    String inputBody = "jar=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaWebHCat%2Fhadoop-examples.jar&class=org.apache.org.apache.hadoop.examples.WordCount&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Finput&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Foutput";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setInputStream(new MockServletInputStream(new ByteArrayInputStream(inputBody.getBytes("UTF-8"))));
    request.setContentType("application/x-www-form-urlencoded");
    request.setMethod("POST");
    IdentityAsserterHttpServletRequestWrapper wrapper = new IdentityAsserterHttpServletRequestWrapper(request, "output-user");
    String outputBody = IOUtils.toString(wrapper.getInputStream(), wrapper.getCharacterEncoding());
    String output = wrapper.getQueryString();
    assertThat(output, containsString("user.name=output-user"));
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.apache.knox.test.mock.MockHttpServletRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IdentityAsserterHttpServletRequestWrapper(org.apache.knox.gateway.identityasserter.common.filter.IdentityAsserterHttpServletRequestWrapper) Test(org.junit.Test)

Example 2 with MockServletInputStream

use of org.apache.knox.test.mock.MockServletInputStream in project knox by apache.

the class IdentityAssertionHttpServletRequestWrapperTest method testInsertUserNameInPostMethod.

@Test
public void testInsertUserNameInPostMethod() throws IOException {
    String inputBody = "jar=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaWebHCat%2Fhadoop-examples.jar&class=org.apache.org.apache.hadoop.examples.WordCount&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Finput&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Foutput";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setInputStream(new MockServletInputStream(new ByteArrayInputStream(inputBody.getBytes("UTF-8"))));
    request.setCharacterEncoding("UTF-8");
    request.setContentType("application/x-www-form-urlencoded");
    request.setMethod("POST");
    IdentityAsserterHttpServletRequestWrapper wrapper = new IdentityAsserterHttpServletRequestWrapper(request, "output-user");
    String outputBody = IOUtils.toString(wrapper.getInputStream(), wrapper.getCharacterEncoding());
    String output = wrapper.getQueryString();
    assertThat(output, containsString("user.name=output-user"));
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.apache.knox.test.mock.MockHttpServletRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IdentityAsserterHttpServletRequestWrapper(org.apache.knox.gateway.identityasserter.common.filter.IdentityAsserterHttpServletRequestWrapper) Test(org.junit.Test)

Example 3 with MockServletInputStream

use of org.apache.knox.test.mock.MockServletInputStream in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-with-complex-var.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${nameNode}/user/${user.name}/${examplesRoot}/apps/hive")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Example 4 with MockServletInputStream

use of org.apache.knox.test.mock.MockServletInputStream in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForValuesRelativeToServiceRegistry.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForValuesRelativeToServiceRegistry() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-relative.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    EasyMock.expect(servletRequest.getContentLength()).andReturn(-1).anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("test-scheme://test-host:42/workflow.xml")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Example 5 with MockServletInputStream

use of org.apache.knox.test.mock.MockServletInputStream in project knox by apache.

the class IdentityAssertionHttpServletRequestWrapperTest method testOverwriteUserNameInPostMethod.

@Test
public void testOverwriteUserNameInPostMethod() throws IOException {
    String inputBody = "user.name=input-user&jar=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaWebHCat%2Fhadoop-examples.jar&class=org.apache.org.apache.hadoop.examples.WordCount&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Finput&arg=%2Ftmp%2FGatewayWebHdfsFuncTest%2FtestJavaMapReduceViaTempleton%2Foutput";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setInputStream(new MockServletInputStream(new ByteArrayInputStream(inputBody.getBytes("UTF-8"))));
    request.setCharacterEncoding("UTF-8");
    request.setContentType("application/x-www-form-urlencoded");
    request.setMethod("POST");
    IdentityAsserterHttpServletRequestWrapper wrapper = new IdentityAsserterHttpServletRequestWrapper(request, "output-user");
    String outputBody = IOUtils.toString(wrapper.getInputStream(), wrapper.getCharacterEncoding());
    String output = wrapper.getQueryString();
    assertThat(output, containsString("user.name=output-user"));
    assertThat(output, not(containsString("input-user")));
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.apache.knox.test.mock.MockHttpServletRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IdentityAsserterHttpServletRequestWrapper(org.apache.knox.gateway.identityasserter.common.filter.IdentityAsserterHttpServletRequestWrapper) Test(org.junit.Test)

Aggregations

MockServletInputStream (org.apache.knox.test.mock.MockServletInputStream)8 Test (org.junit.Test)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IdentityAsserterHttpServletRequestWrapper (org.apache.knox.gateway.identityasserter.common.filter.IdentityAsserterHttpServletRequestWrapper)5 MockHttpServletRequest (org.apache.knox.test.mock.MockHttpServletRequest)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 FilterConfig (javax.servlet.FilterConfig)3 ServletContext (javax.servlet.ServletContext)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)3 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)3 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)3 UrlRewriteServletEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment)3 UrlRewriteRequest (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest)3 GatewayServices (org.apache.knox.gateway.services.GatewayServices)3 ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)3 Document (org.w3c.dom.Document)3