Skip to content

Commit

Permalink
Fix to identify unsuccessful http response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Dec 16, 2024
1 parent 550e72f commit f24f6ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import io.github.sashirestela.cleverclient.support.CleverClientException.HttpResponseInfo.HttpRequestInfo;
import io.github.sashirestela.cleverclient.support.ReturnType;
import io.github.sashirestela.cleverclient.util.CommonUtil;
import io.github.sashirestela.cleverclient.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -46,7 +46,8 @@ public abstract class HttpSender {
@SuppressWarnings("unchecked")
protected void throwExceptionIfErrorIsPresent(HttpResponse<?> response, Class<?> clazz) {
logger.debug("Response Code : {}", response.statusCode());
if (!CommonUtil.isInHundredsOf(response.statusCode(), HttpURLConnection.HTTP_OK)) {
if (CommonUtil.isBetweenHundredsOf(response.statusCode(), Constant.HTTP_CLIENT_ERROR_CODE,
Constant.HTTP_SERVER_ERROR_CODE)) {
var data = "";
if (Stream.class.equals(clazz)) {
data = ((Stream<String>) response.body())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public static <T> T[] concatArrays(T[] array1, T[] array2) {
}

public static boolean isInHundredsOf(int value, int range) {
return Math.floor(value / 100.0) * 100 == range;
return value >= range && value < (range + 100);
}

public static boolean isBetweenHundredsOf(int value, int minRange, int maxRange) {
return value >= minRange && value < (maxRange + 100);
}

public static Map<String, String> createMapString(String... keyValPairs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ private Constant() {

public static final String HTTP_ERROR_MESSAGE = "HTTP interaction failed: server returned a {0} response status.";

public static final int HTTP_CLIENT_ERROR_CODE = 400;

public static final int HTTP_SERVER_ERROR_CODE = 500;

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void shouldReturnJoinedArraysWhenTwoArePassed() {
}

@Test
void shouldDetectIfValueIsInTheRangeWhenTwoValuesAreCompared() {
void shouldDetectIfSomeValueIsInHundredsOfOneLimit() {
Object[][] testData = {
{ 199, 200, false },
{ 200, 200, true },
Expand All @@ -107,6 +107,22 @@ void shouldDetectIfValueIsInTheRangeWhenTwoValuesAreCompared() {
}
}

@Test
void shouldDetectIfSomeValueIsBetweenHundredsOfTwoLimits() {
Object[][] testData = {
{ 399, 400, 500, false },
{ 400, 400, 500, true },
{ 500, 400, 500, true },
{ 599, 400, 500, true },
{ 600, 400, 500, false }
};
for (var data : testData) {
var actualResult = CommonUtil.isBetweenHundredsOf((int) data[0], (int) data[1], (int) data[2]);
var expectedResult = (boolean) data[3];
assertEquals(expectedResult, actualResult);
}
}

@Test
void shouldCreateMapStringWhenAStringListIsPassed() {
var expectedMap = new HashMap<String, String>();
Expand Down

0 comments on commit f24f6ca

Please sign in to comment.