I am trying to write a unit test for my ColdBox application running on Lucee 4.5 using testbox for a function that includes a cfhtmlhead()
call.
Unfortunately, the string that would normally be appended to the <head>
section of the HTML output using that function is appended to the output of the unit test instead, causing the test to fail.
The output of cfhtmlhead()
is obviously written to a special buffer. According to a blog post it is possible to clear that buffer. The example function shown there looks like this:
function clearHeaderBuffer() {
local.out = getPageContext().getOut();
while (getMetaData(local.out).getName() is "coldfusion.runtime.NeoBodyContent") {
local.out = local.out.getEnclosingWriter();
}
local.method = local.out.getClass().getDeclaredMethod("initHeaderBuffer", arrayNew(1));
local.method.setAccessible(true);
local.method.invoke(local.out, arrayNew(1));
}
Though the blog post is written for Adobe ColdFusion and it obviously doesn't work the same way in Lucee.
By dumping local.out
I saw that the object has a method resetHTMLHead()
. But calling that method doesn't seem to work, either (even when the related getHTMLHead()
method outputs the string from the cfhtmlhead()
call).
So, how to reset the header buffer in Lucee?
I found the answer by checking the Lucee sources. There the buffer is accessed via
getRootOut().getHTMLHead()
.So the code to clear the header buffer boils down to this: