Add tests for middleware, add mocking for unit tests