Skip to content

setCharset function does not preserve case of original Content-Type #6613

@xzyingxiashubro

Description

@xzyingxiashubro

Environment information

Version: express 5.0.0-beta.3

Platform: Linux

Node.js version: 18.x

Any other relevant information: N/A

What steps will reproduce the bug?

  1. Import the utils module from Express
  2. Call the setCharset function with an uppercase Content-Type and a charset
  3. Observe that the returned string converts the Content-Type to lowercase
const utils = require('express/lib/utils');

// Call setCharset with uppercase Content-Type
const contentType = 'TEXT/PLAIN';
const charset = 'UTF-8';
const result = utils.setCharset(contentType, charset);

console.log(result); 
// Actual: 'text/plain; charset=UTF-8'
// Expected: 'TEXT/PLAIN; charset=UTF-8'

What is the expected behavior?

The function should preserve the original case of the Content-Type while adding the charset parameter. For example, when TEXT/PLAIN is provided as input, the output should be TEXT/PLAIN; charset=UTF-8.

What happens instead?

The function converts the Content-Type to lowercase, so TEXT/PLAIN becomes text/plain; charset=UTF-8 in the output.

Source of the issue

The issue is in the setCharset function in lib/utils.js.
The function uses contentType.parse() which normalizes the type to lowercase, and then contentType.format() which outputs the type in lowercase, resulting in the loss of the original case information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions