ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/coyote/http2/TestHttp2InitialConnection.java

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

      
    
Rootfs path

      
    
Size
6657 (6.5 KB)
MD5
0e56c186dcade2a07dc080fcd97a243d
SHA1
cf589020832f3c40f93c6bf8d02f8c3c543c8fe8
SHA256
d36f59d4fd50b3d5bcdf382b074bdb42b7e828210bedac20ffb95eaae48324bc
SHA512

      
    
SHA1_git
462f600f3f6bd09cca0935bc0c7d1beacaf46aa9
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestHttp2InitialConnection.java | 6.5 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.coyote.http2; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.util.ServerInfo; import org.apache.catalina.valves.ErrorReportValve; import org.apache.tomcat.util.res.StringManager; public class TestHttp2InitialConnection extends Http2TestBase { private TestData testData; @Test public void testValidHostHeader() throws Exception { List<String> hostHeaders = new ArrayList<>(1); hostHeaders.add("localhost:8080"); testData = new TestData(hostHeaders, 200); http2Connect(); } @Test public void testMultipleHostHeaders() throws Exception { List<String> hostHeaders = new ArrayList<>(1); hostHeaders.add("localhost:8080"); hostHeaders.add("localhost:8081"); testData = new TestData(hostHeaders, 400); http2Connect(); } @Test public void testNoHostHeader() throws Exception { List<String> hostHeaders = new ArrayList<>(1); testData = new TestData(hostHeaders, 400); http2Connect(); } @Override protected void doHttpUpgrade(String connection, String upgrade, String settings, boolean validate) throws IOException { StringBuilder request = new StringBuilder(); request.append("GET /simple HTTP/1.1 "); for (String hostHeader : testData.getHostHeaders()) { request.append("Host: "); request.append(hostHeader); request.append(" "); } // Connection request.append("Connection: "); request.append(connection); request.append(" "); // Upgrade request.append("Upgrade: "); request.append(upgrade); request.append(" "); // Settings request.append(settings); // Locale - Force the en Locale else the i18n on the error page changes // the size of the response body and that triggers a failure as the test // checks the exact response length request.append("Accept-Language: en "); // Request terminator request.append(" "); byte[] upgradeRequest = request.toString().getBytes(StandardCharsets.ISO_8859_1); os.write(upgradeRequest); os.flush(); if (validate) { Assert.assertTrue("Failed to read HTTP Upgrade response", readHttpUpgradeResponse()); } } @Override protected String getResponseBodyFrameTrace(int streamId, String body) { if (testData.getExpectedStatus() == 200) { return super.getResponseBodyFrameTrace(streamId, body); } else if (testData.getExpectedStatus() == 400) { /* * Need to be careful here. The test wants the exact content length in bytes. This will vary depending on * where the test is run due to: - The length of the version string that appears once in the error page - * The status header uses a UTF-8 EN dash. When running in an IDE the UTF-8 properties files will be used * directly rather than after native2ascii conversion. * * Note: The status header appears twice in the error page. */ int serverInfoLength = ServerInfo.getServerInfo().getBytes().length; StringManager sm = StringManager.getManager(ErrorReportValve.class.getPackage().getName(), Locale.ENGLISH); String reason = sm.getString("http." + testData.getExpectedStatus() + ".reason"); int descriptionLength = sm.getString("http." + testData.getExpectedStatus() + ".desc") .getBytes(StandardCharsets.UTF_8).length; int statusHeaderLength = sm .getString("errorReportValve.statusHeader", String.valueOf(testData.getExpectedStatus()), reason) .getBytes(StandardCharsets.UTF_8).length; int typeLabelLength = sm.getString("errorReportValve.type").getBytes(StandardCharsets.UTF_8).length; int statusReportLabelLength = sm.getString("errorReportValve.statusReport") .getBytes(StandardCharsets.UTF_8).length; int descriptionLabelLength = sm.getString("errorReportValve.description") .getBytes(StandardCharsets.UTF_8).length; // 196 bytes is the static length of the pure HTML code from the ErrorReportValve int len = 196 + org.apache.catalina.util.TomcatCSS.TOMCAT_CSS.getBytes(StandardCharsets.UTF_8).length + typeLabelLength + statusReportLabelLength + descriptionLabelLength + descriptionLength + serverInfoLength + statusHeaderLength * 2; String contentLength = String.valueOf(len); return getResponseBodyFrameTrace(streamId, testData.getExpectedStatus(), "text/html;charset=utf-8", "en", contentLength, contentLength); } else { Assert.fail(); // To keep the IDE happy return null; } } private static class TestData { private final List<String> hostHeaders; private final int expectedStatus; TestData(List<String> hostHeaders, int expectedStatus) { this.hostHeaders = hostHeaders; this.expectedStatus = expectedStatus; } public List<String> getHostHeaders() { return hostHeaders; } public int getExpectedStatus() { return expectedStatus; } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
18.34
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