Skip to content

Commit

Permalink
Modified RequestActor to read from the ErrorStream on request failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
JPage committed Jun 6, 2013
1 parent c57f6ad commit 9d096bb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
55 changes: 30 additions & 25 deletions Network/src/edu/wpi/cs/wpisuitetng/network/RequestActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,6 @@ public void run() {
else {
connection.connect();
}

// get the response body
String responseBody = "";
try {
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 1);
String line;
try {
while((line = reader.readLine()) != null) {
responseBody += line + "\n";
}
} catch (SocketTimeoutException e) {
// Note: this will be thrown if a read takes longer than 5 seconds
exceptionRecv = e;
responseBodyReadTimeout = true;
}
finally {
if (reader != null) {
reader.close();
}
}
}
catch (IOException e) {
// do nothing, received a 400, or 500 status code
}

// get the response headers
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
Expand All @@ -114,6 +89,36 @@ public void run() {

// get the response message
String responseMessage = connection.getResponseMessage();

// get the response body
String responseBody = "";
InputStream in;

if (responseCode < 400) { // if the request succeeds, get the InputStream
in = connection.getInputStream();
}
else { // if the request fails, get the ErrorStream
in = connection.getErrorStream();
}

// read response body
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 1);
String line;
try {
while((line = reader.readLine()) != null) {
responseBody += line + "\n";
}
} catch (SocketTimeoutException e) { // if there is a timeout while reading the body
exceptionRecv = e;
responseBodyReadTimeout = true;
} catch (IOException e) { // if readLine() fails
exceptionRecv = e;
}
finally { // make sure that the BufferedReader is closed
if (reader != null) {
reader.close();
}
}

// create Response
ResponseModel response = new Response(responseCode, responseMessage, responseHeaders, responseBody);
Expand Down
38 changes: 37 additions & 1 deletion Network/test/edu/wpi/cs/wpisuitetng/network/TestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void testSetResponseIllegalStateException() {
}

/**
* Test that communication works. DummyServer must be running on port 8080 for this test to work.
* Test that communication works.
*/
@Test
public void testRequestCommunication() {
Expand Down Expand Up @@ -235,4 +235,40 @@ public void testRequestCommunication() {
e.printStackTrace();
}
}

/**
* Test to ensure that the response body can be read when a status code of 400 or above is received.
*/
@Test
public void testRequestResponseCode400Plus() {
// The response body
String body = "Some error";

DummyServer server = new DummyServer(port);

// Construct and configure a canned response for the server to return
ResponseModel cannedResponse = new ResponseModel();
cannedResponse.setStatusCode(400); // status code: 400
cannedResponse.setBody(body); // body: "Some error"

// Make a new POST Request.
Request manualRequest = new Request(config, null, HttpMethod.POST); // construct the Request
manualRequest.setBody(body); // set the request body to send to the server
manualRequest.clearAsynchronous(); // make this request synchronously so that the test is blocked

// set the canned response and start the server
server.setResponse(cannedResponse);
server.start();

// Send the request!
manualRequest.send();

assertTrue(manualRequest.getResponse().getBody().contains(body)); // check that the message in the body was read
assertEquals(400, manualRequest.getResponse().getStatusCode());

assertTrue(body.equals(server.getLastReceived().getBody()));
assertEquals(manualRequest.getHttpMethod(), server.getLastReceived().getHttpMethod());

server.stop();
}
}

0 comments on commit 9d096bb

Please sign in to comment.