Add configurable cleanup on middleware (And unit tests with py.test)