Skip to content

Commit

Permalink
evaluate any status code between 200-299 as sucess when using HTTPSen…
Browse files Browse the repository at this point in the history
…der, fixes mp911de#114
  • Loading branch information
madmuffin1 committed Mar 22, 2017
1 parent ebd832e commit 05cb66c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/
public class GelfHTTPSender implements GelfSender {

private static final int HTTP_ACCEPTED_STATUS = 202;
private static final int HTTP_SUCCESSFUL_LOWER_BOUND = 200;
private static final int HTTP_SUCCESSFUL_UPPER_BOUND = 299;

private final int connectTimeoutMs;
private final int readTimeoutMs;
Expand Down Expand Up @@ -60,7 +61,7 @@ public boolean sendMessage(GelfMessage message) {
outputStream.close();

int responseCode = connection.getResponseCode();
if (responseCode == HTTP_ACCEPTED_STATUS) {
if (responseCode >= HTTP_SUCCESSFUL_LOWER_BOUND && responseCode <= HTTP_SUCCESSFUL_UPPER_BOUND) {
return true;
} else {
errorReporter.reportError("Server responded with unexpected status code: " + responseCode, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void tearDown() {
}

@Test
public void sendMessageTest() throws IOException {
public void sendMessageTestWithAcceptedResponse() throws IOException {

server.setReturnStatus(HttpResponseStatus.ACCEPTED);

Expand All @@ -85,6 +85,28 @@ public void sendMessageTest() throws IOException {
assertThat(messageJson.get("level")).isEqualTo(gelfMessage.getLevel());
}

@Test
public void sendMessageTestWithCreatedResponse() throws IOException {

server.setReturnStatus(HttpResponseStatus.CREATED);

GelfMessage gelfMessage = new GelfMessage("shortMessage", "fullMessage", 12121L, "WARNING");

boolean success = sender.sendMessage(gelfMessage);

assertThat(success).isTrue();
verifyZeroInteractions(errorReporter);

List<Object> jsonValues = server.getJsonValues();
assertThat(jsonValues).hasSize(1);

Map<String, Object> messageJson = (Map<String, Object>) jsonValues.get(0);
assertThat(messageJson.get("short_message")).isEqualTo(gelfMessage.getShortMessage());
assertThat(messageJson.get("full_message")).isEqualTo(gelfMessage.getFullMessage());
assertThat(messageJson.get("timestamp")).isEqualTo(gelfMessage.getTimestamp());
assertThat(messageJson.get("level")).isEqualTo(gelfMessage.getLevel());
}

@Test
public void shouldUsePostHttpMethod() throws IOException {

Expand Down

0 comments on commit 05cb66c

Please sign in to comment.