🤬
  • Added a default case to HttpStatus to handle invalid HTTP status better.

    PiperOrigin-RevId: 464635818
    Change-Id: Iec3bbf9b4199c766cc9eeae1d723d9c5376a1a95
  • Loading...
  • Annie Mao committed with Copybara-Service 2 years ago
    bf8a2e29
    1 parent 8a56c33d
  • ■ ■ ■ ■ ■ ■
    common/src/main/java/com/google/tsunami/common/net/http/HttpStatus.java
    skipped 19 lines
    20 20  import com.google.common.collect.ImmutableMap;
    21 21  import java.util.Arrays;
    22 22  import java.util.function.Function;
    23  -import javax.annotation.Nullable;
    24 23   
    25 24  /**
    26 25   * HTTP Status Codes defined in RFC 2616, RFC 6585, RFC 4918 and RFC 7538.
    skipped 8 lines
    35 34   * target="_top">https://tools.ietf.org/html/rfc7538</a>
    36 35   */
    37 36  public enum HttpStatus {
     37 + // Default
     38 + HTTP_STATUS_UNSPECIFIED(0, "Status Unspecified"),
    38 39   
    39 40   // Informational 1xx
    40 41   CONTINUE(100, "Continue"),
    skipped 74 lines
    115 116   * @param code the HTTP status code.
    116 117   * @return the matching {@link HttpStatus} from the given status code.
    117 118   */
    118  - @Nullable
    119 119   public static HttpStatus fromCode(int code) {
    120  - return BY_CODE.get(code);
     120 + HttpStatus status = BY_CODE.get(code);
     121 + return status == null ? HTTP_STATUS_UNSPECIFIED : status;
    121 122   }
    122 123   
    123 124   private final int code;
    skipped 35 lines
  • ■ ■ ■ ■ ■ ■
    common/src/test/java/com/google/tsunami/common/net/http/HttpResponseTest.java
    skipped 133 lines
    134 134   
    135 135   assertTrue(httpResponse.jsonFieldEqualsToValue("field", "value"));
    136 136   }
     137 + 
     138 + @Test
     139 + public void bodyJson_whenHttpStatusInvalid_parseSucceeds() {
     140 + HttpResponse httpResponse =
     141 + HttpResponse.builder()
     142 + .setStatus(HttpStatus.HTTP_STATUS_UNSPECIFIED)
     143 + .setHeaders(HttpHeaders.builder().build())
     144 + .setResponseUrl(TEST_URL)
     145 + .build();
     146 + 
     147 + assertFalse(httpResponse.status().isSuccess());
     148 + }
    137 149  }
    138 150   
Please wait...
Page is in error, reload to recover