Refactor mock hosts middleware