Fix issue in the middleware handler, add middlware use and handler tests