Search code examples
javahttp-getcitrus-framework

Validate return value of http get


Assume you have some test-case like this:

@Test
public class MsgFlowIT extends TestNGCitrusTestDesigner {

    @Autowired
    private HttpClient todoClient;

    @CitrusTest(name = "SampleJavaTest.Send")
    public void Send() {

        http()
        .client(todoClient)
        .send()
        .post("/api/values/Method1")
        .contentType("application/xml")
        .name("Method1")
        .messageType(MessageType.XML)
        .payload(<XmlMessage>msg1</XmlMessage>);

        http()
        .client(todoClient)
        .send()
        .get("/api/values/Method2")
        //.response(HttpStatus.OK)
        .contentType("application/xml")
        .messageType(MessageType.XML)
        .name("Method2")
        //.accept("text/html") 
        .expect(<XmlMessage>msg2</XmlMessage>); // check if the return value of "get" matches the ".expect"
}

Is it possible to use the return value of the http-get and to check if it matches a predefined xml-structure or satisfies an xpath-expression ? In my case i definitely have to know what the http-get returns and compare it to some expected xml. I hope someone can help here, I have already invested much time :/


Solution

  • The Citrus client sends a GET request and gets a response from the server. You are completely missing the receive part in your test code sample. In your sample you just send two requests without receiving the response in your test for validation purpose.

    You can validate the response messages with expected header and payload information. I suggest to use the following send-receive operations in your code sample:

    @Test
    public class MsgFlowIT extends TestNGCitrusTestDesigner {
    
        @Autowired
        private HttpClient todoClient;
    
        @CitrusTest(name = "SampleJavaTest.Send")
        public void sendAndReceive() {
            http()
                .client(todoClient)
                .send()
                .post("/api/values/Method1")
                .contentType("application/xml")
                .name("Method1")
                .messageType(MessageType.XML)
                .payload("<XmlMessage>msg1</XmlMessage>");
    
            http()
                .client(todoClient)
                .receive()
                .response(HttpStatus.OK)
                .header("X-SomeHeader", "expectedValue")
                .payload("<SomeExpectedXml>foo</SomeExpectedXml>");
    
            http()
                .client(todoClient)
                .send()
                .get("/api/values/Method2")
                .contentType("application/xml")
                .messageType(MessageType.XML)
                .name("Method2");
    
            http()
                .client(todoClient)
                .receive()
                .response(HttpStatus.FOUND)
                .header("X-SomeHeader", "expectedValue")
                .xpath("/some/expression", "expectedValue")
                .payload("<SomeExpectedXml>foo</SomeExpectedXml>");
        }
    }
    

    Each send is followed by a receive on the same client endpoint. Please also notice that Http is a synchronous protocol by nature so the send operation blocks the test until a response from the server has been received.

    Besides validating the received response message with expected header and payload you can also define XPath expressions on that receive operation for validation purpose.