Search code examples
javaweb-servicestestngbddqaf

How to manage cookies with multiple requests with qaf web services request call?


I am using bdd implementation provided by qaf for test automation and using qaf-support-ws for web services testing. I found it very easy to use with all kind of features required for web services test automation with power of TestNG. It helps in easy UI and API orchestration. Our beckend API requires authontication and uses cookie for subsequent API call. I am able to pass coockie by implementation of ClientFilter. I have created providing client by extending RestClientFactory. My client with added Coockie filter works fine with request calls for one domain and whenever I have request on different domain I need to reset client as below:

new RestTestBase().resetClient();

Because of that I have to call API to authenticate each time. If request are for same domain I don't need to reset client. Is there any other better way to manage cookies? So that I can work with multiple domain without resetting client.


Solution

  • Another simpler way is by using Apache HTTP client as below:

    protected Client createClient() {
            HttpClient httpClient = new HttpClient();
            ApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
            config.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
            // ApacheHttpClient httpClient = ApacheHttpClient.create(config);
            ApacheHttpClientHandler clientHandler = new ApacheHttpClientHandler(httpClient, config);
            ClientHandler root = new ApacheHttpClient(clientHandler);
    
            Client client = new Client(root, config);
            return client;
    }
    

    with this implementation you will not required to reset client when doing request for another domain and wise-versa. Complete example can be found here.