Skip to content

Commit 08d56b5

Browse files
committed
some DRY with the test support client
1 parent d4baee6 commit 08d56b5

File tree

4 files changed

+101
-221
lines changed

4 files changed

+101
-221
lines changed

src/test/java/InputNotRead.java

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@
3535
import java.io.BufferedReader;
3636
import java.io.IOException;
3737
import java.io.InputStreamReader;
38-
import java.io.OutputStreamWriter;
39-
import java.io.PrintWriter;
4038
import java.net.InetAddress;
4139
import java.net.InetSocketAddress;
42-
import java.net.Socket;
4340
import java.util.concurrent.ExecutorService;
4441
import java.util.concurrent.Executors;
4542
import java.util.concurrent.ThreadFactory;
@@ -55,6 +52,8 @@
5552

5653
import static java.nio.charset.StandardCharsets.*;
5754

55+
import jdk.test.lib.RawClient;
56+
5857
public class InputNotRead {
5958

6059
private static final int msgCode = 200;
@@ -106,7 +105,7 @@ public void handle(HttpExchange msg) throws IOException {
106105
System.out.println("Server started at port "
107106
+ server.getAddress().getPort());
108107

109-
runRawSocketHttpClient(loopback, server.getAddress().getPort(), -1);
108+
RawClient.runRawSocketHttpClient(loopback, server.getAddress().getPort(), someContext, "I will send all of the data", -1);
110109
} finally {
111110
System.out.println("shutting server down");
112111
executor.shutdown();
@@ -148,7 +147,7 @@ public void handle(HttpExchange msg) throws IOException {
148147
System.out.println("Server started at port "
149148
+ server.getAddress().getPort());
150149

151-
runRawSocketHttpClient(loopback, server.getAddress().getPort(), 64 * 1024 + 16);
150+
RawClient.runRawSocketHttpClient(loopback, server.getAddress().getPort(), someContext, "send some data to trigger the output", 64 * 1024 + 16);
152151
} finally {
153152
System.out.println("shutting server down");
154153
executor.shutdown();
@@ -157,74 +156,5 @@ public void handle(HttpExchange msg) throws IOException {
157156
System.out.println("Server finished.");
158157
}
159158

160-
static void runRawSocketHttpClient(InetAddress address, int port, int contentLength)
161-
throws Exception
162-
{
163-
Socket socket = null;
164-
PrintWriter writer = null;
165-
BufferedReader reader = null;
166-
final String CRLF = "\r\n";
167-
try {
168-
socket = new Socket(address, port);
169-
writer = new PrintWriter(new OutputStreamWriter(
170-
socket.getOutputStream()));
171-
System.out.println("Client connected by socket: " + socket);
172-
String body = "I will send all the data.";
173-
if (contentLength <= 0)
174-
contentLength = body.getBytes(UTF_8).length;
175-
176-
writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF);
177-
writer.print("User-Agent: Java/"
178-
+ System.getProperty("java.version")
179-
+ CRLF);
180-
writer.print("Host: " + address.getHostName() + CRLF);
181-
writer.print("Accept: */*" + CRLF);
182-
writer.print("Content-Length: " + contentLength + CRLF);
183-
writer.print("Connection: keep-alive" + CRLF);
184-
writer.print(CRLF); // Important, else the server will expect that
185-
// there's more into the request.
186-
writer.flush();
187-
System.out.println("Client wrote request to socket: " + socket);
188-
writer.print(body);
189-
writer.flush();
190-
191-
reader = new BufferedReader(new InputStreamReader(
192-
socket.getInputStream()));
193-
System.out.println("Client start reading from server:" );
194-
String line = reader.readLine();
195-
for (; line != null; line = reader.readLine()) {
196-
if (line.isEmpty()) {
197-
break;
198-
}
199-
System.out.println("\"" + line + "\"");
200-
}
201-
System.out.println("Client finished reading from server" );
202-
} finally {
203-
// give time to the server to try & drain its input stream
204-
Thread.sleep(500);
205-
// closes the client outputstream while the server is draining
206-
// it
207-
if (writer != null) {
208-
writer.close();
209-
}
210-
// give time to the server to trigger its assertion
211-
// error before closing the connection
212-
Thread.sleep(500);
213-
if (reader != null)
214-
try {
215-
reader.close();
216-
} catch (IOException logOrIgnore) {
217-
logOrIgnore.printStackTrace();
218-
}
219-
if (socket != null) {
220-
try {
221-
socket.close();
222-
} catch (IOException logOrIgnore) {
223-
logOrIgnore.printStackTrace();
224-
}
225-
}
226-
}
227-
System.out.println("Client finished." );
228-
}
229159

230160
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package jdk.test.lib;
2+
3+
import java.io.*;
4+
import java.net.*;
5+
import static java.nio.charset.StandardCharsets.UTF_8;
6+
7+
public class RawClient {
8+
9+
/**
10+
* performs an HTTP request using a raw socket.
11+
*
12+
* @param address the server address
13+
* @param port the server port
14+
* @param context the server context (i.e. endpoint)
15+
* @param data the data to send in the request body
16+
* @param contentLength the content length, if > 0, this will be used as the
17+
* Content-length header value which can be used to simulate the client
18+
* advertising more data than it sends. in almost all cases this parameter
19+
* should be 0, upon which the actual length of the data is used.
20+
* @throws Exception
21+
*/
22+
public static void runRawSocketHttpClient(InetAddress address, int port, String context, String data, int contentLength)
23+
throws Exception {
24+
Socket socket = null;
25+
PrintWriter writer = null;
26+
BufferedReader reader = null;
27+
final String CRLF = "\r\n";
28+
try {
29+
socket = new Socket(address, port);
30+
writer = new PrintWriter(new OutputStreamWriter(
31+
socket.getOutputStream()));
32+
System.out.println("Client connected by socket: " + socket);
33+
String body = data == null ? "" : data;
34+
if (contentLength <= 0) {
35+
contentLength = body.getBytes(UTF_8).length;
36+
}
37+
38+
writer.print("GET " + context + "/ HTTP/1.1" + CRLF);
39+
writer.print("User-Agent: Java/"
40+
+ System.getProperty("java.version")
41+
+ CRLF);
42+
writer.print("Host: " + address.getHostName() + CRLF);
43+
writer.print("Accept: */*" + CRLF);
44+
writer.print("Content-Length: " + contentLength + CRLF);
45+
writer.print("Connection: keep-alive" + CRLF);
46+
writer.print(CRLF); // Important, else the server will expect that
47+
// there's more into the request.
48+
writer.flush();
49+
System.out.println("Client wrote request to socket: " + socket);
50+
writer.print(body);
51+
writer.flush();
52+
53+
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
54+
System.out.println("Client start reading from server:");
55+
String line = reader.readLine();
56+
for (; line != null; line = reader.readLine()) {
57+
if (line.isEmpty()) {
58+
break;
59+
}
60+
System.out.println("\"" + line + "\"");
61+
}
62+
System.out.println("Client finished reading from server");
63+
} finally {
64+
// give time to the server to try & drain its input stream
65+
Thread.sleep(500);
66+
// closes the client outputstream while the server is draining
67+
// it
68+
if (writer != null) {
69+
writer.close();
70+
}
71+
// give time to the server to trigger its assertion
72+
// error before closing the connection
73+
Thread.sleep(500);
74+
if (reader != null)
75+
try {
76+
reader.close();
77+
} catch (IOException logOrIgnore) {
78+
logOrIgnore.printStackTrace();
79+
}
80+
if (socket != null) {
81+
try {
82+
socket.close();
83+
} catch (IOException logOrIgnore) {
84+
logOrIgnore.printStackTrace();
85+
}
86+
}
87+
}
88+
System.out.println("Client finished.");
89+
}
90+
91+
}
Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package robaho.net.httpserver;
22

3-
import java.io.BufferedReader;
43
import java.io.IOException;
5-
import java.io.InputStreamReader;
64
import java.io.OutputStream;
7-
import java.io.OutputStreamWriter;
8-
import java.io.PrintWriter;
95
import java.net.InetAddress;
106
import java.net.InetSocketAddress;
11-
import java.net.Socket;
127
import java.util.concurrent.ExecutorService;
138
import java.util.concurrent.Executors;
149
import java.util.concurrent.ThreadFactory;
@@ -22,7 +17,7 @@
2217

2318
import org.testng.annotations.Test;
2419

25-
import static java.nio.charset.StandardCharsets.*;
20+
import jdk.test.lib.RawClient;
2621

2722
/**
2823
* see issue #19
@@ -93,83 +88,12 @@ public void handle(HttpExchange exchange) throws IOException {
9388
System.out.println("Server started at port "
9489
+ server.getAddress().getPort());
9590

96-
runRawSocketHttpClient(loopback, server.getAddress().getPort(), -1);
91+
RawClient.runRawSocketHttpClient(loopback, server.getAddress().getPort(),someContext,"I will send all of the data", -1);
9792
} finally {
9893
System.out.println("shutting server down");
9994
executor.shutdown();
10095
server.stop(0);
10196
}
10297
System.out.println("Server finished.");
10398
}
104-
105-
static void runRawSocketHttpClient(InetAddress address, int port, int contentLength)
106-
throws Exception {
107-
Socket socket = null;
108-
PrintWriter writer = null;
109-
BufferedReader reader = null;
110-
final String CRLF = "\r\n";
111-
try {
112-
socket = new Socket(address, port);
113-
writer = new PrintWriter(new OutputStreamWriter(
114-
socket.getOutputStream()));
115-
System.out.println("Client connected by socket: " + socket);
116-
String body = "I will send all the data.";
117-
if (contentLength <= 0) {
118-
contentLength = body.getBytes(UTF_8).length;
119-
}
120-
121-
writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF);
122-
writer.print("User-Agent: Java/"
123-
+ System.getProperty("java.version")
124-
+ CRLF);
125-
writer.print("Host: " + address.getHostName() + CRLF);
126-
writer.print("Accept: */*" + CRLF);
127-
writer.print("Content-Length: " + contentLength + CRLF);
128-
writer.print("Connection: keep-alive" + CRLF);
129-
writer.print(CRLF); // Important, else the server will expect that
130-
// there's more into the request.
131-
writer.flush();
132-
System.out.println("Client wrote request to socket: " + socket);
133-
writer.print(body);
134-
writer.flush();
135-
136-
reader = new BufferedReader(new InputStreamReader(
137-
socket.getInputStream()));
138-
System.out.println("Client start reading from server:");
139-
String line = reader.readLine();
140-
for (; line != null; line = reader.readLine()) {
141-
if (line.isEmpty()) {
142-
break;
143-
}
144-
System.out.println("\"" + line + "\"");
145-
}
146-
System.out.println("Client finished reading from server");
147-
} finally {
148-
// give time to the server to try & drain its input stream
149-
Thread.sleep(500);
150-
// closes the client outputstream while the server is draining
151-
// it
152-
if (writer != null) {
153-
writer.close();
154-
}
155-
// give time to the server to trigger its assertion
156-
// error before closing the connection
157-
Thread.sleep(500);
158-
if (reader != null)
159-
try {
160-
reader.close();
161-
} catch (IOException logOrIgnore) {
162-
logOrIgnore.printStackTrace();
163-
}
164-
if (socket != null) {
165-
try {
166-
socket.close();
167-
} catch (IOException logOrIgnore) {
168-
logOrIgnore.printStackTrace();
169-
}
170-
}
171-
}
172-
System.out.println("Client finished.");
173-
}
174-
17599
}

0 commit comments

Comments
 (0)