Test coverage and refactor for configuring middleware