Http with Java DSL

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

Http with Java DSL

hlopez
Hello,

How do I test Http with Java DSL?
There seem to be different ways:

One way could be like this:
        http(builder -> builder
                .client(httpClient)
                .send()
                .get(path)
                .build()
        );

        http(builder -> builder
                .client(httpClient)
                .receive()
                .response(HttpStatus.OK)
                .messageType(MessageType.JSON)
                .payload(list, objectMapper)
                .build()
        );

Other way could be like this:
        send(builder -> builder.endpoint(httpClient)
                .message(new HttpMessage()
                        .method(HttpMethod.GET)
                        .path(path)
                )
        );

        receive(builder -> builder.endpoint(httpClient)
                .payload(list, objectMapper)
                .messageType(MessageType.JSON)
        );

The second one looks more like the XML based one and I would like to use that.
But how do I check the status code without using ".message(new HttpMessage())"? I was expecting a response method like in the first one.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

hlopez
I think I did find it when running Citrus in DEBUG mode and checking the logging:
I should use citrus special headers, would be nice to have a page with an overview of the headers in the documentation page.

headers: {
    Transfer-Encoding=chunked, 
    citrus_message_timestamp=1515495049629, 
    citrus_http_reason_phrase=OK, 
    citrus_http_version=HTTP/1.1, 
    citrus_message_id=12d0076e-9dc0-42bd-9b7f-c53516770469, 
    contentType=application/json;charset=UTF-8, 
    Date=1515495048000, 
    Content-Type=application/json;charset=UTF-8, 
    citrus_http_status_code=200
}
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

hlopez
It's working nice like this:
        receive(builder -> builder.endpoint(httpClient)
                .payload(list, objectMapper)
                .header("citrus_http_reason_phrase", HttpStatus.OK.getReasonPhrase())
                .header("citrus_http_status_code", HttpStatus.OK)
                .header("contentType", MediaType.APPLICATION_JSON_UTF8_VALUE)
                .header("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE)
                .messageType(MessageType.JSON)
        );
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

hlopez
I've found the MessageHeaders:
        receive(builder -> builder.endpoint(httpClient)
                .header(HttpMessageHeaders.HTTP_REASON_PHRASE, HttpStatus.OK.getReasonPhrase())
                .header(HttpMessageHeaders.HTTP_STATUS_CODE, HttpStatus.OK)
                .header(HttpMessageHeaders.HTTP_CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
                .messageType(MessageType.JSON)
                .payload(list, objectMapper)
        );

Can now use it also for the sending part.
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

hlopez
I think I found a bug:

In my AppConfig file I'm creating the httpClient with default requestUrl:
    @Bean
    public HttpClient httpClient() {
        return CitrusEndpoints.http().client().requestUrl("http://localhost:8080").defaultAcceptHeader(false).build();
    }

Then later in my test class I have that been injected.
When I use the code below it is still going to default and not applying (adding) my path to the requestUri cause if I don't supply the HTTP_REQUEST_URI it gives the same result.

        send(builder -> builder.endpoint(httpClient)
                .header(HTTP_REQUEST_METHOD, HttpMethod.GET)
                .header(HTTP_REQUEST_URI, requestUri)
                .header(HTTP_CONTEXT_PATH, path)
                .header(HTTP_CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
        );

I'm using latest version 2.7.2 of citrus.
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

hlopez
Here my code that generates an error:

        HttpClient httpClient = CitrusEndpoints.http().client().defaultAcceptHeader(false).build();
        send(builder -> builder.endpoint(httpClient)
                .header(HTTP_REQUEST_METHOD, HttpMethod.GET)
                .header(HTTP_REQUEST_URI, requestUri)
                .header(HTTP_CONTEXT_PATH, path)
                .header(HTTP_CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
        );

        List<Person> list = new ArrayList<>();
        list.add(person1);
        list.add(person2);

        receive(builder -> builder.endpoint(httpClient)
                .header(HTTP_REASON_PHRASE, HttpStatus.OK.getReasonPhrase())
                .header(HTTP_STATUS_CODE, HttpStatus.OK)
                .header(HTTP_CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
                .messageType(MessageType.JSON)
                .payload(list, objectMapper)
        );

FAILURE: Caused by: CitrusRuntimeException: Unable to resolve dynamic endpoint uri! Neither header entry 'citrus_endpoint_uri' nor default endpoint uri is set

Seems like something is not going well.
Reply | Threaded
Open this post in threaded view
|

Re: Http with Java DSL

Christoph Deppisch
Administrator
This is because HTTP_REQUEST_URI header is not there to define a dynamic endpoint URI. Instead this header is used during validation to check that a request was using that endpoint URI.

You must use citrus_endpoint_uri (DynamicEndpointUriResolver.ENDPOINT_URI_HEADER_NAME) message header. Or you use the http specific Java DSL methods that automatically handle dynamic endpoint URI settings on the request message.

http().client(httpClient)
                .send()
                .post()
                .payload("Hello Citrus")
                .contentType("text/plain")
                .uri("http://somehost:8080");