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
|
/*
* 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\r\n");
for (String hostHeader : testData.getHostHeaders()) {
request.append("Host: ");
request.append(hostHeader);
request.append("\r\n");
}
// Connection
request.append("Connection: ");
request.append(connection);
request.append("\r\n");
// Upgrade
request.append("Upgrade: ");
request.append(upgrade);
request.append("\r\n");
// 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\r\n");
// Request terminator
request.append("\r\n");
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;
}
}
}
|