mirror of
https://github.com/google/nomulus.git
synced 2025-07-25 20:18:34 +02:00
Check for null error stream (#2249)
This commit is contained in:
parent
01f868cefc
commit
4893ea307b
2 changed files with 20 additions and 0 deletions
|
@ -50,6 +50,8 @@ public final class UrlConnectionUtils {
|
||||||
try (InputStream is =
|
try (InputStream is =
|
||||||
responseCode < 400 ? connection.getInputStream() : connection.getErrorStream()) {
|
responseCode < 400 ? connection.getInputStream() : connection.getErrorStream()) {
|
||||||
return ByteStreams.toByteArray(is);
|
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.verifyNoMoreInteractions;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -104,4 +105,21 @@ public class UrlConnectionUtilsTest {
|
||||||
"Multipart data contains autogenerated boundary: "
|
"Multipart data contains autogenerated boundary: "
|
||||||
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
+ "------------------------------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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue