SimpleWebServer for HTTP Header Testing

SimpleWebServer is socket server. It listens for incoming connections and echo the Content type back to the client with expected response code and some data. This server mainly facilitates HTTP header testing when message routes though WSO2 ESB.


Initializing the Server

You can start the new server instance by providing port and expected response code.  

SimpleWebServer simpleWebServer = new SimpleWebServer(9006, 200);
simpleWebServer.start();

Stop the server

simpleWebServer.terminate();

Sample Test Case

Following test case verifies HTTP header content types of ESB outbound and inbound messages. The client sends HTTP request with different content types. The server set the same contentType and expected response code to the HTTP response and send it back.


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
public class SimpleWebServer extends Thread {
private volatile boolean running = true;
private int port;
private int expectedResponseCode;
protected Log log = LogFactory.getLog(SimpleWebServer.class);
public SimpleWebServer(int listenPort, int expectedResponseCode) {
this.port = listenPort;
this.expectedResponseCode = expectedResponseCode;
}
public void run() {
ServerSocket serverSocket = null;
Socket connectionSocket = null;
try {
log.info("Trying to bind to localhost on port " + Integer.toString(port) + "...");
serverSocket = new ServerSocket(port);
log.info("Running Simple WebServer!\n");
connectionSocket = serverSocket.accept();
connectionSocket.setSoTimeout(30000);
//go in a infinite loop, wait for connections, process request, send response
while (running) {
log.info("\nReady, Waiting for requests...\n");
try {
//this call waits/blocks until someone connects to the port we
BufferedReader input;
if (!connectionSocket.isClosed()) {
InetAddress client = connectionSocket.getInetAddress();
log.info(client.getHostName() + " connected to server.\n");
input = new BufferedReader(new InputStreamReader(connectionSocket.
getInputStream(), Charset.defaultCharset()));
if (input.ready()) {
DataOutputStream output =
new DataOutputStream(connectionSocket.getOutputStream());
httpHandler(input, output);
}
}
//Prepare a outputStream. This will be used sending back our response
//(header + requested file) to the client.
} catch (Exception e) { //catch any errors, and print them
log.info("\nError:" + e.getMessage());
running = false;
}
}
} catch (Exception e) {
log.error("\nFatal Error:" + e.getMessage());
running = false;
} finally {
try {
if (connectionSocket != null) {
connectionSocket.close();
serverSocket.close();
}
} catch (IOException e) {
log.error("Error while shutting down server sockets", e);
}
}
}
private void httpHandler(BufferedReader input, DataOutputStream output)
throws IOException, InterruptedException {
String contentType;
String tmp;
try {
tmp = input.readLine(); //read from the stream
contentType = input.readLine();
log.info(tmp);
assert tmp != null;
String sampleReturnResponse = "<testResponse>\n" +
" <message>" + tmp.toUpperCase() + "Success</message>\n" +
" </testResponse>";
output.writeBytes(constructHttpHeader(expectedResponseCode, contentType));
output.write(sampleReturnResponse.getBytes(Charset.defaultCharset()));
} catch (Exception e) {
log.error("error" + e.getMessage());
} finally {
output.flush();
Thread.sleep(1000);
input.close();
output.close();
}
}
private String constructHttpHeader(int returnCode, String contentType) {
String header = "HTTP/1.0 ";
switch (returnCode) {
case 200:
header = header + "200 OK";
break;
case 400:
header = header + "400 Bad Request";
break;
case 403:
header = header + "403 Forbidden";
break;
case 404:
header = header + "404 Not Found";
break;
case 500:
header = header + "500 Internal Server Error";
break;
case 501:
header = header + "501 Not Implemented";
break;
case 503:
header = header + "503 Error";
break;
default:
header = header + returnCode + " Testing response code";
}
header = header + "\r\n"; //other header fields,
header = header + "Connection: close\r\n"; //can't handle persistent connections
header = header + "Server: SimpleWebServer\r\n"; //server name
header = header + contentType + "\r\n";
header = header + "\r\n";
return header;
}
public void terminate() throws IOException {
running = false;
}
}