You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Chris Wolf <cw...@gmail.com> on 2015/07/15 02:20:19 UTC

How to inject fake HttpServletRequest/HttpServletResponse in local transport?

For legacy reasons, all of the resource methods in the project I'm
working on have a signature similar to:

    @GET
    @Path("/book")
    public Response getBook(@Context HttpServletRequest request,
            @Context HttpServletResponse response);


When I change to local transport (to save runtime setup of Jetty in
unit tests), these parameters are null, which makes sense since the
protocol is not HTTP.  I tried implementing a custom Interceptor to
replace Message content with a two element list containing mock
HttpServletRequest and HttpServletResponse , but I couldn't find the
correct phase.

(the mocks are from mockrunner-servlet)

Now I'm thinking it might be easier with a custom invoker, as
mentioned at the bottom of this page:

http://cxf.apache.org/docs/jax-rs-filters.html

I downloaded the source tarball and found the source at:

https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomJAXRSInvoker.java;h=cce0f048d1c9f1b12925725b9def7730a4aa01ff;hb=HEAD

...but now I can't find any other java module that would show how to
configure this custom invoker.  Apologies if I missed it online, but
could find nothing.

Thanks,

Chris

Re: How to inject fake HttpServletRequest/HttpServletResponse in local transport?

Posted by Chris Wolf <cw...@gmail.com>.
Ok, I finally was able to inject the mock HttpServletRequest/Response
via a custom invoker (on the server-side) - thanks for all the help!
Now looking at the Jackson issue...

On Wed, Jul 15, 2015 at 10:14 AM, Sergey Beryozkin <sb...@gmail.com> wrote:
> You do not need this interceptor on the client side, pass 'null' where
> context params are expected, and register it on the server side with the
> server factory bean if you need to test the server code interacting with the
> servlet request/response
>
> Sergey
> On 15/07/15 17:07, Chris Wolf wrote:
>>
>> Sergey,
>>
>> I changed the phase to UNMARSHAL.  But it seems to build and call the
>> chain *after* the resource method is invoked.  (I need to created the
>> mock req/res before the resource method is called).
>>
>> 10:01:29.301 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
>> org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424 to phase receive
>> 10:01:29.301 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
>>
>> org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
>> to phase unmarshal
>> 10:01:29.301 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
>> CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
>> to phase unmarshal
>> 10:01:29.303 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
>> org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
>> to phase pre-protocol-frontend
>> 10:01:29.303 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Chain
>> org.apache.cxf.phase.PhaseInterceptorChain@7329779f was created.
>> Current flow:
>>    receive [PolicyInInterceptor]
>>    pre-protocol-frontend [ClientResponseFilterInterceptor]
>>    unmarshal [ClientAsyncResponseInterceptor,
>> FakeHttpRequestResponseInjectingInterceptor]
>>
>> 10:01:29.303 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
>> interceptor org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424
>> 10:01:29.304 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
>> interceptor
>> org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
>> 10:01:29.304 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
>> interceptor
>> org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
>> 10:01:29.304 [default-workqueue-2] DEBUG
>> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
>> interceptor
>> CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
>> 10:01:29.304 [default-workqueue-2] DEBUG
>> C.c.FakeHttpRequestResponseInjectingInterceptor - ***************
>> Called handleMessage()...
>>
>>
>>
>> I assume this interceptor is on the client-side's InInterceptor chain,
>> maybe I'm wrong...
>>
>>
>> On Wed, Jul 15, 2015 at 3:35 AM, Sergey Beryozkin <sb...@gmail.com>
>> wrote:
>>>
>>>
>>> On 15/07/15 03:20, Chris Wolf wrote:
>>>>
>>>>
>>>> For legacy reasons, all of the resource methods in the project I'm
>>>> working on have a signature similar to:
>>>>
>>>>       @GET
>>>>       @Path("/book")
>>>>       public Response getBook(@Context HttpServletRequest request,
>>>>               @Context HttpServletResponse response);
>>>>
>>>>
>>>> When I change to local transport (to save runtime setup of Jetty in
>>>> unit tests), these parameters are null, which makes sense since the
>>>> protocol is not HTTP.  I tried implementing a custom Interceptor to
>>>> replace Message content with a two element list containing mock
>>>> HttpServletRequest and HttpServletResponse , but I couldn't find the
>>>> correct phase.
>>>
>>>
>>>
>>> JAXRSInInterceptor runs at the UNMARSHAL phase, so you can register an
>>> interceptor at UMMARSHAL or earlier, and set the mocks as message
>>> properties, example:
>>> message.put("HTTP.REQUEST", request);
>>> message.put("HTTP.RESPONSE", response);
>>>
>>>>
>>>> (the mocks are from mockrunner-servlet)
>>>>
>>>> Now I'm thinking it might be easier with a custom invoker, as
>>>> mentioned at the bottom of this page:
>>>>
>>>> http://cxf.apache.org/docs/jax-rs-filters.html
>>>>
>>>> I downloaded the source tarball and found the source at:
>>>>
>>>>
>>>>
>>>> https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomJAXRSInvoker.java;h=cce0f048d1c9f1b12925725b9def7730a4aa01ff;hb=HEAD
>>>>
>>>> ...but now I can't find any other java module that would show how to
>>>> configure this custom invoker.  Apologies if I missed it online, but
>>>> could find nothing.
>>>>
>>> This documentation shows how to set it up from Spring only:
>>>
>>> http://cxf.apache.org/docs/jax-rs-filters.html#JAX-RSFilters-Custominvokers
>>>
>>> or do from the code:
>>> myServerfactoryBean.setInvoker(myInvoker)
>>>
>>> Cheers, Sergey
>>>
>>>> Thanks,
>>>>
>>>> Chris
>>>>
>>>
>>>
>>> --
>>> Sergey Beryozkin
>>>
>>> Talend Community Coders
>>> http://coders.talend.com/
>>>
>>> Blog: http://sberyozkin.blogspot.com
>
>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com

