Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Nov 20, 2021
1 parent 6834d3a commit 027d19a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,4 @@ public int statusCode() {
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());
}
}
}
44 changes: 24 additions & 20 deletions src/test/java/org/kohsuke/github/AbuseLimitHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 @@ -60,13 +59,6 @@ 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 All @@ -90,20 +82,34 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
// getting an input stream in an error case should throw
IOException ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());

InputStream errorStream = uc.getErrorStream();
assertThat(errorStream, notNullValue());
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"));
try (InputStream errorStream = uc.getErrorStream()) {
assertThat(errorStream, notNullValue());
String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
assertThat(errorString, containsString("Must have push access to repository"));
}

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

// calling again on a GitHubConnectorResponse should yield the same value
if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
try (InputStream errorStream = uc.getErrorStream()) {
assertThat(errorStream, notNullValue());
String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
assertThat(errorString, containsString("Must have push access to repository"));
}
} else {
try (InputStream errorStream = uc.getErrorStream()) {
assertThat(errorStream, notNullValue());
String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
fail();
assertThat(errorString, containsString("Must have push access to repository"));
} catch (IOException ex) {
assertThat(ex, notNullValue());
assertThat(ex.getMessage(), containsString("stream is closed"));
}
}

assertThat(uc.getHeaderFields(), instanceOf(Map.class));
assertThat(uc.getHeaderFields().size(), Matchers.greaterThan(25));
assertThat(uc.getHeaderField("Status"), equalTo("403 Forbidden"));
Expand All @@ -130,9 +136,7 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
Assert.assertThrows(IllegalStateException.class, () -> uc.setRequestProperty("bogus", "thing"));
Assert.assertThrows(IllegalStateException.class, () -> uc.setUseCaches(true));

if (connectorResponse != null) {
assertThat(uc.toString(),
containsString("GitHubConnectorResponseHttpUrlConnectionAdapter"));
if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {

Assert.assertThrows(UnsupportedOperationException.class,
() -> uc.getAllowUserInteraction());
Expand Down

0 comments on commit 027d19a

Please sign in to comment.