Skip to content

Commit 633e348

Browse files
committed
Fix: getResponseHeaders() returns empty struct after setup()
MockRequestContext.getResponseHeaders() always returns an empty struct {} in integration tests, even after calling setHTTPHeader() with named headers. This causes test assertions on response headers to fail unexpectedly when setup() is called between tests.
1 parent ff8ff28 commit 633e348

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

system/testing/mock/web/context/MockRequestContext.cfc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ component
2929
else if ( structKeyExists( arguments, "name" ) ) {
3030
var headers = getValue( "cbox_headers", {} );
3131
headers[ lCase( arguments.name ) ] = arguments.value;
32+
// Keep variables.responseHeaders in sync so getResponseHeaders() works in tests
33+
variables.responseHeaders[ arguments.name ] = arguments.value;
3234
setValue( "cbox_headers", headers );
3335
} else {
3436
throw(

tests/specs/web/context/RequestContextTest.cfc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,25 @@ component extends="coldbox.system.testing.BaseModelTest" {
591591
event.setHTTPHeader( name = "expires", value = "#now()#" );
592592
}
593593

594+
function testMockRequestContextPopulatesResponseHeaders(){
595+
// MockRequestContext.setHTTPHeader() should populate variables.responseHeaders
596+
// so that getResponseHeaders() works correctly in integration tests
597+
var mockEvent = getMockBox().createMock( "coldbox.system.testing.mock.web.context.MockRequestContext" );
598+
mockEvent.init( properties = props, controller = mockController );
599+
600+
// Set custom headers
601+
mockEvent.setHTTPHeader( name = "x-custom-header", value = "test-value" );
602+
mockEvent.setHTTPHeader( name = "cached-data", value = "false" );
603+
604+
// getResponseHeaders() should return the headers that were set
605+
var headers = mockEvent.getResponseHeaders();
606+
607+
expect( headers ).toHaveKey( "x-custom-header" );
608+
expect( headers[ "x-custom-header" ] ).toBe( "test-value" );
609+
expect( headers ).toHaveKey( "cached-data" );
610+
expect( headers[ "cached-data" ] ).toBe( "false" );
611+
}
612+
594613
function testGetHTTPContent(){
595614
var event = getRequestContext();
596615
test = event.getHTTPContent();

0 commit comments

Comments
 (0)