■ ■ ■ ■ ■ ■
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 | | |