Add tests for the cookie handling. Fix a couple of errors when handling cookie.