ClassCastException from InternalHttpClient to AbstractHttpClient

2,582 views
Skip to first unread message

pggi...@gmail.com

unread,
Nov 29, 2014, 2:28:07 PM11/29/14
to rest-a...@googlegroups.com
Hi,

  I'm configuring RestAssured (2.3.3) with a custom HttpClient (4.3.5) as shown here:

https://code.google.com/p/rest-assured/wiki/Usage#HTTP_Client_Config

  Even though I return an instance of the HttpClient interface (via HttpClients.custom().build() (see https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#build%28%29) I get an exception:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.apache.http.imple.client.InternalHttpClient@37c49e41' with class 'org.apache.http.impl.client.InternalHttpClient' to class 'org.apache.http.impl.client.AbstractHttpClient'
[removed Groovy internals]
at com
.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:925)

  Why does RequestSpecificationImpl require an AbstractHttpClient when HttpClientFactory.createHttpClient() returns an HttpClient?  See https://github.com/jayway/rest-assured/blob/master/rest-assured/src/main/groovy/com/jayway/restassured/internal/RequestSpecificationImpl.groovy#L109

  Examples of HttpClient creation return a CloseableHttpClient (https://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html) which is a superclass of AbstractHttpClient, which itself is deprecated: https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/deprecated-list.html

  Is there a workaround for this?  I need to configure the HttpClient myself which is why I'm using my own HttpClientFactory. The API needs to be internally consistent: if the factory method returns an HttpClient then other classes should not be casting the client to a different class.

Thanks,

Paul


Johan Haleby

unread,
Dec 1, 2014, 1:30:35 PM12/1/14
to rest-a...@googlegroups.com
Hi, 

Unfortunately it doesn't work with instances that doesn't extend from AbstractHttpClient. The reason why HttpClientFactory returns HttpClient was for "future compatibility" where I hoped to fix so that any HttpClient instance would work. In hindsight this was probably a mistake. RA depends heavily on the methods in AbstractHttpClient and I think that major refactoring is necessary to support the HttpClient interface. I'd love this to happen though and if you want to help out I'll be glad to help. But I'm on parental leave now I won't have time to work on such a big change myself right now.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maciej Gawinecki

unread,
Jan 29, 2016, 8:07:21 AM1/29/16
to REST assured
Hi Johan,

I can see a lot of improvements and bug fixes has been added since this this was reported. The most recent version is 2.8.0, while this limitation was reported in 2.3.3.

Do you plan to refactor that?

If so, when or, in other word, how high is that on your priority list?

Regards,
Maciej

Johan Haleby

unread,
Feb 2, 2016, 2:15:01 AM2/2/16
to rest-a...@googlegroups.com
Hi,

I plan to refactor it but I'm not sure when I'll find the time to do it unfortunately :(

/Johan 

Miguel García Puyol

unread,
Apr 15, 2016, 10:38:15 AM4/15/16
to REST assured
+1 to this feature.

I am trying to add a generic retry mechanism as you described here: https://groups.google.com/forum/#!topic/rest-assured/KwMYDKLEmRk

But I get the same cast exception.

Rohan Palkar

unread,
Mar 3, 2017, 5:00:49 AM3/3/17
to REST assured
Is there no workaround available for this issue so far ?

If yes, could anyone please share on how to go about it.

- Rohan

Rohan Palkar

unread,
Mar 3, 2017, 10:48:06 AM3/3/17
to REST assured
Hi Johan, 

I am trying to build a retry strategy for specific response codes and implemented the ServiceUnavailableRetryStrategy interface for the same.
When trying to plug it in into RestAssured using the HttpClientFactory, I am hitting the issue being discussed here in this thread.

I am currently blocked with this and although we are looking into an upgrade to the latest RestAssured version 2.9.0/3.0.0, it's not going to be inducted immediately.

Hence, it would be great if you could suggest any alternative implementation for the same.

Thanks in advance.

Regards,
Rohan

rejis...@gmail.com

unread,
Aug 22, 2023, 8:40:59 AM8/22/23
to REST assured
Hi, 
This seems to be a very old thread, but I still face the same issue. Any of you have any solution/fix for the above issue.

Thanks in advance.

Reply all
Reply to author
Forward
0 new messages