Skip to content

Commit

Permalink
HttpOpener: Close input stream resources. (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Jan 11, 2024
1 parent a9d569b commit 83e5d9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
10 changes: 7 additions & 3 deletions metafacture-io/src/main/java/org/metafacture/io/HttpOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,16 @@ public void process(final String input) {
final HttpURLConnection connection = requestBody != null ?
doOutput(urlToOpen, requestBody) : doRedirects(urlToOpen);

final InputStream inputStream = getInputStream(connection);
final String charset = getContentCharset(connection);

getReceiver().process(new InputStreamReader(
try (
InputStream inputStream = getInputStream(connection);
Reader reader = new InputStreamReader(
"gzip".equalsIgnoreCase(connection.getContentEncoding()) ?
new GZIPInputStream(inputStream) : inputStream, charset));
new GZIPInputStream(inputStream) : inputStream, charset)
) {
getReceiver().process(reader);
}
}
catch (final IOException e) {
throw new MetafactureException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

package org.metafacture.io;

import org.metafacture.commons.ResourceUtil;
import org.metafacture.framework.ObjectReceiver;

import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
Expand All @@ -34,10 +31,7 @@
import org.junit.ComparisonFailure;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

Expand All @@ -49,8 +43,6 @@
import java.util.function.Consumer;
import java.util.zip.GZIPOutputStream;

import static org.mockito.Mockito.times;

/**
* Tests for class {@link HttpOpener}.
*
Expand Down Expand Up @@ -92,9 +84,6 @@ public final class HttpOpenerTest {
@Mock
private ObjectReceiver<Reader> receiver;

@Captor
private ArgumentCaptor<Reader> processedObject;

@Test
public void shouldPerformGetRequestWithInputAsUrlByDefault() throws IOException {
shouldPerformRequest(TEST_URL, HttpOpener.Method.GET, (o, u) -> {});
Expand Down Expand Up @@ -341,17 +330,18 @@ private void shouldPerformRequest(final String input, final HttpOpener.Method me

WireMock.stubFor(stub);

opener.process(String.format(input, baseUrl));
TestHelpers.assertReader(receiver, responseBody, () -> {
opener.process(String.format(input, baseUrl));

// use the opener a second time in a workflow:
opener.process(String.format(input, baseUrl));
// use the opener a second time in a workflow:
opener.process(String.format(input, baseUrl));

opener.closeStream();
opener.closeStream();

WireMock.verify(request);
return 2;
});

Mockito.verify(receiver, times(2)).process(processedObject.capture());
Assert.assertEquals(responseBody, ResourceUtil.readAll(processedObject.getValue()));
WireMock.verify(request);
}

}

0 comments on commit 83e5d9e

Please sign in to comment.