Skip to content

Conversation

@akapug
Copy link
Member

@akapug akapug commented Dec 9, 2025

Ready for review Powered by Pull Request Badge

Summary

Fixes #1817 - Response Content-Type header is overwritten by mapResponseBody()

Problem

When using elide serve with a fetch handler that returns a Response with a custom Content-Type header, the header was being unconditionally overwritten based on body type:

// User code
return new Response("<h1>Hello</h1>", { 
  headers: { "Content-Type": "text/html; charset=utf-8" }
});

// Actual response: Content-Type: text/plain (wrong!)

Note: This only affects the declarative fetch handler pattern (elide serve). The node:http imperative pattern (used by apps like compare-gpt-elide) is not affected.

Regression Source

Bug introduced in PR #1736 ("feat: new server engine"). The mapResponseBody() function was added with unconditional headers.set("Content-Type", ...) calls.

Solution

Modified mapResponseBody() to check if Content-Type is already set before applying defaults:

// Before (bug):
headers.set("Content-Type", "text/plain")

// After (fix):
if (!headers.has("Content-Type")) headers.set("Content-Type", "text/plain")

This preserves user-specified Content-Type headers while still providing sensible defaults when none is specified.

Testing

Added regression tests in FetchIntrinsicTest.kt:

  • response should preserve user-specified Content-Type header
  • response should use default Content-Type when not specified
  • response should preserve custom headers

Manual testing:

  • Tested with elide serve - custom Content-Type: text/html now preserved
  • Custom headers like X-Custom were already working (only Content-Type was affected)

Changelog

  • fix(graalvm): preserve user-specified Content-Type header in Response
  • test(graalvm): add regression tests for Content-Type header preservation

Previously, mapResponseBody() unconditionally overwrote the Content-Type
header based on body type (string→text/plain, buffer→octet-stream, etc.).

This fix checks if Content-Type is already set before applying defaults,
allowing users to specify custom Content-Type headers like text/html.

Fixes #1817
@akapug akapug requested a review from sgammon as a code owner December 9, 2025 21:06
@akapug akapug requested a review from darvld December 9, 2025 21:15
- Test that user-specified Content-Type is preserved
- Test that default Content-Type is applied when not specified
- Test that other custom headers work correctly
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.

Bug: Response Content-Type header is overwritten by mapResponseBody()

2 participants