Skip to content

Commit

Permalink
Replace reflection with X-HTTP-Method-Override
Browse files Browse the repository at this point in the history
Fixes hub4j#754
  • Loading branch information
bitwiseman committed Apr 8, 2021
1 parent 7c8a7ff commit 8e20f4d
Show file tree
Hide file tree
Showing 104 changed files with 623 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -163,30 +162,9 @@ private static void setRequestMethod(String method, HttpURLConnection connection
connection.setRequestMethod(method);
} catch (ProtocolException e) {
// JDK only allows one of the fixed set of verbs. Try to override that
try {
Field $method = HttpURLConnection.class.getDeclaredField("method");
$method.setAccessible(true);
$method.set(connection, method);
} catch (Exception x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
// sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
try {
Field $delegate = connection.getClass().getDeclaredField("delegate");
$delegate.setAccessible(true);
Object delegate = $delegate.get(connection);
if (delegate instanceof HttpURLConnection) {
HttpURLConnection nested = (HttpURLConnection) delegate;
setRequestMethod(method, nested);
}
} catch (NoSuchFieldException x) {
// no problem
} catch (IllegalAccessException x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
connection.setRequestProperty("X-HTTP-Method-Override", method);
connection.setRequestMethod("POST");
}
if (!connection.getRequestMethod().equals(method))
throw new IllegalStateException("Failed to set the request method to " + method);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private void recordSnapshot(WireMockServer server, String target, boolean isRawS
if (server != null) {

server.snapshotRecord(recordSpec().forTarget(target)
// "X-HTTP-Method-Override" is used to send http methods not supported by
// HttpURLConnection without using hacky Java reflection techniques
.captureHeader("X-HTTP-Method-Override")
// "If-None-Match" header used for ETag matching for caching connections
.captureHeader("If-None-Match")
// "If-Modified-Since" header used for ETag matching for caching connections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"name": "notifications_threads_523050578",
"request": {
"url": "/notifications/threads/523050578",
"method": "PATCH",
"bodyPatterns": [
{
"equalToJson": "{}",
"ignoreArrayOrder": true,
"ignoreExtraElements": true
}
],
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_kohsuke_sandbox-ant_comments_46267761",
"request": {
"url": "/repos/kohsuke/sandbox-ant/comments/46267761",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"name": "repos_hub4j-test-org_github-api-test_issues_1",
"request": {
"url": "/repos/hub4j-test-org/github-api-test/issues/1",
"method": "PATCH",
"bodyPatterns": [
{
"equalToJson": "{\"state\":\"closed\"}",
"ignoreArrayOrder": true,
"ignoreExtraElements": true
}
],
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_bitwiseman_github-api-test-rename",
"request": {
"url": "/repos/bitwiseman/github-api-test-rename",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_bitwiseman_github-api-test-rename",
"request": {
"url": "/repos/bitwiseman/github-api-test-rename",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_bitwiseman_github-api-test-rename",
"request": {
"url": "/repos/bitwiseman/github-api-test-rename",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_bitwiseman_github-api-test-rename",
"request": {
"url": "/repos/bitwiseman/github-api-test-rename",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"name": "repos_bitwiseman_github-api-test-rename",
"request": {
"url": "/repos/bitwiseman/github-api-test-rename",
"method": "PATCH",
"method": "POST",
"headers": {
"X-HTTP-Method-Override": {
"equalTo": "PATCH"
},
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
Expand Down Expand Up @@ -105,7 +105,7 @@
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
Expand All @@ -23,23 +23,12 @@
"location": "Seattle, WA, USA",
"email": "[email protected]",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 181,
"public_gists": 7,
"followers": 149,
"following": 9,
"bio": null,
"twitter_username": "bitwiseman",
"public_repos": 206,
"public_gists": 8,
"followers": 181,
"following": 11,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2020-03-26T21:08:45Z",
"private_gists": 8,
"total_private_repos": 10,
"owned_private_repos": 0,
"disk_usage": 33697,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
"updated_at": "2021-04-02T21:55:29Z"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "e24be5d0-73bf-4748-8c79-a68eb5e2becf",
"id": "55f5d0e7-2f1f-4a8a-812c-204ea56540f7",
"name": "repos_hub4j-test-org_test-labels",
"request": {
"url": "/repos/hub4j-test-org/test-labels",
Expand All @@ -14,33 +14,33 @@
"status": 200,
"bodyFileName": "repos_hub4j-test-org_test-labels-2.json",
"headers": {
"Date": "Thu, 26 Mar 2020 21:52:19 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4993",
"X-RateLimit-Reset": "1585263138",
"Date": "Thu, 08 Apr 2021 20:38:59 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With, X-GitHub-OpenAPI"
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"6b3e989ed9af12ce6cd75038c1664ab5\"",
"ETag": "W/\"86fbc9276b6690bc16bd74f81c0ce45c1f5fda7db638aaa2c4bb0bcdd63da176\"",
"Last-Modified": "Sun, 15 Feb 2015 14:49:09 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-OAuth-Scopes": "repo, workflow",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4937",
"X-RateLimit-Reset": "1617917629",
"X-RateLimit-Used": "63",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "E69B:9495:1D1CC:2452B:5E7D2411"
"X-GitHub-Request-Id": "E09C:148F:17AC74:19F151:606F69E3"
}
},
"uuid": "e24be5d0-73bf-4748-8c79-a68eb5e2becf",
"uuid": "55f5d0e7-2f1f-4a8a-812c-204ea56540f7",
"persistent": true,
"insertionIndex": 2
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "2c942d88-1a5a-4fba-ae09-ada2a5dff83f",
"id": "7896038e-2291-4a94-8ce9-ea12923353b2",
"name": "repos_hub4j-test-org_test-labels_labels",
"request": {
"url": "/repos/hub4j-test-org/test-labels/labels",
Expand All @@ -13,41 +13,41 @@
{
"equalToJson": "{\"color\":\"123457\",\"name\":\"test2\",\"description\":\"this is a different test\"}",
"ignoreArrayOrder": true,
"ignoreExtraElements": true
"ignoreExtraElements": false
}
]
},
"response": {
"status": 201,
"body": "{\"id\":1937402965,\"node_id\":\"MDU6TGFiZWwxOTM3NDAyOTY1\",\"url\":\"https://api.github.com/repos/hub4j-test-org/test-labels/labels/test2\",\"name\":\"test2\",\"color\":\"123457\",\"default\":false,\"description\":\"this is a different test\"}",
"body": "{\"id\":2897001309,\"node_id\":\"MDU6TGFiZWwyODk3MDAxMzA5\",\"url\":\"https://api.github.com/repos/hub4j-test-org/test-labels/labels/test2\",\"name\":\"test2\",\"color\":\"123457\",\"default\":false,\"description\":\"this is a different test\"}",
"headers": {
"Date": "Thu, 26 Mar 2020 21:52:22 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "201 Created",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4980",
"X-RateLimit-Reset": "1585263137",
"Date": "Thu, 08 Apr 2021 20:39:02 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With, X-GitHub-OpenAPI"
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "\"648a902d66c2f46f74c7b836969fdfe9\"",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"ETag": "\"6bb2796b20a76d6ab8e7a147bf6b19cf6d0605587e59367c34afe06bb4786f60\"",
"X-OAuth-Scopes": "repo, workflow",
"X-Accepted-OAuth-Scopes": "",
"Location": "https://api.github.com/repos/hub4j-test-org/test-labels/labels/test2",
"X-GitHub-Media-Type": "unknown, github.v3",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4924",
"X-RateLimit-Reset": "1617917629",
"X-RateLimit-Used": "76",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "E69B:9495:1D214:245B7:5E7D2416"
"X-GitHub-Request-Id": "E09C:148F:17AD20:19F219:606F69E6",
"Location": "https://api.github.com/repos/hub4j-test-org/test-labels/labels/test2"
}
},
"uuid": "2c942d88-1a5a-4fba-ae09-ada2a5dff83f",
"uuid": "7896038e-2291-4a94-8ce9-ea12923353b2",
"persistent": true,
"insertionIndex": 15
}
Loading

0 comments on commit 8e20f4d

Please sign in to comment.