ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/connector/TestConnector.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/connector/TestConnector.java
Status
scanned
Type
file
Name
TestConnector.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
6390 (6.2 KB)
MD5
b7ba1727cc29caf7688c79143ce37796
SHA1
b80d5156f04464bb6c2074ab15d878fb85c51a30
SHA256
9c58a73dc738256655034de33363e2982b96fd7690fa156d00a868d8510aaba3
SHA512

      
    
SHA1_git
167a0a667ef7ce785f9a438df9cc27105f053105
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestConnector.java | 6.2 KB |

/* * 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.catalina.connector; import java.io.File; import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.List; import java.util.Map; import jakarta.servlet.Servlet; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; import org.apache.catalina.Wrapper; import org.apache.catalina.servlets.DefaultServlet; import org.apache.catalina.servlets.WebdavServlet; import org.apache.catalina.startup.TesterServlet; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.http.Method; /** * Test cases for {@link Connector}. */ public class TestConnector extends TomcatBaseTest { @Test public void testStop() throws Exception { Tomcat tomcat = getTomcatInstance(); Context root = tomcat.addContext("", TEMP_DIR); Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet()); w.setAsyncSupported(true); root.addServletMappingDecoded("/", "tester"); Connector connector = tomcat.getConnector(); tomcat.start(); ByteChunk bc = new ByteChunk(); int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null); Assert.assertEquals(200, rc); Assert.assertEquals("OK", bc.toString()); rc = -1; bc.recycle(); connector.stop(); try { rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000, null, null); } catch (SocketTimeoutException ste) { // May also see this with NIO // Make sure the test passes if we do rc = 503; } Assert.assertEquals(503, rc); } @Test public void testPort() throws Exception { Tomcat tomcat = getTomcatInstance(); Connector connector1 = tomcat.getConnector(); connector1.setPort(0); Connector connector2 = new Connector(); connector2.setPort(0); tomcat.getService().addConnector(connector2); tomcat.start(); int localPort1 = connector1.getLocalPort(); int localPort2 = connector2.getLocalPort(); Assert.assertTrue(localPort1 > 0); Assert.assertTrue(localPort2 > 0); } @Test(expected = LifecycleException.class) public void testInvalidProtocolThrows() throws Exception { doTestInvalidProtocol(true); } @Test public void testInvalidProtocolNoThrows() throws Exception { doTestInvalidProtocol(false); } private void doTestInvalidProtocol(boolean throwOnFailure) throws Exception { Connector c = new Connector("foo.Bar"); c.setThrowOnFailure(throwOnFailure); c.start(); } @Test(expected = LifecycleException.class) public void testDuplicatePortThrows() throws Exception { doTestDuplicatePort(true); } @Test public void testDuplicatePortNoThrows() throws Exception { doTestDuplicatePort(false); } private void doTestDuplicatePort(boolean throwOnFailure) throws Exception { Connector c1 = new Connector(); c1.setThrowOnFailure(throwOnFailure); c1.setPort(0); c1.start(); Connector c2 = new Connector(); c2.setThrowOnFailure(throwOnFailure); c2.setPort(c1.getLocalPort()); c2.start(); } @Test public void testTraceAllowedDefault() throws Exception { doTestTrace(new DefaultServlet(), true); } @Test public void testTraceNotAllowedDefault() throws Exception { doTestTrace(new DefaultServlet(), false); } @Test public void testTraceAllowedWebDav() throws Exception { doTestTrace(new WebdavServlet(), true); } @Test public void testTraceNotAllowedWebDav() throws Exception { doTestTrace(new WebdavServlet(), false); } @Test public void testTraceAllowedCustom() throws Exception { doTestTrace(new TesterServlet(), true); } @Test public void testTraceNotAllowedCustom() throws Exception { doTestTrace(new TesterServlet(), false); } private void doTestTrace(Servlet servlet, boolean allowTrace) throws Exception { Tomcat tomcat = getTomcatInstance(); File appDir = new File("test/webapp"); Context root = tomcat.addContext("", appDir.getAbsolutePath()); Tomcat.addServlet(root, "default", servlet); root.addServletMappingDecoded("/", "default"); Connector connector = tomcat.getConnector(); connector.setAllowTrace(allowTrace); tomcat.start(); ByteChunk bc = new ByteChunk(); Map<String,List<String>> respHeaders = new HashMap<>(); int rc = methodUrl("http://localhost:" + getPort() + "/index.html", bc, 30000, null, respHeaders, Method.OPTIONS); Assert.assertEquals(200, rc); boolean foundTrace = false; for (String header : respHeaders.get("Allow")) { if (header.contains(Method.TRACE)) { foundTrace = true; break; } } if (allowTrace) { Assert.assertTrue(foundTrace); } else { Assert.assertFalse(foundTrace); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
19.6
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9