Unit Testing Simple Request-Response Protocols

Many of the XMPP protocols are simple: one side sends an IQ request, the other side receives the request, processes it, and responds with an IQ result. An example of such a simple request-response protocol is the software version protocol illustrated earlier. An implementation of this protocol consists of two parts:

  • The initiator implementation sends a software version request and processes the corresponding response when it comes in.

  • The responder listens for incoming software version requests and responds to them.

These implementations are typically implemented locally in one class for the initiator and one for the responder.[15] Example 7-1 shows how a VersionResponder class is instantiated in a client to respond to incoming software version requests. All this class does is listen for an incoming IQ query of the type jabber:iq:version, and respond with the values set through setVersion. The class uses the central XMPPClient class to send data to and receive data from the XMPP connection.

Example 7-1. Using VersionResponder to listen and respond to software version requests

class MyClient {
  MyClient() {
    xmppClient = new XMPPClient("alice@wonderland.lit", "mypass");
    versionResponder = new VersionResponder(xmppClient);
    versionResponder->setVersion("Swift", "0.1");
    xmppClient->connect();
  }
  …
};

Since the implementation of request-response protocols is local to one class, unit testing is a good way to test the functionality of the protocol ...

Get Beautiful Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.