Skip to content

Commit 7471ec9

Browse files
authoredMay 7, 2020
Merge pull request #461 from Ladicek/protocol-upgrade-fix
fix the test for HTTP protocol upgrade
2 parents 278bf76 + b0e8b57 commit 7471ec9

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed
 

‎servlet/protocol-handler/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@
1212
<packaging>war</packaging>
1313

1414
<name>Java EE 7 Sample: servlet - protocol-handler</name>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-surefire-plugin</artifactId>
21+
<configuration>
22+
<systemPropertyVariables combine.children="append">
23+
<!-- to request protocol upgrade, the client must send the Connection and Upgrade headers -->
24+
<sun.net.http.allowRestrictedHeaders>true</sun.net.http.allowRestrictedHeaders>
25+
</systemPropertyVariables>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
1530
</project>

‎servlet/protocol-handler/src/main/java/org/javaee7/servlet/protocolhandler/UpgradeServlet.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package org.javaee7.servlet.protocolhandler;
4141

42+
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
4243
import static javax.servlet.http.HttpServletResponse.SC_SWITCHING_PROTOCOLS;
4344

4445
import java.io.IOException;
@@ -66,11 +67,16 @@ public class UpgradeServlet extends HttpServlet {
6667
* @throws IOException if an I/O error occurs
6768
*/
6869
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69-
response.setStatus(SC_SWITCHING_PROTOCOLS);
70-
response.setHeader("Connection", "Upgrade");
71-
response.setHeader("Upgrade", "echo");
72-
request.upgrade(MyProtocolHandler.class);
73-
74-
System.out.println("Request upgraded to MyProtocolHandler");
70+
String requestedUpgrade = request.getHeader("Upgrade");
71+
if ("echo".equals(requestedUpgrade)) {
72+
response.setStatus(SC_SWITCHING_PROTOCOLS);
73+
response.setHeader("Connection", "Upgrade");
74+
response.setHeader("Upgrade", "echo");
75+
request.upgrade(MyProtocolHandler.class);
76+
77+
System.out.println("Request upgraded to MyProtocolHandler");
78+
} else {
79+
response.sendError(SC_BAD_REQUEST, "unknown upgrade " + requestedUpgrade);
80+
}
7581
}
7682
}

‎servlet/protocol-handler/src/test/java/org/javaee7/servlet/protocolhandler/ProtocolHandlerTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.javaee7.servlet.protocolhandler;
22

3-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.Assert.assertEquals;
44

55
import java.io.IOException;
66
import java.io.InputStream;
@@ -44,6 +44,8 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
4444
// typically hang when reading.
4545

4646
URLConnection connection = new URL(base, "UpgradeServlet").openConnection();
47+
connection.setRequestProperty("Connection", "Upgrade");
48+
connection.setRequestProperty("Upgrade", "echo");
4749
connection.setConnectTimeout(2000);
4850
connection.setReadTimeout(2000);
4951

@@ -71,7 +73,7 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
7173
}
7274
}
7375

74-
assertTrue("In protocol handler".equals(response.toString()));
76+
assertEquals("In protocol handler", response.toString());
7577
}
7678

7779
}

0 commit comments

Comments
 (0)
Failed to load comments.