2121import org .junit .Test ;
2222import org .junit .rules .Timeout ;
2323
24+ import java .lang .invoke .MethodHandles ;
25+ import java .lang .invoke .VarHandle ;
26+ import java .lang .reflect .Field ;
27+ import java .lang .reflect .Modifier ;
28+ import java .net .HttpURLConnection ;
29+ import java .util .Arrays ;
2430import java .util .HashMap ;
2531import java .util .UUID ;
2632import java .util .function .Predicate ;
@@ -38,22 +44,21 @@ public class RestChannelMessageEditTest extends ParameterizedTest {
3844 @ Rule
3945 public Timeout testTimeout = Timeout .seconds (300 );
4046 private AblyRest ably ;
41- private EngineType engineType ;
4247
4348 @ Before
4449 public void setUpBefore () throws Exception {
4550 ClientOptions opts = createOptions (testVars .keys [0 ].keyStr );
4651 ably = new AblyRest (opts );
47- engineType = HttpEngineFactory .getFirstAvailable ().getEngineType ();
52+ if (HttpEngineFactory .getFirstAvailable ().getEngineType () == EngineType .DEFAULT ) {
53+ addPatchSupport ();
54+ }
4855 }
4956
5057 /**
5158 * Test getMessage: Publish a message and retrieve it by serial
5259 */
5360 @ Test
5461 public void getMessage_retrieveBySerial () throws Exception {
55- if (engineType == EngineType .DEFAULT ) return ;
56-
5762 String channelName = "mutable:get_message_" + UUID .randomUUID () + "_" + testParams .name ;
5863 Channel channel = ably .channels .get (channelName );
5964
@@ -83,8 +88,6 @@ public void getMessage_retrieveBySerial() throws Exception {
8388 */
8489 @ Test
8590 public void updateMessage_updateData () throws Exception {
86- if (engineType == EngineType .DEFAULT ) return ;
87-
8891 String channelName = "mutable:update_message_" + UUID .randomUUID () + "_" + testParams .name ;
8992 Channel channel = ably .channels .get (channelName );
9093
@@ -123,8 +126,6 @@ public void updateMessage_updateData() throws Exception {
123126 */
124127 @ Test
125128 public void updateMessage_updateEncodedData () throws Exception {
126- if (engineType == EngineType .DEFAULT ) return ;
127-
128129 String channelName = "mutable:update_encodedmessage_" + UUID .randomUUID () + "_" + testParams .name ;
129130 ChannelOptions channelOptions = ChannelOptions .withCipherKey (Crypto .generateRandomKey ());
130131 Channel channel = ably .channels .get (channelName , channelOptions );
@@ -164,8 +165,6 @@ public void updateMessage_updateEncodedData() throws Exception {
164165 */
165166 @ Test
166167 public void updateMessage_async () throws Exception {
167- if (engineType == EngineType .DEFAULT ) return ;
168-
169168 String channelName = "mutable:update_message_async_" + UUID .randomUUID () + "_" + testParams .name ;
170169 Channel channel = ably .channels .get (channelName );
171170
@@ -202,8 +201,6 @@ public void updateMessage_async() throws Exception {
202201 */
203202 @ Test
204203 public void deleteMessage_softDelete () throws Exception {
205- if (engineType == EngineType .DEFAULT ) return ;
206-
207204 String channelName = "mutable:delete_message_" + UUID .randomUUID () + "_" + testParams .name ;
208205 Channel channel = ably .channels .get (channelName );
209206
@@ -239,8 +236,6 @@ public void deleteMessage_softDelete() throws Exception {
239236 */
240237 @ Test
241238 public void appendMessage_checkUpdatedData () throws Exception {
242- if (engineType == EngineType .DEFAULT ) return ;
243-
244239 String channelName = "mutable:append_message_" + UUID .randomUUID () + "_" + testParams .name ;
245240 Channel channel = ably .channels .get (channelName );
246241
@@ -277,8 +272,6 @@ public void appendMessage_checkUpdatedData() throws Exception {
277272 */
278273 @ Test
279274 public void deleteMessage_async () throws Exception {
280- if (engineType == EngineType .DEFAULT ) return ;
281-
282275 String channelName = "mutable:delete_message_async_" + UUID .randomUUID () + "_" + testParams .name ;
283276 Channel channel = ably .channels .get (channelName );
284277
@@ -315,8 +308,6 @@ public void deleteMessage_async() throws Exception {
315308 */
316309 @ Test
317310 public void getMessageVersions_retrieveHistory () throws Exception {
318- if (engineType == EngineType .DEFAULT ) return ;
319-
320311 String channelName = "mutable:message_versions_" + UUID .randomUUID () + "_" + testParams .name ;
321312 Channel channel = ably .channels .get (channelName );
322313
@@ -366,8 +357,6 @@ public void getMessageVersions_retrieveHistory() throws Exception {
366357 */
367358 @ Test
368359 public void getMessageVersions_async () throws Exception {
369- if (engineType == EngineType .DEFAULT ) return ;
370-
371360 String channelName = "mutable:message_versions_async_" + UUID .randomUUID () + "_" + testParams .name ;
372361 Channel channel = ably .channels .get (channelName );
373362
@@ -445,8 +434,6 @@ public void deleteMessage_emptySerial() {
445434 */
446435 @ Test
447436 public void completeWorkflow_publishUpdateVersionsDelete () throws Exception {
448- if (engineType == EngineType .DEFAULT ) return ;
449-
450437 String channelName = "mutable:complete_workflow_" + UUID .randomUUID () + "_" + testParams .name ;
451438 Channel channel = ably .channels .get (channelName );
452439
@@ -525,4 +512,22 @@ private Message waitForDeletedMessageAppear(Channel channel, String serial) thro
525512 Thread .sleep (200 );
526513 }
527514 }
515+
516+ /**
517+ * We need to add patch support for HttpUrlConnect
518+ */
519+ private static void addPatchSupport () throws Exception {
520+ Field methodsField = HttpURLConnection .class .getDeclaredField ("methods" );
521+
522+ MethodHandles .Lookup lookup = MethodHandles .privateLookupIn (Field .class , MethodHandles .lookup ());
523+ VarHandle modifiers = lookup .findVarHandle (Field .class , "modifiers" , int .class );
524+ modifiers .set (methodsField , methodsField .getModifiers () & ~Modifier .FINAL );
525+ methodsField .setAccessible (true );
526+
527+ String [] methods = (String []) methodsField .get (null );
528+ String [] newMethods = Arrays .copyOf (methods , methods .length + 1 );
529+ newMethods [methods .length ] = "PATCH" ;
530+
531+ methodsField .set (null , newMethods );
532+ }
528533}
0 commit comments