Fixing cookie commands. Include port of java cookie regression tests.

Fixes issue 5321.