Re: How to inject fake HttpServletRequest/HttpServletResponse in local transport?

Posted by Sergey Beryozkin <sb...@gmail.com>.
You do not need this interceptor on the client side, pass 'null' where 
context params are expected, and register it on the server side with the 
server factory bean if you need to test the server code interacting with 
the servlet request/response

Sergey
On 15/07/15 17:07, Chris Wolf wrote:
> Sergey,
>
> I changed the phase to UNMARSHAL.  But it seems to build and call the
> chain *after* the resource method is invoked.  (I need to created the
> mock req/res before the resource method is called).
>
> 10:01:29.301 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
> org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424 to phase receive
> 10:01:29.301 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
> org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
> to phase unmarshal
> 10:01:29.301 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
> CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
> to phase unmarshal
> 10:01:29.303 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
> org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
> to phase pre-protocol-frontend
> 10:01:29.303 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Chain
> org.apache.cxf.phase.PhaseInterceptorChain@7329779f was created.
> Current flow:
>    receive [PolicyInInterceptor]
>    pre-protocol-frontend [ClientResponseFilterInterceptor]
>    unmarshal [ClientAsyncResponseInterceptor,
> FakeHttpRequestResponseInjectingInterceptor]
>
> 10:01:29.303 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
> interceptor org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424
> 10:01:29.304 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
> interceptor org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
> 10:01:29.304 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
> interceptor org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
> 10:01:29.304 [default-workqueue-2] DEBUG
> o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
> interceptor CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
> 10:01:29.304 [default-workqueue-2] DEBUG
> C.c.FakeHttpRequestResponseInjectingInterceptor - ***************
> Called handleMessage()...
>
>
>
> I assume this interceptor is on the client-side's InInterceptor chain,
> maybe I'm wrong...
>
>
> On Wed, Jul 15, 2015 at 3:35 AM, Sergey Beryozkin <sb...@gmail.com> wrote:
>>
>> On 15/07/15 03:20, Chris Wolf wrote:
>>>
>>> For legacy reasons, all of the resource methods in the project I'm
>>> working on have a signature similar to:
>>>
>>>       @GET
>>>       @Path("/book")
>>>       public Response getBook(@Context HttpServletRequest request,
>>>               @Context HttpServletResponse response);
>>>
>>>
>>> When I change to local transport (to save runtime setup of Jetty in
>>> unit tests), these parameters are null, which makes sense since the
>>> protocol is not HTTP.  I tried implementing a custom Interceptor to
>>> replace Message content with a two element list containing mock
>>> HttpServletRequest and HttpServletResponse , but I couldn't find the
>>> correct phase.
>>
>>
>> JAXRSInInterceptor runs at the UNMARSHAL phase, so you can register an
>> interceptor at UMMARSHAL or earlier, and set the mocks as message
>> properties, example:
>> message.put("HTTP.REQUEST", request);
>> message.put("HTTP.RESPONSE", response);
>>
>>>
>>> (the mocks are from mockrunner-servlet)
>>>
>>> Now I'm thinking it might be easier with a custom invoker, as
>>> mentioned at the bottom of this page:
>>>
>>> http://cxf.apache.org/docs/jax-rs-filters.html
>>>
>>> I downloaded the source tarball and found the source at:
>>>
>>>
>>> https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomJAXRSInvoker.java;h=cce0f048d1c9f1b12925725b9def7730a4aa01ff;hb=HEAD
>>>
>>> ...but now I can't find any other java module that would show how to
>>> configure this custom invoker.  Apologies if I missed it online, but
>>> could find nothing.
>>>
>> This documentation shows how to set it up from Spring only:
>> http://cxf.apache.org/docs/jax-rs-filters.html#JAX-RSFilters-Custominvokers
>>
>> or do from the code:
>> myServerfactoryBean.setInvoker(myInvoker)
>>
>> Cheers, Sergey
>>
>>> Thanks,
>>>
>>> Chris
>>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>> Blog: http://sberyozkin.blogspot.com


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: How to inject fake HttpServletRequest/HttpServletResponse in local transport?

Posted by Chris Wolf <cw...@gmail.com>.
Sergey,

I changed the phase to UNMARSHAL.  But it seems to build and call the
chain *after* the resource method is invoked.  (I need to created the
mock req/res before the resource method is called).

