qaf-tm/qaf-support-ws

Any plans to support PATCH request?

Closed this issue · 5 comments

We tried making the PATCH type of request in the version 2.1.13 and it did not work. I believe the jersey client that is used in this project does not support PATCH requests and that is the root cause of it.

Try adding property com.sun.jersey.client.property.httpUrlConnectionSetMethodWorkaround=true
Alternately create implementation that set this property and use it. I tried below and it worked fine for me:

  1. Create client implemetation
package qaf.example.steps;

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
import com.sun.jersey.multipart.impl.MultiPartWriter;

/**
 * @author chirag
 *
 */
public class MyClient extends RestClientFactory {

	@Override
	protected Client createClient() {
		ClientConfig config = new DefaultClientConfig();
		config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);
		config.getClasses().add(MultiPartWriter.class);
		return Client.create(config);
	}
}
  1. Register using Property:
rest.client.impl=qaf.example.steps.MyClient
com.sun.jersey.client.property.httpUrlConnectionSetMethodWorkaround=true

Try to add below code and it worked for me

// PATCH Method support for using reflection

	```
		Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
		methodsField.setAccessible(true);
		// get the methods field modifiers
		Field modifiersField = Field.class.getDeclaredField("modifiers");
		// bypass the "private" modifier
		modifiersField.setAccessible(true);
		System.out.println("methods Fields::" + methodsField);
		// remove the "final" modifier
		modifiersField.setInt(methodsField,
				methodsField.getModifiers() & ~Modifier.FINAL);
String[] methods =
				{"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"};
		// set the new methods - including patch
		methodsField.set(null, methods);
                     return client;

Thanks for your support!
I will close this issue now.

Just updating the final MyClient that worked for me as expected:

package xyz;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.net.HttpURLConnection;

 

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.config.ClientConfig;

import com.sun.jersey.api.client.config.DefaultClientConfig;

import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;

import com.sun.jersey.multipart.impl.MultiPartWriter;

public class MyClient extends RestClientFactory {

 

        @Override

        protected Client createClient() {

                ClientConfig config = new DefaultClientConfig();

                config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);

                config.getClasses().add(MultiPartWriter.class);

                try {

                        Field methodsField = HttpURLConnection.class.getDeclaredField("methods");

                        methodsField.setAccessible(true);

                        // get the methods field modifiers

                        Field modifiersField = Field.class.getDeclaredField("modifiers");

                        // bypass the "private" modifier

                        modifiersField.setAccessible(true);

                        System.out.println("methods Fields::" + methodsField);

                        // remove the "final" modifier

                        modifiersField.setInt(methodsField,

                                        methodsField.getModifiers() & ~Modifier.FINAL);

        String[] methods =

                                        {"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"};

                        // set the new methods - including patch

                        methodsField.set(null, methods);

                        System.out.println("Methods set");

                } catch(Exception e) {

                        System.out.println("Exceptipn in setting the methods");

                }

                return Client.create(config);

        }

}

With qaf-tm/qaf@246f2da PATCH should work without custom implementation.