Testing for cfheader in cfwheels

In the process of trying to write a plugin for cfwheels, I can across the issue of trying to test to see if “cfheader” had been set.

So, if you had:

<cfheader name="foo" value="foo">

How do you test for this in rocketunit? Specifically, I had a function, setCustomHeader() which literally calls wheels’s internal $header() function (which in turn sets cfheader);

Turns out there’s a solution by looking directly at the page context:

// Set a custom header and check it's in the response
function Test_Set_Custom_Header_Foo(){
  setCustomHeader(name="foo", value="bar");
  loc.headers=getPageContext().getResponse().getHeaderNames();
  loc.headerValue=getPageContext().getResponse().getHeader('foo');
  assert("loc.headers.toString() CONTAINS 'foo'");
  assert("loc.headerValue EQ 'bar'");
}

In this example, using getReponse().getHeader() returns a java.util.hashSet, where we can just convert to a string and look for the value we’ve set. Nifty!