Skip to content

Commit c24df1d

Browse files
authored
Merge pull request #1164 from bitwiseman/revert/reflective
Revert #1083 reflective access for PATCH method
2 parents 8234655 + 2436ed0 commit c24df1d

106 files changed

Lines changed: 396 additions & 629 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/maven-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ jobs:
9090
if: matrix.os == 'windows' && matrix.java != '8'
9191
env:
9292
MAVEN_OPTS: ${{ env.JAVA_11_PLUS_MAVEN_OPTS }}
93-
run: mvn -B install --file pom.xml
93+
run: mvn -B install --file pom.xml "-Dsurefire.argLine=--add-opens java.base/java.net=ALL-UNNAMED"
9494
- name: Maven Install with Code Coverage
9595
if: matrix.os != 'windows' && matrix.java != '8'
9696
env:
9797
MAVEN_OPTS: ${{ env.JAVA_11_PLUS_MAVEN_OPTS }}
98-
run: mvn -B install -D enable-ci --file pom.xml
98+
run: mvn -B install -D enable-ci --file pom.xml "-Dsurefire.argLine=--add-opens java.base/java.net=ALL-UNNAMED"

src/main/java/org/kohsuke/github/GitHubHttpUrlConnectionClient.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.lang.reflect.Field;
89
import java.net.HttpURLConnection;
910
import java.net.ProtocolException;
1011
import java.nio.charset.StandardCharsets;
@@ -162,9 +163,30 @@ private static void setRequestMethod(String method, HttpURLConnection connection
162163
connection.setRequestMethod(method);
163164
} catch (ProtocolException e) {
164165
// JDK only allows one of the fixed set of verbs. Try to override that
165-
connection.setRequestProperty("X-HTTP-Method-Override", method);
166-
connection.setRequestMethod("POST");
166+
try {
167+
Field $method = HttpURLConnection.class.getDeclaredField("method");
168+
$method.setAccessible(true);
169+
$method.set(connection, method);
170+
} catch (Exception x) {
171+
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
172+
}
173+
// sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
174+
try {
175+
Field $delegate = connection.getClass().getDeclaredField("delegate");
176+
$delegate.setAccessible(true);
177+
Object delegate = $delegate.get(connection);
178+
if (delegate instanceof HttpURLConnection) {
179+
HttpURLConnection nested = (HttpURLConnection) delegate;
180+
setRequestMethod(method, nested);
181+
}
182+
} catch (NoSuchFieldException x) {
183+
// no problem
184+
} catch (IllegalAccessException x) {
185+
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
186+
}
167187
}
188+
if (!connection.getRequestMethod().equals(method))
189+
throw new IllegalStateException("Failed to set the request method to " + method);
168190
}
169191

170192
/**

src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ private void recordSnapshot(WireMockServer server, String target, boolean isRawS
159159
if (server != null) {
160160

161161
server.snapshotRecord(recordSpec().forTarget(target)
162-
// "X-HTTP-Method-Override" is used to send http methods not supported by
163-
// HttpURLConnection without using hacky Java reflection techniques
164-
.captureHeader("X-HTTP-Method-Override")
165162
// "If-None-Match" header used for ETag matching for caching connections
166163
.captureHeader("If-None-Match")
167164
// "If-Modified-Since" header used for ETag matching for caching connections

src/test/resources/org/kohsuke/github/AppTest/wiremock/notifications/mappings/notifications_threads_523050578-25.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
"name": "notifications_threads_523050578",
44
"request": {
55
"url": "/notifications/threads/523050578",
6+
"method": "PATCH",
67
"bodyPatterns": [
78
{
89
"equalToJson": "{}",
910
"ignoreArrayOrder": true,
1011
"ignoreExtraElements": true
1112
}
1213
],
13-
"method": "POST",
1414
"headers": {
15-
"X-HTTP-Method-Override": {
16-
"equalTo": "PATCH"
17-
},
1815
"Accept": {
1916
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
2017
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCreateCommitComment/mappings/repos_kohsuke_sandbox-ant_comments_46267761-7.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"name": "repos_kohsuke_sandbox-ant_comments_46267761",
44
"request": {
55
"url": "/repos/kohsuke/sandbox-ant/comments/46267761",
6-
"method": "POST",
6+
"method": "PATCH",
77
"headers": {
8-
"X-HTTP-Method-Override": {
9-
"equalTo": "PATCH"
10-
},
118
"Accept": {
129
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1310
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCreateIssue/mappings/repos_hub4j-test-org_github-api-test_issues_1-11.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
"name": "repos_hub4j-test-org_github-api-test_issues_1",
44
"request": {
55
"url": "/repos/hub4j-test-org/github-api-test/issues/1",
6+
"method": "PATCH",
67
"bodyPatterns": [
78
{
89
"equalToJson": "{\"state\":\"closed\"}",
910
"ignoreArrayOrder": true,
1011
"ignoreExtraElements": true
1112
}
1213
],
13-
"method": "POST",
1414
"headers": {
15-
"X-HTTP-Method-Override": {
16-
"equalTo": "PATCH"
17-
},
1815
"Accept": {
1916
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
2017
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testRepoCRUD/mappings/repos_bitwiseman_github-api-test-rename-3.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"name": "repos_bitwiseman_github-api-test-rename",
44
"request": {
55
"url": "/repos/bitwiseman/github-api-test-rename",
6-
"method": "POST",
6+
"method": "PATCH",
77
"headers": {
8-
"X-HTTP-Method-Override": {
9-
"equalTo": "PATCH"
10-
},
118
"Accept": {
129
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1310
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testRepoCRUD/mappings/repos_bitwiseman_github-api-test-rename-4.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"name": "repos_bitwiseman_github-api-test-rename",
44
"request": {
55
"url": "/repos/bitwiseman/github-api-test-rename",
6-
"method": "POST",
6+
"method": "PATCH",
77
"headers": {
8-
"X-HTTP-Method-Override": {
9-
"equalTo": "PATCH"
10-
},
118
"Accept": {
129
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1310
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testRepoCRUD/mappings/repos_bitwiseman_github-api-test-rename-5.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"name": "repos_bitwiseman_github-api-test-rename",
44
"request": {
55
"url": "/repos/bitwiseman/github-api-test-rename",
6-
"method": "POST",
6+
"method": "PATCH",
77
"headers": {
8-
"X-HTTP-Method-Override": {
9-
"equalTo": "PATCH"
10-
},
118
"Accept": {
129
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1310
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testRepoCRUD/mappings/repos_bitwiseman_github-api-test-rename-6.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"name": "repos_bitwiseman_github-api-test-rename",
44
"request": {
55
"url": "/repos/bitwiseman/github-api-test-rename",
6-
"method": "POST",
6+
"method": "PATCH",
77
"headers": {
8-
"X-HTTP-Method-Override": {
9-
"equalTo": "PATCH"
10-
},
118
"Accept": {
129
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1310
}

0 commit comments

Comments
 (0)