Skip to content

Commit 8effe61

Browse files
committed
Add more coverage
1 parent 4846442 commit 8effe61

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,25 @@ public Map<String, List<String>> allHeaders() {
113113
return headers;
114114
}
115115

116+
/**
117+
* Unwraps a {@link GitHubConnectorResponse} from a {@link HttpURLConnection} adapter.
118+
*
119+
* Only works on the internal {@link GitHubConnectorResponseHttpUrlConnectionAdapter}.
120+
*
121+
* @param connection
122+
* the connection to unwrap.
123+
* @return an unwrapped response from an adapter.
124+
* @throws UnsupportedOperationException
125+
* if the connection is not an adapter.
126+
* @deprecated Only preset for testing and interaction with deprecated HttpURLConnection components.
127+
*/
128+
@Deprecated
129+
public final static GitHubConnectorResponse fromHttpURLConnectionAdapter(HttpURLConnection connection) {
130+
if (connection instanceof GitHubConnectorResponseHttpUrlConnectionAdapter) {
131+
return ((GitHubConnectorResponseHttpUrlConnectionAdapter) connection).connectorResponse();
132+
} else {
133+
throw new UnsupportedOperationException(
134+
"Cannot unwrap GitHubConnectorResponse from " + connection.getClass().getName());
135+
}
136+
}
116137
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
import java.security.Permission;
1010
import java.util.*;
1111

12+
/**
13+
* Adapter class for {@link org.kohsuke.github.connector.GitHubConnectorResponse} to be usable as a
14+
* {@link HttpURLConnection}.
15+
*
16+
* Behavior is equivalent to a {@link HttpURLConnection} after {@link HttpURLConnection#connect()} has been called.
17+
* Methods that make no sense throw {@link UnsupportedOperationException}.
18+
*/
19+
@Deprecated
1220
class GitHubConnectorResponseHttpUrlConnectionAdapter extends HttpURLConnection {
1321

1422
private final GitHubConnectorResponse connectorResponse;
@@ -19,6 +27,16 @@ public GitHubConnectorResponseHttpUrlConnectionAdapter(GitHubConnectorResponse c
1927
this.connectorResponse = connectorResponse;
2028
}
2129

30+
/**
31+
* Readable to support {@link GitHubConnectorResponse#fromHttpURLConnectionAdapter(HttpURLConnection)} which only
32+
* exist for testing.
33+
*
34+
* @return
35+
*/
36+
GitHubConnectorResponse connectorResponse() {
37+
return connectorResponse;
38+
}
39+
2240
@Override
2341
public String getHeaderFieldKey(int n) {
2442
List<String> keys = new ArrayList<>(connectorResponse.allHeaders().keySet());

src/main/java/org/kohsuke/github/internal/GitHubConnectorHttpConnectorAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ public InputStream bodyStream() throws IOException {
193193
return inputBytes == null ? null : new ByteArrayInputStream(inputBytes);
194194
}
195195

196+
/**
197+
* {@inheritDoc}
198+
*/
196199
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" },
197200
justification = "Internal implementation class. Should not be used externally.")
198201
@Nonnull

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hamcrest.Matchers;
66
import org.junit.Assert;
77
import org.junit.Test;
8+
import org.kohsuke.github.connector.GitHubConnectorResponse;
89

910
import java.io.IOException;
1011
import java.io.InputStream;
@@ -59,6 +60,13 @@ public void testHandler_Fail() throws Exception {
5960
@Override
6061
public void onError(IOException e, HttpURLConnection uc) throws IOException {
6162

63+
GitHubConnectorResponse connectorResponse = null;
64+
try {
65+
connectorResponse = GitHubConnectorResponse.fromHttpURLConnectionAdapter(uc);
66+
} catch (UnsupportedOperationException ex) {
67+
assertThat(ex.getMessage(), startsWith("Cannot unwrap GitHubConnectorResponse"));
68+
}
69+
6270
// Verify
6371
assertThat(uc.getDate(), Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
6472
assertThat(uc.getExpiration(), equalTo(0L));
@@ -87,6 +95,12 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
8795
String error = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
8896
assertThat(error, containsString("Must have push access to repository"));
8997

98+
if (connectorResponse != null) {
99+
String connectorBody = IOUtils.toString(connectorResponse.bodyStream(),
100+
StandardCharsets.UTF_8);
101+
assertThat(connectorBody, containsString("Must have push access to repository"));
102+
}
103+
90104
// calling again should still error
91105
ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());
92106

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

119-
boolean isAdapter = false;
120-
if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
121-
isAdapter = true;
122-
}
133+
if (connectorResponse != null) {
134+
assertThat(uc.toString(),
135+
containsString("GitHubConnectorResponseHttpUrlConnectionAdapter"));
123136

124-
if (isAdapter) {
125137
Assert.assertThrows(UnsupportedOperationException.class,
126138
() -> uc.getAllowUserInteraction());
127139
Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getConnectTimeout());

0 commit comments

Comments
 (0)