Citrus http server

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Citrus http server

gituser25
Hi,
I am using citrus as server for my use case, and trying to hit the server with HTTP post with some request. I am using dispatching-endpoint-adapter/static-response-adapter with mappingStrategy/mappingKeyExtractor. I have couple of questions. Any inputs on this would be helpful.

1.How do i validate my request payload?  
2.How do i read the response from server. I am trying to implement as below but i dont get any response back
HttpPost request = new HttpPost("http://localhost:8080/search/customer");

                        request.addHeader(CustomerConstants.HEADER_TRANSACTION_ID, UUID.randomUUID().toString());
                        StringEntity input = new StringEntity("{\"id\":\"test\"}");
                        input.setContentType("application/json);
                        request.setEntity(input);
                        HttpResponse response = httpClient.execute(request);
                       
                       
                        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
                        if(response.getStatusLine().getStatusCode()==200){
                                String output;
                                while ((output = br.readLine()) != null) {
                                        String response=mapper.readValue(output, String.class);
                                }
                        }
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

Christoph Deppisch
Administrator
I do not see Citrus related code here. Also you did not mention the error that you get while executing the Http communication.

In general I would suggest to use Spring's RestTemplate for creating the Http request as your application is a Spring Boot application as I know from other questions. Handling the response is very easy then, too. Please refer to the Spring Boot samples and official documentation how to produce Http request/response communication.

Also I do not really understand your use case of your code setup. What exactly do you want to do? Do you need an automated integration test that is constantly running with your code base or do you just want to explore the Http communication in a proof of concept test.
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

gituser25
Hi Christoph,
I got the response using static response adapter, some issues w.r.t response format due to which i didnt get the response earlier.

Sorry if i was not clear on my use case with my previous post. I am trying to use citrus server (to replace the 3rd party call) to automate the integration test to run against my code base. I need to validate the request header, method and request payload in citrus server before sending the response back to the client (which is my spring boot application). Used the below for header validation but dont see this working.
Am i doing it right?

<citrus:header>
                <citrus:element name="svcRequestor" value="test"/>
</citrus:header>

Any option to validate the http method and request payload? Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

Christoph Deppisch
Administrator
Please also share your error message. Without that I can not really help because I do not see what is going wrong.

Also please have a look at this chapter in reference documentation. You will get a lot of answers here.

http://www.citrusframework.org/reference/html/index.html#http-rest
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

Mounika
Hi Christoph,

I have a doubt like can we expose citrus test case as a rest service and will we be able to run that from an url like remotely.
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

Christoph Deppisch
Administrator
Please have a look at the citrus-simulator project which provides Citrus based rest-server simulation as standalone Spring Boot service. https://github.com/christophd/citrus-simulator
Reply | Threaded
Open this post in threaded view
|

Re: Citrus http server

gituser25
In reply to this post by Christoph Deppisch
I didnt get any error, it always sends the response back irrespective of header  

I read the documentation and I was able to validate the request header/uri in server side when I run citrus test using Java DSL by passing the request and header through citrus client. But my use case is to pass the request payload/header/uri from my spring boot application to citrus server and to get the response back only if the request payload/header/uri matches.

Below is the code that i have in citrus-context.xml. Please let me know if you need any other details to help me on this issue.

<citrus-http:server id="dpHttpServer"
             port="8090"
             auto-start="true"
             resource-base="src/test/resources"
             endpoint-adapter="dispatchingEndpointAdapter"
             interceptors="serverInterceptors"/> 
     
     <util:list id="serverInterceptors">
  <bean class="com.consol.citrus.http.interceptor.LoggingHandlerInterceptor"/>
        </util:list>
                 
     <citrus:dispatching-endpoint-adapter id="dispatchingEndpointAdapter"
             mapping-key-extractor="mappingKeyExtractor"
             mapping-strategy="mappingStrategy"/>
 
  <bean id="mappingKeyExtractor" class="com.consol.citrus.endpoint.adapter.mapping.HeaderMappingKeyExtractor">
        <property name="headerName" value="#{T(com.consol.citrus.http.message.HttpMessageHeaders).HTTP_REQUEST_URI}"/>
    </bean>

    <bean id="mappingStrategy" class="com.consol.citrus.endpoint.adapter.mapping.SimpleMappingStrategy">
        <property name="adapterMappings">
            <map>
                <entry key="/search/customer" value-ref="customerEndpointAdapter"/>
            </map>
        </property>
    </bean>
 
   <citrus:static-response-adapter id="customerEndpointAdapter">
    <citrus:payload>
                {"customer":{"firstName":"test","lastName":"user","phone":"675-890-9008"}}    
        </citrus:payload>
    <citrus:header>
                <citrus:element name="svcRequestor" value="test"/>
         </citrus:header>
  </citrus:static-response-adapter>