users@jersey.java.net

[Jersey] Re: Using ApacheHttpClient in a JerseyTest

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Mon, 07 Feb 2011 17:39:48 +0100

Hello,

excellent question, thanks for it.

Unfortunately, there is no simple way how to do it now. I tried it but I
ended with almost all overridden methods from JerseyTest, which is
certainly not good solution. Main problem here is that webresource()
method doesn't use client(), but internally saved instance in private
field, to which you don't have access.

There is definitely room (and need) for improvement, please file RFE
issue for this..

What I'd recommend to do now is just copy JerseyTest class source [1],
rename it to something like ApacheClientJerseyTest and modify getClient
to something like this:

     private static Client getClient(TestContainer tc, AppDescriptor ad) {
         Client c = ApacheHttpClient.create(ad.getClientConfig());

         //check if logging is required
         boolean enableLogging = (System.getProperty("enableLogging") !=
null)
                 ? true
                 : false;

         if (enableLogging) {
                 c.addFilter(new LoggingFilter());
         }

         return c;
     }


And your tests should extend this newly created class..

Pavel

[1]
http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-test-framework/jersey-test-framework-core/src/main/java/com/sun/jersey/test/framework/JerseyTest.java?rev=4574


On 02/07/2011 02:08 PM, Damiano ALBANI wrote:
> Hello,
>
> I'd like to use ApacheHttpClient in my JerseyTest-based test suite, to
> leverage its cookie management capabilities in particular.
> The Java code for my test class looks like this:
>
> protected AppDescriptor configure() {
> return
> new WebAppDescriptor.Builder("fr.univNantes.spin")
> .clientConfig(
> new DefaultApacheHttpClientConfig() {
> {
> this.getClasses().add(JAXBContextResolver.class);
>
> this.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES,
> true);
>
> this.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
> }
> }
> )
> .build();
> }
>
> protected TestContainerFactory getTestContainerFactory() {
> return new ExternalTestContainerFactory();
> }
>
> @Test
> public void test_login() {
> ClientResponse response =
> resource().path("login").get(ClientResponse.class);
> }
>
> But that doesn't make it use an ApacheHttpClient at all.
> It looks like the culprit is "JerseyTest::getClient(TestContainer,
> AppDescriptor)", which always returns a client based on
> URLConnectionClientHandler...
>
> So where/how can I plug in an Apache client in there?
>
> Thanks a lot.
>