Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-11764] Test case for preserve-path-on-forward #44

Merged
merged 1 commit into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.jboss.as.test.integration.web.servlet.preservepath;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/test")
public class ForwardingServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String nextJSP = "/preserve-path.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(req,resp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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.jboss.as.test.integration.web.servlet.preservepath;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class PreservePathFilter implements Filter {


@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {

filterChain.doFilter(servletRequest, servletResponse);

HttpServletRequest request = (HttpServletRequest) servletRequest;

String tmpFolder = request.getParameter("path");
File file = new File(tmpFolder + "/output.txt");
file.createNewFile();
try(FileWriter fw = new FileWriter(file)) {
fw.write("servletPath: " + request.getServletPath());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* 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.jboss.as.test.integration.web.servlet.preservepath;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.test.integration.management.util.ServerReload;
import org.jboss.as.test.integration.security.common.CoreUtils;
import org.jboss.as.test.shared.TestSuiteEnvironment;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.UrlAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.jboss.as.test.integration.management.util.ModelUtil.createOpNode;

@RunAsClient
@RunWith(Arquillian.class)
public class PreservePathTestCase {

@ArquillianResource
private URL url;

@ArquillianResource
private ManagementClient managementClient;

final String tempDir = TestSuiteEnvironment.getTmpDir();

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
File file = new File(tempDir + "/output.txt");
if (file.exists()) {
file.delete();
}

ModelNode setPreservePathOp = createOpNode("system-property=io.undertow.servlet.dispatch.preserve_path_of_forward", ModelDescriptionConstants.REMOVE);
CoreUtils.applyUpdate(setPreservePathOp, managementClient.getControllerClient());
ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient());
}

@Deployment
public static WebArchive deployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class);
war.addClass(ForwardingServlet.class);
war.addClass(PreservePathFilter.class);
war.add(new UrlAsset(PreservePathTestCase.class.getResource("preserve-path.jsp")), "preserve-path.jsp");
war.addAsWebInfResource(PreservePathTestCase.class.getPackage(), "web.xml", "web.xml");
return war;
}

@Test
public void testPreservePath() throws Exception {
setPreservePathOnForward("true");

HttpClient httpclient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(url.toString() + "/test?path="+ URLEncoder.encode(tempDir));
HttpResponse response = httpclient.execute(httpget);

Thread.sleep(100);
FileReader fr = new FileReader(new File(tempDir + "/output.txt"));
Assert.assertEquals("servletPath: /test", new BufferedReader(fr).readLine());
}

@Test
public void testDontPreservePath() throws Exception {
setPreservePathOnForward("false");

HttpClient httpclient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(url.toString() + "/test?path="+ URLEncoder.encode(tempDir));
HttpResponse response = httpclient.execute(httpget);

Thread.sleep(100);
FileReader fr = new FileReader(new File(tempDir + "/output.txt"));
Assert.assertEquals("servletPath: /preserve-path.jsp", new BufferedReader(fr).readLine());
}

private void setPreservePathOnForward(String s) throws Exception {
ModelNode setPreservePathOp = createOpNode("system-property=io.undertow.servlet.dispatch.preserve_path_of_forward", ModelDescriptionConstants.ADD);
setPreservePathOp.get("value").set(s);

CoreUtils.applyUpdate(setPreservePathOp, managementClient.getControllerClient());

ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<h1>Hello</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2015, Red Hat, Inc., and individual contributors
~ as indicated by the @author tags. See the copyright.txt file in the
~ distribution for a full listing of individual contributors.
~
~ This is free software; you can redistribute it and/or modify it
~ under the terms of the GNU Lesser General Public License as
~ published by the Free Software Foundation; either version 2.1 of
~ the License, or (at your option) any later version.
~
~ This software is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this software; if not, write to the Free
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<filter>
<filter-name>filter</filter-name>
<filter-class>org.jboss.as.test.integration.web.servlet.preservepath.PreservePathFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>