A blog about software development, primarily in Java and about web applications.

Thursday, October 13, 2011

Mock Server/Socket Process Testing

I recently had to do some development against a socket-level API daemon process that a 3rd party company runs as a service for us. During the initial set up period, I didn't have reliable access to the service for testing. I also wanted to test how my code handled getting weird and unusual responses from their server. I do my development on Windows 7 and the code is in Java and gets deployed to Linux machines. I run Cygwin and rely heavily on it. A simple solution was to use the nc command to create a server process listening on a port. My java process would then connect to this process and send a request. To simulate a response going to back to my process, I could simply cut-n-paste the XML response (as described in the vendor's API documentation). This was the command to start the mock server process listening on port 19110:
nc -t -l -p 19110
To test connections to this mock server process you can run:
nc localhost 19110
or
telnet localhost 19110

1 comment:

huggyfee said...

Hi. Thanks for this - I used it many years ago and couldn't for the life of me remember which command it was - great for troubleshooting firewall issues. Tx!