Skip to content

Commit

Permalink
Add more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Nov 16, 2021
1 parent 4846442 commit 8effe61
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,25 @@ public Map<String, List<String>> allHeaders() {
return headers;
}

/**
* Unwraps a {@link GitHubConnectorResponse} from a {@link HttpURLConnection} adapter.
*
* Only works on the internal {@link GitHubConnectorResponseHttpUrlConnectionAdapter}.
*
* @param connection
* the connection to unwrap.
* @return an unwrapped response from an adapter.
* @throws UnsupportedOperationException
* if the connection is not an adapter.
* @deprecated Only preset for testing and interaction with deprecated HttpURLConnection components.
*/
@Deprecated
public final static GitHubConnectorResponse fromHttpURLConnectionAdapter(HttpURLConnection connection) {
if (connection instanceof GitHubConnectorResponseHttpUrlConnectionAdapter) {
return ((GitHubConnectorResponseHttpUrlConnectionAdapter) connection).connectorResponse();
} else {
throw new UnsupportedOperationException(
"Cannot unwrap GitHubConnectorResponse from " + connection.getClass().getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
import java.security.Permission;
import java.util.*;

/**
* Adapter class for {@link org.kohsuke.github.connector.GitHubConnectorResponse} to be usable as a
* {@link HttpURLConnection}.
*
* Behavior is equivalent to a {@link HttpURLConnection} after {@link HttpURLConnection#connect()} has been called.
* Methods that make no sense throw {@link UnsupportedOperationException}.
*/
@Deprecated
class GitHubConnectorResponseHttpUrlConnectionAdapter extends HttpURLConnection {

private final GitHubConnectorResponse connectorResponse;
Expand All @@ -19,6 +27,16 @@ public GitHubConnectorResponseHttpUrlConnectionAdapter(GitHubConnectorResponse c
this.connectorResponse = connectorResponse;
}

/**
* Readable to support {@link GitHubConnectorResponse#fromHttpURLConnectionAdapter(HttpURLConnection)} which only
* exist for testing.
*
* @return
*/
GitHubConnectorResponse connectorResponse() {
return connectorResponse;
}

@Override
public String getHeaderFieldKey(int n) {
List<String> keys = new ArrayList<>(connectorResponse.allHeaders().keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public InputStream bodyStream() throws IOException {
return inputBytes == null ? null : new ByteArrayInputStream(inputBytes);
}

/**
* {@inheritDoc}
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" },
justification = "Internal implementation class. Should not be used externally.")
@Nonnull
Expand Down
22 changes: 17 additions & 5 deletions src/test/java/org/kohsuke/github/AbuseLimitHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.kohsuke.github.connector.GitHubConnectorResponse;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -59,6 +60,13 @@ public void testHandler_Fail() throws Exception {
@Override
public void onError(IOException e, HttpURLConnection uc) throws IOException {

GitHubConnectorResponse connectorResponse = null;
try {
connectorResponse = GitHubConnectorResponse.fromHttpURLConnectionAdapter(uc);
} catch (UnsupportedOperationException ex) {
assertThat(ex.getMessage(), startsWith("Cannot unwrap GitHubConnectorResponse"));
}

// Verify
assertThat(uc.getDate(), Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
assertThat(uc.getExpiration(), equalTo(0L));
Expand Down Expand Up @@ -87,6 +95,12 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
String error = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
assertThat(error, containsString("Must have push access to repository"));

if (connectorResponse != null) {
String connectorBody = IOUtils.toString(connectorResponse.bodyStream(),
StandardCharsets.UTF_8);
assertThat(connectorBody, containsString("Must have push access to repository"));
}

// calling again should still error
ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());

Expand Down Expand Up @@ -116,12 +130,10 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
Assert.assertThrows(IllegalStateException.class, () -> uc.setRequestProperty("bogus", "thing"));
Assert.assertThrows(IllegalStateException.class, () -> uc.setUseCaches(true));

boolean isAdapter = false;
if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
isAdapter = true;
}
if (connectorResponse != null) {
assertThat(uc.toString(),
containsString("GitHubConnectorResponseHttpUrlConnectionAdapter"));

if (isAdapter) {
Assert.assertThrows(UnsupportedOperationException.class,
() -> uc.getAllowUserInteraction());
Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getConnectTimeout());
Expand Down

0 comments on commit 8effe61

Please sign in to comment.