Skip to content

Adding null checks for instantiating InstrHttpInputStream/InstrHttpOutputStream #3415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ public InputStream getInputStream() throws IOException {

try {
final InputStream inputStream = httpUrlConnection.getInputStream();
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
// Make sure we don't pass in a null into InstrHttpInputStream, since InstrHttpInputStream is
// not null-safe.
if (inputStream != null) {
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
}
// Only reached when inputStream is null
return inputStream;
} catch (final IOException e) {
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);
Expand All @@ -156,8 +162,14 @@ public long getLastModified() {

public OutputStream getOutputStream() throws IOException {
try {
return new InstrHttpOutputStream(
httpUrlConnection.getOutputStream(), networkMetricBuilder, timer);
final OutputStream outputStream = httpUrlConnection.getOutputStream();
// Make sure we don't pass in a null into InstrHttpOutputStream, since InstrHttpOutputStream
// is not null-safe.
if (outputStream != null) {
return new InstrHttpOutputStream(outputStream, networkMetricBuilder, timer);
}
// Only reached when outputStream is null
return outputStream;
} catch (final IOException e) {
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public void testGetInputStreamThrowsIOException() throws IOException {
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}

@Test
public void testGetInputStreamWithNullInputStreamReturnsNull() throws IOException {
HttpURLConnection urlConnection = mockHttpUrlConnection();
when(urlConnection.getInputStream()).thenReturn(null);
assertThat(
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getInputStream())
.isNull();
}

@Test
public void testGetOutputStreamThrowsIOException() throws IOException {
HttpURLConnection urlConnection = mockHttpUrlConnection();
Expand All @@ -122,6 +131,16 @@ public void testGetOutputStreamThrowsIOException() throws IOException {
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}

@Test
public void testGetOutputStreamWithNullOuputStreamReturnsNull() throws IOException {
HttpURLConnection urlConnection = mockHttpUrlConnection();
when(urlConnection.getOutputStream()).thenReturn(null);
assertThat(
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder)
.getOutputStream())
.isNull();
}

@Test
public void testGetPermissionThrowsIOException() throws IOException {
HttpURLConnection urlConnection = mockHttpUrlConnection();
Expand Down