Move middleware functions into middleware module and create associated tests
Note: this only moves middleware functions that have associated tests.