Fix: getResponseHeaders() returns empty struct after setup()#641
Fix: getResponseHeaders() returns empty struct after setup()#641homestar9 wants to merge 1 commit intoColdBox:developmentfrom
Conversation
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.
There was a problem hiding this comment.
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 |
| 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" ); | ||
| } |
There was a problem hiding this comment.
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.
| 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" ); | |
| } |
| 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 |
There was a problem hiding this comment.
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.
| // Keep variables.responseHeaders in sync so getResponseHeaders() works in tests | |
| // Keep variables.responseHeaders in sync so getResponseHeaders() works in tests |
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.
Checklist