10:01:29.301 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424 to phase receive
10:01:29.301 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
to phase unmarshal
10:01:29.301 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
to phase unmarshal
10:01:29.303 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor
org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
to phase pre-protocol-frontend
10:01:29.303 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Chain
org.apache.cxf.phase.PhaseInterceptorChain@7329779f was created.
Current flow:
  receive [PolicyInInterceptor]
  pre-protocol-frontend [ClientResponseFilterInterceptor]
  unmarshal [ClientAsyncResponseInterceptor,
FakeHttpRequestResponseInjectingInterceptor]

10:01:29.303 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
interceptor org.apache.cxf.ws.policy.PolicyInInterceptor@87a40424
10:01:29.304 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
interceptor org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor@6eec47c4
10:01:29.304 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
interceptor org.apache.cxf.jaxrs.client.WebClient$ClientAsyncResponseInterceptor@bb171679
10:01:29.304 [default-workqueue-2] DEBUG
o.a.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on
interceptor CXF_Test.cxf_test.FakeHttpRequestResponseInjectingInterceptor@64e43406
10:01:29.304 [default-workqueue-2] DEBUG
C.c.FakeHttpRequestResponseInjectingInterceptor - ***************
Called handleMessage()...



I assume this interceptor is on the client-side's InInterceptor chain,
maybe I'm wrong...


On Wed, Jul 15, 2015 at 3:35 AM, Sergey Beryozkin <sb...@gmail.com> wrote:
>
> On 15/07/15 03:20, Chris Wolf wrote:
>>
>> For legacy reasons, all of the resource methods in the project I'm
>> working on have a signature similar to:
>>
>>      @GET
>>      @Path("/book")
>>      public Response getBook(@Context HttpServletRequest request,
>>              @Context HttpServletResponse response);
>>
>>
>> When I change to local transport (to save runtime setup of Jetty in
>> unit tests), these parameters are null, which makes sense since the
>> protocol is not HTTP.  I tried implementing a custom Interceptor to
>> replace Message content with a two element list containing mock
>> HttpServletRequest and HttpServletResponse , but I couldn't find the
>> correct phase.
>
>
> JAXRSInInterceptor runs at the UNMARSHAL phase, so you can register an
> interceptor at UMMARSHAL or earlier, and set the mocks as message
> properties, example:
> message.put("HTTP.REQUEST", request);
> message.put("HTTP.RESPONSE", response);
>
>>
>> (the mocks are from mockrunner-servlet)
>>
>> Now I'm thinking it might be easier with a custom invoker, as
>> mentioned at the bottom of this page:
>>
>> http://cxf.apache.org/docs/jax-rs-filters.html
>>
>> I downloaded the source tarball and found the source at:
>>
>>
>> https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomJAXRSInvoker.java;h=cce0f048d1c9f1b12925725b9def7730a4aa01ff;hb=HEAD
>>
>> ...but now I can't find any other java module that would show how to
>> configure this custom invoker.  Apologies if I missed it online, but
>> could find nothing.
>>
> This documentation shows how to set it up from Spring only:
> http://cxf.apache.org/docs/jax-rs-filters.html#JAX-RSFilters-Custominvokers
>
> or do from the code:
> myServerfactoryBean.setInvoker(myInvoker)
>
> Cheers, Sergey
>
>> Thanks,
>>
>> Chris
>>
>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com

Re: How to inject fake HttpServletRequest/HttpServletResponse in local transport?

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 15/07/15 03:20, Chris Wolf wrote:
> For legacy reasons, all of the resource methods in the project I'm
> working on have a signature similar to:
>
>      @GET
>      @Path("/book")
>      public Response getBook(@Context HttpServletRequest request,
>              @Context HttpServletResponse response);
>
>
> When I change to local transport (to save runtime setup of Jetty in
> unit tests), these parameters are null, which makes sense since the
> protocol is not HTTP.  I tried implementing a custom Interceptor to
> replace Message content with a two element list containing mock
> HttpServletRequest and HttpServletResponse , but I couldn't find the
> correct phase.

JAXRSInInterceptor runs at the UNMARSHAL phase, so you can register an 
interceptor at UMMARSHAL or earlier, and set the mocks as message 
properties, example:
message.put("HTTP.REQUEST", request);
message.put("HTTP.RESPONSE", response);

>
> (the mocks are from mockrunner-servlet)
>
> Now I'm thinking it might be easier with a custom invoker, as
> mentioned at the bottom of this page:
>
> http://cxf.apache.org/docs/jax-rs-filters.html
>
> I downloaded the source tarball and found the source at:
>
> https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomJAXRSInvoker.java;h=cce0f048d1c9f1b12925725b9def7730a4aa01ff;hb=HEAD
>
> ...but now I can't find any other java module that would show how to
> configure this custom invoker.  Apologies if I missed it online, but
> could find nothing.
>
This documentation shows how to set it up from Spring only:
http://cxf.apache.org/docs/jax-rs-filters.html#JAX-RSFilters-Custominvokers

or do from the code:
myServerfactoryBean.setInvoker(myInvoker)

Cheers, Sergey

> Thanks,
>
> Chris
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com