Skip to content

Fix: getResponseHeaders() returns empty struct after setup()#641

Open
homestar9 wants to merge 1 commit intoColdBox:developmentfrom
homestar9:hotfix/mock-request-context-fix
Open

Fix: getResponseHeaders() returns empty struct after setup()#641
homestar9 wants to merge 1 commit intoColdBox:developmentfrom
homestar9:hotfix/mock-request-context-fix

Conversation

@homestar9
Copy link
Contributor

@homestar9 homestar9 commented Feb 26, 2026

MockRequestContext.getResponseHeaders() always returns an empty struct {} in integration tests, even after calling setHTTPHeader(). This causes test assertions on response headers to fail unexpectedly when setup() is called between tests.

Note: I didn't see a specific test file for MockRequestContext, so I added my test to RequestContextTest.

Jira Issues

https://ortussolutions.atlassian.net/browse/COLDBOX-1380

Type of change

Please delete options that are not relevant.

  • [x ] Bug Fix

Checklist

  • [ x] My code follows the style guidelines of this project cfformat
  • [ x] I have commented my code, particularly in hard-to-understand areas
  • [x ] I have added tests that prove my fix is effective or that my feature works
  • [x ] New and existing unit tests pass locally with my changes

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.
Copilot AI review requested due to automatic review settings February 26, 2026 04:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in MockRequestContext where getResponseHeaders() always returns an empty struct even after setHTTPHeader() is called. The issue occurs because MockRequestContext's overridden setHTTPHeader() method only updates the mock-specific cbox_headers collection but doesn't update the variables.responseHeaders that getResponseHeaders() reads from.

Changes:

  • Updated MockRequestContext.setHTTPHeader() to populate variables.responseHeaders when headers are set
  • Added test coverage to verify getResponseHeaders() returns headers set via setHTTPHeader()

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
system/testing/mock/web/context/MockRequestContext.cfc Added line to sync responseHeaders when setHTTPHeader() is called with a header name
tests/specs/web/context/RequestContextTest.cfc Added test case to verify MockRequestContext populates responseHeaders correctly

Comment on lines +594 to +611
function testMockRequestContextPopulatesResponseHeaders(){
// MockRequestContext.setHTTPHeader() should populate variables.responseHeaders
// so that getResponseHeaders() works correctly in integration tests
var mockEvent = getMockBox().createMock( "coldbox.system.testing.mock.web.context.MockRequestContext" );
mockEvent.init( properties = props, controller = mockController );

// Set custom headers
mockEvent.setHTTPHeader( name = "x-custom-header", value = "test-value" );
mockEvent.setHTTPHeader( name = "cached-data", value = "false" );

// getResponseHeaders() should return the headers that were set
var headers = mockEvent.getResponseHeaders();

expect( headers ).toHaveKey( "x-custom-header" );
expect( headers[ "x-custom-header" ] ).toBe( "test-value" );
expect( headers ).toHaveKey( "cached-data" );
expect( headers[ "cached-data" ] ).toBe( "false" );
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test function uses space indentation instead of tabs. According to the project's .cfformat.json configuration, "tab_indent": true, all indentation should use tabs, not spaces. Please update the indentation throughout this function to use tabs to match the project's formatting standards.

Suggested change
function testMockRequestContextPopulatesResponseHeaders(){
// MockRequestContext.setHTTPHeader() should populate variables.responseHeaders
// so that getResponseHeaders() works correctly in integration tests
var mockEvent = getMockBox().createMock( "coldbox.system.testing.mock.web.context.MockRequestContext" );
mockEvent.init( properties = props, controller = mockController );
// Set custom headers
mockEvent.setHTTPHeader( name = "x-custom-header", value = "test-value" );
mockEvent.setHTTPHeader( name = "cached-data", value = "false" );
// getResponseHeaders() should return the headers that were set
var headers = mockEvent.getResponseHeaders();
expect( headers ).toHaveKey( "x-custom-header" );
expect( headers[ "x-custom-header" ] ).toBe( "test-value" );
expect( headers ).toHaveKey( "cached-data" );
expect( headers[ "cached-data" ] ).toBe( "false" );
}
function testMockRequestContextPopulatesResponseHeaders(){
// MockRequestContext.setHTTPHeader() should populate variables.responseHeaders
// so that getResponseHeaders() works correctly in integration tests
var mockEvent = getMockBox().createMock( "coldbox.system.testing.mock.web.context.MockRequestContext" );
mockEvent.init( properties = props, controller = mockController );
// Set custom headers
mockEvent.setHTTPHeader( name = "x-custom-header", value = "test-value" );
mockEvent.setHTTPHeader( name = "cached-data", value = "false" );
// getResponseHeaders() should return the headers that were set
var headers = mockEvent.getResponseHeaders();
expect( headers ).toHaveKey( "x-custom-header" );
expect( headers[ "x-custom-header" ] ).toBe( "test-value" );
expect( headers ).toHaveKey( "cached-data" );
expect( headers[ "cached-data" ] ).toBe( "false" );
}

Copilot uses AI. Check for mistakes.
else if ( structKeyExists( arguments, "name" ) ) {
var headers = getValue( "cbox_headers", {} );
headers[ lCase( arguments.name ) ] = arguments.value;
// Keep variables.responseHeaders in sync so getResponseHeaders() works in tests
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment line uses space indentation instead of tabs. According to the project's .cfformat.json configuration, "tab_indent": true, all indentation should use tabs, not spaces. Please update the indentation to use tabs to match the project's formatting standards.

Suggested change
// Keep variables.responseHeaders in sync so getResponseHeaders() works in tests
// Keep variables.responseHeaders in sync so getResponseHeaders() works in tests

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants