Skip to content

Commit

Permalink
WasapiValidator.validateResponse requires 200 HTTP responseCode
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed May 25, 2017
1 parent 00018c4 commit 5f8bbc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/edu/stanford/dlss/was/WasapiValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@

import javax.xml.bind.DatatypeConverter;

import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;

@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class WasapiValidator {

private static final int STATUS_CODE_THRESHOLD = 300;

public static boolean validateResponse(StatusLine statusLine, boolean entityIsNull) throws ClientProtocolException, HttpResponseException {
if(statusLine.getStatusCode() >= STATUS_CODE_THRESHOLD) {
if(statusLine.getStatusCode() != HttpStatus.SC_OK) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
if (entityIsNull) {
Expand Down
13 changes: 11 additions & 2 deletions test/edu/stanford/dlss/was/TestWasapiValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@ public class TestWasapiValidator {

private StatusLine validStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private StatusLine invalidStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 300, "Not Defined");
private StatusLine invalidStatusLine2 = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 201, "Whatever");

@Test(expected = ClientProtocolException.class)
public void testNullEntity() throws ClientProtocolException, HttpResponseException {
// testing that exception is thrown
WasapiValidator.validateResponse(validStatusLine, true);
}

@Test(expected = HttpResponseException.class)
public void testWrongResponseCode() throws ClientProtocolException, HttpResponseException {
public void test300ResponseCode() throws ClientProtocolException, HttpResponseException {
// testing that exception is thrown
WasapiValidator.validateResponse(invalidStatusLine, false);
}

@Test(expected = HttpResponseException.class)
public void test201ResponseCode() throws ClientProtocolException, HttpResponseException {
// testing that exception is thrown
WasapiValidator.validateResponse(invalidStatusLine2, false);
}

@Test
public void testValidResponse() throws ClientProtocolException, HttpResponseException {
public void testValidResponseCode() throws ClientProtocolException, HttpResponseException {
assertTrue(WasapiValidator.validateResponse(validStatusLine, false));
}

Expand Down

0 comments on commit 5f8bbc5

Please sign in to comment.