Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e20f4d

Browse files
committed
Replace reflection with X-HTTP-Method-Override
Fixes hub4j#754
1 parent 7c8a7ff commit 8e20f4d

File tree

104 files changed

+623
-393
lines changed

Some content is hidden

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

104 files changed

+623
-393
lines changed

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

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

66
import java.io.IOException;
77
import java.io.InputStream;
8-
import java.lang.reflect.Field;
98
import java.net.HttpURLConnection;
109
import java.net.ProtocolException;
1110
import java.nio.charset.StandardCharsets;
@@ -163,30 +162,9 @@ private static void setRequestMethod(String method, HttpURLConnection connection
163162
connection.setRequestMethod(method);
164163
} catch (ProtocolException e) {
165164
// JDK only allows one of the fixed set of verbs. Try to override that
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-
}
165+
connection.setRequestProperty("X-HTTP-Method-Override", method);
166+
connection.setRequestMethod("POST");
187167
}
188-
if (!connection.getRequestMethod().equals(method))
189-
throw new IllegalStateException("Failed to set the request method to " + method);
190168
}
191169

192170
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ 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")
162165
// "If-None-Match" header used for ETag matching for caching connections
163166
.captureHeader("If-None-Match")
164167
// "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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
"name": "notifications_threads_523050578",
44
"request": {
55
"url": "/notifications/threads/523050578",
6-
"method": "PATCH",
76
"bodyPatterns": [
87
{
98
"equalToJson": "{}",
109
"ignoreArrayOrder": true,
1110
"ignoreExtraElements": true
1211
}
1312
],
13+
"method": "POST",
1414
"headers": {
15+
"X-HTTP-Method-Override": {
16+
"equalTo": "PATCH"
17+
},
1518
"Accept": {
1619
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1720
}

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

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
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",
76
"bodyPatterns": [
87
{
98
"equalToJson": "{\"state\":\"closed\"}",
109
"ignoreArrayOrder": true,
1110
"ignoreExtraElements": true
1211
}
1312
],
13+
"method": "POST",
1414
"headers": {
15+
"X-HTTP-Method-Override": {
16+
"equalTo": "PATCH"
17+
},
1518
"Accept": {
1619
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
1720
}

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)