mirror of
https://github.com/google/nomulus
synced 2026-01-07 22:15:30 +00:00
Check for null error stream (#2249)
This commit is contained in:
@@ -50,6 +50,8 @@ public final class UrlConnectionUtils {
|
||||
try (InputStream is =
|
||||
responseCode < 400 ? connection.getInputStream() : connection.getErrorStream()) {
|
||||
return ByteStreams.toByteArray(is);
|
||||
} catch (NullPointerException e) {
|
||||
return new byte[] {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -104,4 +105,21 @@ public class UrlConnectionUtilsTest {
|
||||
"Multipart data contains autogenerated boundary: "
|
||||
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorStream() throws Exception {
|
||||
HttpsURLConnection connection = mock(HttpsURLConnection.class);
|
||||
when(connection.getResponseCode()).thenReturn(400);
|
||||
when(connection.getErrorStream())
|
||||
.thenReturn(new ByteArrayInputStream("Failure".getBytes(UTF_8)));
|
||||
assertThat(UrlConnectionUtils.getResponseBytes(connection))
|
||||
.isEqualTo("Failure".getBytes(UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorStream_null() throws Exception {
|
||||
HttpsURLConnection connection = mock(HttpsURLConnection.class);
|
||||
when(connection.getResponseCode()).thenReturn(400);
|
||||
assertThat(UrlConnectionUtils.getResponseBytes(connection)).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user