I need to create a brain-dead HTTP server that returns 404s for everything on localhost. Specifically, I have a program under an acceptance test harness which calls out to a server, but for testing I want to save time by skipping that unrelated check.
I have a way to pass in the URL the program uses as part of the test harness, and the test harness (currently) just creates such a server on port 80.
However, a problem appears if more than one test attempts to run at the same time, because each test harness tries to create an HTTP server on port 80, which fails for one of the harnesses.
I'd therefore like to randomize the port and ensure it is available before attempting to create the HTTP server. How can I check if that port is in use?