Skip to content

Commit b2641c0

Browse files
committed
Update GitHubConnectorResponse.bodyStream() implementation
1 parent d47b1cf commit b2641c0

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@
169169
<!-- Unbridged test changes -->
170170
<exclude>org.kohsuke.github.GHCommit.GHAuthor</exclude>
171171

172-
<!-- Ignore default interface method coverage -->
173-
<exclude>org.kohsuke.github.connector.GitHubConnectorRequest</exclude>
174-
175172
<!-- TODO: Some coverage, but more needed -->
176173
<exclude>org.kohsuke.github.GHIssue.PullRequest</exclude>
177174
<exclude>org.kohsuke.github.GHCommitSearchBuilder</exclude>

src/main/java/org/kohsuke/github/connector/GitHubConnectorResponse.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public String header(String name) {
8787
public abstract InputStream bodyStream() throws IOException;
8888

8989
/**
90-
* Gets the {@link GitHubConnectorRequest} for this response.
90+
* Gets the {@link GitHubConnector} for this response.
9191
*
92-
* @return the {@link GitHubConnectorRequest} for this response.
92+
* @return the {@link GitHubConnector} for this response.
9393
*/
9494
@Nonnull
9595
public GitHubConnectorRequest request() {
@@ -161,7 +161,6 @@ public abstract static class ByteArrayResponse extends GitHubConnectorResponse {
161161
private boolean inputStreamRead = false;
162162
private byte[] inputBytes = null;
163163
private boolean isClosed = false;
164-
private boolean avoidBufferedResponseStream;
165164

166165
/**
167166
* Constructor for ByteArray Response
@@ -177,7 +176,6 @@ protected ByteArrayResponse(@Nonnull GitHubConnectorRequest request,
177176
int statusCode,
178177
@Nonnull Map<String, List<String>> headers) {
179178
super(request, statusCode, headers);
180-
avoidBufferedResponseStream = request.avoidBufferedResponseStream();
181179
}
182180

183181
/**
@@ -190,29 +188,27 @@ public InputStream bodyStream() throws IOException {
190188
throw new IOException("Response is closed");
191189
}
192190

193-
if (avoidBufferedResponseStream) {
194-
synchronized (this) {
195-
if (inputStreamRead) {
196-
throw new IOException("Response is already consumed");
197-
}
198-
inputStreamRead = true;
199-
return wrapStream(rawBodyStream());
200-
}
201-
}
202-
203191
synchronized (this) {
192+
InputStream body;
204193
if (!inputStreamRead) {
205-
InputStream rawStream = rawBodyStream();
206-
try (InputStream stream = wrapStream(rawStream)) {
207-
if (stream != null) {
208-
inputBytes = IOUtils.toByteArray(stream);
194+
body = wrapStream(rawBodyStream());
195+
if (!request().avoidBufferedResponseStream()) {
196+
try (InputStream stream = body) {
197+
if (stream != null) {
198+
inputBytes = IOUtils.toByteArray(stream);
199+
}
209200
}
210201
}
211202
inputStreamRead = true;
203+
if (request().avoidBufferedResponseStream()) {
204+
return body;
205+
}
212206
}
213207
}
214208

215-
if (inputBytes == null) {
209+
if (request().avoidBufferedResponseStream()) {
210+
throw new IOException("Response is already consumed");
211+
} else if (inputBytes == null) {
216212
throw new IOException("Response body missing, stream null");
217213
}
218214

src/test/java/org/kohsuke/github/RequesterRetryTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ public void testGitHubIsApiUrlValid() throws Exception {
275275
*/
276276
@Test
277277
public void testResponseCodeFailureExceptions() throws Exception {
278+
// Cover default method in GitHubConnectorRequest
279+
assertThat(IGNORED_EMPTY_REQUEST.avoidBufferedResponseStream(), equalTo(false));
280+
278281
// No retry for these Exceptions
279282
GitHubConnector connector = new SendThrowingGitHubConnector<>(() -> {
280283
throw new IOException("Custom");

0 commit comments

Comments
 (0)