📄 testhttpmirrorthread.java
字号:
outputStream.write(bos.toByteArray());
// Read the response
ByteArrayOutputStream response = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while((length = inputStream.read(buffer)) != -1) {
response.write(buffer, 0, length);
}
response.close();
byte[] mirroredResponse = getMirroredResponse(response.toByteArray());
// Check that the request and response matches
checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
// Close the connection
clientSocket.close();
// Connect to the http server, and do a simple http post, with
// a pause after transmitting the headers
clientSocket = new Socket("localhost", HTTP_SERVER_PORT);
outputStream = clientSocket.getOutputStream();
inputStream = clientSocket.getInputStream();
// Write to the socket
bos = new ByteArrayOutputStream();
// Headers
bos.write("GET / HTTP 1.1".getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write("Host: localhost".getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(("Content-type: text/plain; charset=" + ISO_8859_1).getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(("Content-length: " + postBody.length).getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(CRLF);
bos.close();
// Write the headers, and then sleep
bos.close();
byte[] firstChunk = bos.toByteArray();
outputStream.write(firstChunk);
Thread.sleep(300);
// Write the body
byte[] secondChunk = postBody;
outputStream.write(secondChunk);
// Read the response
response = new ByteArrayOutputStream();
buffer = new byte[1024];
length = 0;
while((length = inputStream.read(buffer)) != -1) {
response.write(buffer, 0, length);
}
response.close();
mirroredResponse = getMirroredResponse(response.toByteArray());
// The content sent
bos = new ByteArrayOutputStream();
bos.write(firstChunk);
bos.write(secondChunk);
bos.close();
// Check that the request and response matches
checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
// Close the connection
clientSocket.close();
// Connect to the http server, and do a simple http post with utf-8
// encoding of the body, which caused problems when reader/writer
// classes were used in the HttpMirrorThread
clientSocket = new Socket("localhost", HTTP_SERVER_PORT);
outputStream = clientSocket.getOutputStream();
inputStream = clientSocket.getInputStream();
// Construct body
postBodyBuffer = new StringBuffer();
for(int i = 0; i < 1000; i++) {
postBodyBuffer.append("\u0364\u00c5\u2052");
}
postBody = postBodyBuffer.toString().getBytes(UTF_8);
// Write to the socket
bos = new ByteArrayOutputStream();
// Headers
bos.write("GET / HTTP 1.1".getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write("Host: localhost".getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(("Content-type: text/plain; charset=" + UTF_8).getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(("Content-length: " + postBody.length).getBytes(ISO_8859_1));
bos.write(CRLF);
bos.write(CRLF);
bos.close();
// Write the headers, and then sleep
bos.close();
firstChunk = bos.toByteArray();
outputStream.write(firstChunk);
Thread.sleep(300);
// Write the body
secondChunk = postBody;
outputStream.write(secondChunk);
// Read the response
response = new ByteArrayOutputStream();
buffer = new byte[1024];
length = 0;
while((length = inputStream.read(buffer)) != -1) {
response.write(buffer, 0, length);
}
response.close();
mirroredResponse = getMirroredResponse(response.toByteArray());
// The content sent
bos = new ByteArrayOutputStream();
bos.write(firstChunk);
bos.write(secondChunk);
bos.close();
// Check that the request and response matches
checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
// Close the connection
clientSocket.close();
}
/*
public void testPostRequestChunked() throws Exception {
// TODO - implement testing of chunked post request
}
*/
/**
* Check that the the two byte arrays have identical content
*
* @param expected
* @param actual
* @throws UnsupportedEncodingException
*/
private void checkArraysHaveSameContent(byte[] expected, byte[] actual) throws UnsupportedEncodingException {
if(expected != null && actual != null) {
if(expected.length != actual.length) {
System.out.println(">>>>>>>>>>>>>>>>>>>> (expected) : length " + expected.length);
System.out.println(new String(expected,"UTF-8"));
System.out.println("==================== (actual) : length " + actual.length);
System.out.println(new String(actual,"UTF-8"));
System.out.println("<<<<<<<<<<<<<<<<<<<<");
fail("arrays have different length, expected is " + expected.length + ", actual is " + actual.length);
}
else {
for(int i = 0; i < expected.length; i++) {
if(expected[i] != actual[i]) {
System.out.println(">>>>>>>>>>>>>>>>>>>> (expected) : length " + expected.length);
System.out.println(new String(expected,0,i+1));
System.out.println("==================== (actual) : length " + actual.length);
System.out.println(new String(actual,0,i+1));
System.out.println("<<<<<<<<<<<<<<<<<<<<");
/*
// Useful to when debugging
for(int j = 0; j < expected.length; j++) {
System.out.print(expected[j] + " ");
}
System.out.println();
for(int j = 0; j < actual.length; j++) {
System.out.print(actual[j] + " ");
}
System.out.println();
*/
fail("byte at position " + i + " is different, expected is " + expected[i] + ", actual is " + actual[i]);
}
}
}
}
else {
fail("expected or actual byte arrays were null");
}
}
private byte[] getMirroredResponse(byte[] allResponse) {
// The response includes the headers from the mirror server,
// we want to skip those, to only keep the content mirrored.
// Look for the first CRLFCRLF section
int startOfMirrorResponse = 0;
for(int i = 0; i < allResponse.length; i++) {
// TODO : This is a bit fragile
if(allResponse[i] == 0x0d && allResponse[i+1] == 0x0a && allResponse[i+2] == 0x0d && allResponse[i+3] == 0x0a) {
startOfMirrorResponse = i + 4;
break;
}
}
byte[] mirrorResponse = new byte[allResponse.length - startOfMirrorResponse];
System.arraycopy(allResponse, startOfMirrorResponse, mirrorResponse, 0, mirrorResponse.length);
return mirrorResponse;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -