Skip to content

Commit 298fee3

Browse files
authored
Remove usages of deprecated functionality (#433)
1 parent 28beba4 commit 298fee3

File tree

5 files changed

+31
-42
lines changed

5 files changed

+31
-42
lines changed

src/test/java/winstone/AbstractWinstoneTest.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.jetty.http2.client.transport.ClientConnectionFactoryOverHTTP2;
1818
import org.eclipse.jetty.io.ClientConnectionFactory;
1919
import org.eclipse.jetty.io.ClientConnector;
20+
import org.eclipse.jetty.io.Transport;
2021
import org.eclipse.jetty.util.ssl.SslContextFactory;
2122
import org.junit.After;
2223

@@ -33,25 +34,18 @@ public void tearDown() {
3334
}
3435
}
3536

36-
enum Protocol {
37+
public enum Protocol {
3738
HTTP_1,
3839
HTTP_2;
3940
}
4041

4142
/**
42-
* please use {@link #makeRequest(String, String, Protocol)}
43-
*/
44-
@Deprecated
45-
public String makeRequest(String url) throws Exception {
46-
return makeRequest(null, url, Protocol.HTTP_1);
47-
}
48-
49-
/**
50-
* please use {@link #makeRequest(String, String, Protocol)}
43+
* @param url the URL to request
44+
* @param protocol see {@link AbstractWinstoneTest.Protocol}
45+
* @return the response
5146
*/
52-
@Deprecated
53-
public String makeRequest(String path, String url) throws Exception {
54-
return makeRequest(path, url, Protocol.HTTP_1);
47+
public String makeRequest(String url, Protocol protocol) throws Exception {
48+
return makeRequest(null, url, HttpStatus.OK_200, protocol);
5549
}
5650

5751
/**
@@ -77,9 +71,12 @@ public String makeRequest(String path, String url, Protocol protocol) throws Exc
7771
*/
7872
public String makeRequest(String path, String url, int expectedHttpStatus, Protocol protocol) throws Exception {
7973

80-
HttpClient httpClient = getHttpClient(path);
74+
HttpClient httpClient = getHttpClient();
8175

8276
Request request = httpClient.newRequest(url);
77+
if (path != null) {
78+
request.transport(new Transport.TCPUnix(Path.of(path)));
79+
}
8380

8481
switch (protocol) {
8582
case HTTP_1 -> request.version(HttpVersion.HTTP_1_1);
@@ -95,29 +92,20 @@ public String makeRequest(String path, String url, int expectedHttpStatus, Proto
9592
return response.getContentAsString();
9693
}
9794

98-
protected HttpClient getHttpClient(String path) throws Exception {
99-
HttpClient httpClient;
100-
101-
ClientConnector connector;
95+
protected HttpClient getHttpClient() throws Exception {
96+
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(true);
97+
sslContextFactory.setHostnameVerifier((hostname, session) -> true);
10298

103-
if (path != null) {
104-
Path unixDomainPath = Path.of(path);
105-
connector = ClientConnector.forUnixDomain(unixDomainPath);
106-
} else {
107-
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(true);
108-
sslContextFactory.setHostnameVerifier((hostname, session) -> true);
109-
110-
connector = new ClientConnector();
111-
connector.setSslContextFactory(sslContextFactory);
112-
}
99+
ClientConnector connector = new ClientConnector();
100+
connector.setSslContextFactory(sslContextFactory);
113101

114102
ClientConnectionFactory.Info http1 = HttpClientConnectionFactory.HTTP11;
115103

116104
HTTP2Client http2Client = new HTTP2Client(connector);
117105
ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
118106

119107
HttpClientTransportDynamic transport = new HttpClientTransportDynamic(connector, http1, http2);
120-
httpClient = new HttpClient(transport);
108+
HttpClient httpClient = new HttpClient(transport);
121109
httpClient.setFollowRedirects(false);
122110
httpClient.start();
123111
return httpClient;

src/test/java/winstone/Http2ConnectorFactoryTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void wildcard() throws Exception {
3030
assertConnectionRefused("127.0.0.2", port);
3131
assertEquals(
3232
"<html><body>This servlet has been accessed via GET 1001 times</body></html>\r\n",
33-
makeRequest(null, "https://localhost:" + port + "/CountRequestsServlet", Protocol.HTTP_2));
33+
makeRequest("https://localhost:" + port + "/CountRequestsServlet", Protocol.HTTP_2));
3434
LowResourceMonitor lowResourceMonitor = winstone.server.getBean(LowResourceMonitor.class);
3535
assertNotNull(lowResourceMonitor);
3636
assertFalse(lowResourceMonitor.isLowOnResources());
@@ -52,12 +52,11 @@ public void helloSuspiciousPathCharacters() throws Exception {
5252

5353
assertEquals(
5454
"<html><body>Hello winstone </body></html>\r\n",
55-
makeRequest(null, "https://127.0.0.1:" + port + "/hello/winstone", Protocol.HTTP_2));
55+
makeRequest("https://127.0.0.1:" + port + "/hello/winstone", Protocol.HTTP_2));
5656

5757
assertEquals(
5858
"<html><body>Hello win\\stone </body></html>\r\n",
5959
makeRequest(
60-
null,
6160
"https://127.0.0.1:" + port + "/hello/"
6261
+ URLEncoder.encode("win\\stone", StandardCharsets.UTF_8),
6362
Protocol.HTTP_2)); // %5C == \

src/test/java/winstone/HttpConnectorFactoryTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testListenUnixDomainPath() throws Exception {
5757

5858
assertEquals(
5959
"<html><body>This servlet has been accessed via GET 1001 times</body></html>\r\n",
60-
makeRequest(path, "http://127.0.0.1:80/CountRequestsServlet"));
60+
makeRequest(path, "http://127.0.0.1:80/CountRequestsServlet", Protocol.HTTP_1));
6161

6262
LowResourceMonitor lowResourceMonitor = winstone.server.getBean(LowResourceMonitor.class);
6363
assertNotNull(lowResourceMonitor);
@@ -79,7 +79,7 @@ public void testListenAddress() throws Exception {
7979

8080
assertEquals(
8181
"<html><body>This servlet has been accessed via GET 1001 times</body></html>\r\n",
82-
makeRequest("http://127.0.0.2:" + port + "/CountRequestsServlet"));
82+
makeRequest("http://127.0.0.2:" + port + "/CountRequestsServlet", Protocol.HTTP_1));
8383

8484
LowResourceMonitor lowResourceMonitor = winstone.server.getBean(LowResourceMonitor.class);
8585
assertNotNull(lowResourceMonitor);
@@ -125,11 +125,14 @@ public void helloSuspiciousPathCharacters() throws Exception {
125125

126126
assertEquals(
127127
"<html><body>Hello winstone </body></html>\r\n",
128-
makeRequest("http://127.0.0.1:" + port + "/hello/winstone"));
128+
makeRequest("http://127.0.0.1:" + port + "/hello/winstone", Protocol.HTTP_1));
129129

130+
// %5C == \
130131
assertEquals(
131132
"<html><body>Hello win\\stone </body></html>\r\n",
132-
makeRequest("http://127.0.0.1:" + port + "/hello/"
133-
+ URLEncoder.encode("win\\stone", StandardCharsets.UTF_8))); // %5C == \
133+
makeRequest(
134+
"http://127.0.0.1:" + port + "/hello/"
135+
+ URLEncoder.encode("win\\stone", StandardCharsets.UTF_8),
136+
Protocol.HTTP_1));
134137
}
135138
}

src/test/java/winstone/HttpsConnectorFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void wildcard() throws Exception {
4040
assertConnectionRefused("127.0.0.2", port);
4141
assertEquals(
4242
"<html><body>This servlet has been accessed via GET 1001 times</body></html>\r\n",
43-
makeRequest(null, "https://localhost:" + port + "/CountRequestsServlet", Protocol.HTTP_1));
43+
makeRequest("https://localhost:" + port + "/CountRequestsServlet", Protocol.HTTP_1));
4444
LowResourceMonitor lowResourceMonitor = winstone.server.getBean(LowResourceMonitor.class);
4545
assertNotNull(lowResourceMonitor);
4646
assertFalse(lowResourceMonitor.isLowOnResources());
@@ -85,11 +85,11 @@ public void httpRedirect() throws Exception {
8585
// also verify that directly accessing the resource works.
8686
assertEquals(
8787
"<html><body>This servlet has been accessed via GET 1002 times</body></html>\r\n",
88-
makeRequest(null, "https://localhost:" + httpsPort + "/CountRequestsServlet", Protocol.HTTP_1));
88+
makeRequest("https://localhost:" + httpsPort + "/CountRequestsServlet", Protocol.HTTP_1));
8989
}
9090

9191
private String requestRedirect(int httpPort) throws Exception {
92-
HttpClient httpClient = getHttpClient(null);
92+
HttpClient httpClient = getHttpClient();
9393
try {
9494
ContentResponse contentResponse = httpClient.GET("http://localhost:" + httpPort + "/CountRequestsServlet");
9595
assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, contentResponse.getStatus());

src/test/java/winstone/accesslog/SimpleAccessLoggerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public void testSimpleConnection() throws Exception {
3838
// make a request
3939
assertEquals(
4040
"<html><body>This servlet has been accessed via GET 1001 times</body></html>\r\n",
41-
makeRequest("http://localhost:" + port + "/examples/CountRequestsServlet"));
42-
41+
makeRequest("http://localhost:" + port + "/examples/CountRequestsServlet", Protocol.HTTP_1));
4342
// check the log file
4443
// check the log file every 100ms for 5s
4544
String text = "";

0 commit comments

Comments
 (0)