Unit-Testing HTTP header output

I’m writing a CGI application that uses cookies. When I initially
started the project, I looked about for a good method of unit testing
the methods related to cookie handling. I ended up mocking CGI, and
passing it in.

Mocking CGI worked fine for unit testing the class’s response to
already set cookies. But, I’m finding myself at a loss as to a good
way (short of capturing output and using regexes) to test that the
class properly sets the cookies.

What I’m currently doing is implementing MockCGI#header, and then I
inspect the cookies that were passed in. But, there’s no guarantee
that when I call CGI#header that the headers are actually being output
to the client, and I’d like my unit tests to reflect that.

If I were using the Apache classes provided with mod_ruby, this
wouldn’t be an issue. I could just override Apache#headers_out and
mock up an Apache::Table object. But, I want this to run with CGI.

Any suggestions?

Thanks,
Samuel