🤬
  • Add a util method in HttpResponse to check existence of a key/value pair in the json response.

    PiperOrigin-RevId: 461957965
    Change-Id: I909283ef61b151ae841546eefd61be13d6ddbf08
  • Loading...
  • Annie Mao committed with Copybara-Service 2 years ago
    52b72f98
    1 parent be3368f2
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    common/src/main/java/com/google/tsunami/common/net/http/HttpResponse.java
    skipped 19 lines
    20 20  import com.google.errorprone.annotations.Immutable;
    21 21  import com.google.gson.JsonElement;
    22 22  import com.google.gson.JsonParser;
     23 +import com.google.gson.JsonPrimitive;
    23 24  import com.google.protobuf.ByteString;
    24 25  import java.util.Optional;
    25 26  import okhttp3.HttpUrl;
    skipped 31 lines
    57 58   @Memoized
    58 59   public Optional<JsonElement> bodyJson() {
    59 60   return bodyString().map(JsonParser::parseString);
     61 + }
     62 + 
     63 + /**
     64 + * Tries to determine if a given field in Json response is equal to a specific value. If parsing
     65 + * failed, {@link com.google.gson.JsonSyntaxException} or {@link IllegalStateException} will be
     66 + * thrown.
     67 + *
     68 + * @return boolean
     69 + */
     70 + public boolean jsonFieldEqualsToValue(String fieldname, String value) {
     71 + Optional<JsonPrimitive> jsonPrimitive =
     72 + bodyJson()
     73 + .map(JsonElement::getAsJsonObject)
     74 + .map(object -> object.getAsJsonPrimitive(fieldname));
     75 + return jsonPrimitive.isPresent() && jsonPrimitive.get().getAsString().equals(value);
    60 76   }
    61 77   
    62 78   public static Builder builder() {
    skipped 17 lines
  • ■ ■ ■ ■ ■ ■
    common/src/test/java/com/google/tsunami/common/net/http/HttpResponseTest.java
    skipped 16 lines
    17 17   
    18 18  import static com.google.common.truth.Truth.assertThat;
    19 19  import static com.google.common.truth.Truth8.assertThat;
     20 +import static org.junit.Assert.assertFalse;
    20 21  import static org.junit.Assert.assertThrows;
     22 +import static org.junit.Assert.assertTrue;
    21 23   
    22 24  import com.google.gson.JsonSyntaxException;
    23 25  import com.google.protobuf.ByteString;
    skipped 53 lines
    77 79   .build();
    78 80   
    79 81   assertThrows(JsonSyntaxException.class, httpResponse::bodyJson);
     82 + }
     83 + 
     84 + @Test
     85 + public void bodyJson_whenEmptyBodyResponseBody_throwsJsonSyntaxException() {
     86 + HttpResponse httpResponse =
     87 + HttpResponse.builder()
     88 + .setStatus(HttpStatus.OK)
     89 + .setHeaders(HttpHeaders.builder().build())
     90 + .setBodyBytes(ByteString.copyFromUtf8(""))
     91 + .setResponseUrl(TEST_URL)
     92 + .build();
     93 + 
     94 + assertThrows(
     95 + IllegalStateException.class, () -> httpResponse.jsonFieldEqualsToValue("field", "value"));
     96 + }
     97 + 
     98 + @Test
     99 + public void jsonFieldEqualsToValue_whenEmptyJsonResponseBody_returnsFalse() {
     100 + HttpResponse httpResponse =
     101 + HttpResponse.builder()
     102 + .setStatus(HttpStatus.OK)
     103 + .setHeaders(HttpHeaders.builder().build())
     104 + .setBodyBytes(ByteString.copyFromUtf8("{}"))
     105 + .setResponseUrl(TEST_URL)
     106 + .build();
     107 + 
     108 + assertFalse(httpResponse.jsonFieldEqualsToValue("field", "value"));
     109 + }
     110 + 
     111 + @Test
     112 + public void jsonFieldEqualsToValue_whenNonJsonResponseBody_throwsJsonSyntaxException() {
     113 + HttpResponse httpResponse =
     114 + HttpResponse.builder()
     115 + .setStatus(HttpStatus.OK)
     116 + .setHeaders(HttpHeaders.builder().build())
     117 + .setBodyBytes(ByteString.copyFromUtf8("not a json"))
     118 + .setResponseUrl(TEST_URL)
     119 + .build();
     120 + 
     121 + assertThrows(
     122 + JsonSyntaxException.class, () -> httpResponse.jsonFieldEqualsToValue("field", "value"));
     123 + }
     124 + 
     125 + @Test
     126 + public void jsonFieldEqualsToValue_whenJsonFieldContainsValue_returnsTrue() {
     127 + HttpResponse httpResponse =
     128 + HttpResponse.builder()
     129 + .setStatus(HttpStatus.OK)
     130 + .setHeaders(HttpHeaders.builder().build())
     131 + .setBodyBytes(ByteString.copyFromUtf8("{\"field\": \"value\"}"))
     132 + .setResponseUrl(TEST_URL)
     133 + .build();
     134 + 
     135 + assertTrue(httpResponse.jsonFieldEqualsToValue("field", "value"));
    80 136   }
    81 137  }
    82 138   
Please wait...
Page is in error, reload to recover