Skip to content

Commit

Permalink
Added a default case to HttpStatus to handle invalid HTTP status better.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 464635818
Change-Id: Iec3bbf9b4199c766cc9eeae1d723d9c5376a1a95
  • Loading branch information
maoning authored and copybara-github committed Aug 1, 2022
1 parent 8a56c33 commit bf8a2e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.function.Function;
import javax.annotation.Nullable;

/**
* HTTP Status Codes defined in RFC 2616, RFC 6585, RFC 4918 and RFC 7538.
Expand All @@ -35,6 +34,8 @@
* target="_top">https://tools.ietf.org/html/rfc7538</a>
*/
public enum HttpStatus {
// Default
HTTP_STATUS_UNSPECIFIED(0, "Status Unspecified"),

// Informational 1xx
CONTINUE(100, "Continue"),
Expand Down Expand Up @@ -115,9 +116,9 @@ public enum HttpStatus {
* @param code the HTTP status code.
* @return the matching {@link HttpStatus} from the given status code.
*/
@Nullable
public static HttpStatus fromCode(int code) {
return BY_CODE.get(code);
HttpStatus status = BY_CODE.get(code);
return status == null ? HTTP_STATUS_UNSPECIFIED : status;
}

private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,16 @@ public void jsonFieldEqualsToValue_whenJsonFieldContainsValue_returnsTrue() {

assertTrue(httpResponse.jsonFieldEqualsToValue("field", "value"));
}

@Test
public void bodyJson_whenHttpStatusInvalid_parseSucceeds() {
HttpResponse httpResponse =
HttpResponse.builder()
.setStatus(HttpStatus.HTTP_STATUS_UNSPECIFIED)
.setHeaders(HttpHeaders.builder().build())
.setResponseUrl(TEST_URL)
.build();

assertFalse(httpResponse.status().isSuccess());
}
}

0 comments on commit bf8a2e2

Please sign in to comment.