HTTP Server — Day 2 — Routes
Today I needed to put in a loop to allow a ClientSocket connection to remain open so any number of Requests can be retrieved from the Socket’s ‘in’ stream, rather than the server terminating after a client connects.
This took what felt like ages (but probably wasn’t all that long). The issue I was having wasn’t a problem for my unit tests, but only occurred when trying to run the Fitnesse tests. The tests would connect, and I could see the server was accepting the Request and sending back a Response on the Client socket’s output stream, but the test wouldn’t terminated. I tried all sorts of things like trying to close the ServerSocket or Client Socket, and I wondered if there would be a null value read off of the In-stream or something, but, NO. Finally I had to discuss it and the one thing I hadn’t tried was closing the stream itself. This was the answer!
So then on to getting Simple GET, PUT and POST requests to receive a simple 200 response. I hadn’t needed any routing up to this point for the FourOhFour tests as the simplest thing was to just have the one response, but now I did have a reason for a Router and Routes. I again have started with this in it’s simplest form e.g. passing in a list of Routes to the Router where the Route object just holds the method and path for that Route and holds a string representation of the ‘200’ response for now.
I feel like I’ve done a lot of work in preparation for the ‘Simple’ acceptance tests but it was only late yesterday that I was able to get them to pass. I am assessing today whether I could have done things even simpler in order to get them to pass sooner. Possibly this is so, but I’m relatively confident that the work I have done won’t be wasted.