Be more restrictive on allowed hostnames

And add tests for valid hostnames
Fix #749