1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* 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.tomcat.util.http.mapper;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.deploy.SecurityCollection;
import org.apache.catalina.deploy.SecurityConstraint;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.catalina.valves.RemoteAddrValve;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Mapper tests that use real web applications on a running Tomcat.
*/
public class TestMapperWebapps extends TomcatBaseTest{
@Test
public void testContextRoot_Bug53339() throws Exception {
Tomcat tomcat = getTomcatInstance();
tomcat.enableNaming();
// Must have a real docBase - just use temp
Context ctx =
tomcat.addContext("", System.getProperty("java.io.tmpdir"));
Tomcat.addServlet(ctx, "Bug53356", new Bug53356Servlet());
ctx.addServletMapping("", "Bug53356");
tomcat.start();
ByteChunk body = getUrl("http://localhost:" + getPort());
Assert.assertEquals("OK", body.toString());
}
private static class Bug53356Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Confirm behaviour as per Servler 12.2
boolean pass = "/".equals(req.getPathInfo());
if (pass) {
pass = "".equals(req.getServletPath());
}
if (pass) {
pass = "".equals(req.getContextPath());
}
resp.setContentType("text/plain");
if (pass) {
resp.getWriter().write("OK");
} else {
resp.getWriter().write("FAIL");
}
}
}
@Test
public void testContextReload_Bug56658_Bug56882() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File(getBuildDirectory(), "webapps/examples");
// app dir is relative to server home
org.apache.catalina.Context ctxt = tomcat.addWebapp(
null, "/examples", appDir.getAbsolutePath());
tomcat.start();
// The tests are from TestTomcat#testSingleWebapp(), #testJsps()
// We reload the context and verify that the pages are still accessible
ByteChunk res;
String text;
res = getUrl("http://localhost:" + getPort()
+ "/examples/servlets/servlet/HelloWorldExample");
text = res.toString();
Assert.assertTrue(text, text.contains("<h1>Hello World!</h1>"));
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/jsp2/el/basic-arithmetic.jsp");
text = res.toString();
Assert.assertTrue(text, text.contains("<td>${(1==2) ? 3 : 4}</td>"));
res = getUrl("http://localhost:" + getPort() + "/examples/index.html");
text = res.toString();
Assert.assertTrue(text, text.contains("<title>Apache Tomcat Examples</title>"));
long timeA = System.currentTimeMillis();
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/include/include.jsp");
String timestamp = findCommonPrefix(timeA, System.currentTimeMillis());
text = res.toString();
Assert.assertTrue(text, text.contains(
"In place evaluation of another JSP which gives you the current time: " + timestamp));
Assert.assertTrue(text, text.contains(
"To get the current time in ms"));
Assert.assertTrue(text, text.contains(
"by including the output of another JSP: " + timestamp));
Assert.assertTrue(text, text.contains(":-)"));
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/forward/forward.jsp");
text = res.toString();
Assert.assertTrue(text, text.contains("VM Memory usage"));
ctxt.reload();
res = getUrl("http://localhost:" + getPort()
+ "/examples/servlets/servlet/HelloWorldExample");
text = res.toString();
Assert.assertTrue(text, text.contains("<h1>Hello World!</h1>"));
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/jsp2/el/basic-arithmetic.jsp");
text = res.toString();
Assert.assertTrue(text, text.contains("<td>${(1==2) ? 3 : 4}</td>"));
res = getUrl("http://localhost:" + getPort() + "/examples/index.html");
text = res.toString();
Assert.assertTrue(text, text.contains("<title>Apache Tomcat Examples</title>"));
timeA = System.currentTimeMillis();
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/include/include.jsp");
timestamp = findCommonPrefix(timeA, System.currentTimeMillis());
text = res.toString();
Assert.assertTrue(text, text.contains(
"In place evaluation of another JSP which gives you the current time: " + timestamp));
Assert.assertTrue(text, text.contains(
"To get the current time in ms"));
Assert.assertTrue(text, text.contains(
"by including the output of another JSP: " + timestamp));
Assert.assertTrue(text, text.contains(":-)"));
res = getUrl("http://localhost:" + getPort()
+ "/examples/jsp/forward/forward.jsp");
text = res.toString();
Assert.assertTrue(text, text.contains("VM Memory usage"));
}
@Test
public void testWelcomeFileNotStrict() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.0");
StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, "/test",
appDir.getAbsolutePath());
ctxt.setReplaceWelcomeFiles(true);
ctxt.addWelcomeFile("index.jsp");
// Mapping for *.do is defined in web.xml
ctxt.addWelcomeFile("index.do");
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/welcome-files", bc, new HashMap<String,List<String>>());
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(bc.toString().contains("JSP"));
rc = getUrl("http://localhost:" + getPort() +
"/test/welcome-files/sub", bc,
new HashMap<String,List<String>>());
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(bc.toString().contains("Servlet"));
}
@Test
public void testWelcomeFileStrict() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.0");
StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, "/test",
appDir.getAbsolutePath());
ctxt.setReplaceWelcomeFiles(true);
ctxt.addWelcomeFile("index.jsp");
// Mapping for *.do is defined in web.xml
ctxt.addWelcomeFile("index.do");
// Simulate STRICT_SERVLET_COMPLIANCE
ctxt.setResourceOnlyServlets("");
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/welcome-files", bc, new HashMap<String,List<String>>());
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(bc.toString().contains("JSP"));
rc = getUrl("http://localhost:" + getPort() +
"/test/welcome-files/sub", bc,
new HashMap<String,List<String>>());
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
@Test
public void testRedirect() throws Exception {
// Disable the following of redirects for this test only
boolean originalValue = HttpURLConnection.getFollowRedirects();
HttpURLConnection.setFollowRedirects(false);
try {
Tomcat tomcat = getTomcatInstance();
// Use standard test webapp as ROOT
File rootDir = new File("test/webapp-3.0");
org.apache.catalina.Context root =
tomcat.addWebapp(null, "", rootDir.getAbsolutePath());
// Add a security constraint
SecurityConstraint constraint = new SecurityConstraint();
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/welcome-files/*");
collection.addPattern("/welcome-files");
constraint.addCollection(collection);
constraint.addAuthRole("foo");
root.addConstraint(constraint);
// Also make examples available
File examplesDir = new File(getBuildDirectory(), "webapps/examples");
org.apache.catalina.Context examples = tomcat.addWebapp(
null, "/examples", examplesDir.getAbsolutePath());
// Then block access to the examples to test redirection
RemoteAddrValve rav = new RemoteAddrValve();
rav.setDeny(".*");
rav.setDenyStatus(404);
examples.getPipeline().addValve(rav);
tomcat.start();
// Redirects within a web application
doRedirectTest("/welcome-files", 401);
doRedirectTest("/welcome-files/", 401);
doRedirectTest("/jsp", 302);
doRedirectTest("/jsp/", 404);
doRedirectTest("/WEB-INF", 404);
doRedirectTest("/WEB-INF/", 404);
// Redirects between web applications
doRedirectTest("/examples", 404);
doRedirectTest("/examples/", 404);
} finally {
HttpURLConnection.setFollowRedirects(originalValue);
}
}
private void doRedirectTest(String path, int expected) throws IOException {
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + path, bc, null);
Assert.assertEquals(expected, rc);
}
/**
* Prepare a string to search in messages that contain a timestamp, when it
* is known that the timestamp was printed between {@code timeA} and
* {@code timeB}.
*/
private static String findCommonPrefix(long timeA, long timeB) {
while ((timeA != timeB) && timeA > 0) {
timeA /= 10;
timeB /= 10;
}
return String.valueOf(timeA);
}
}
|