Skip to content

fix: trim and lowercase the media type in request.type - #2019

Open
ybai08 wants to merge 1 commit into
koajs:masterfrom
ybai08:fix/request-type-ows-case
Open

ybai08 wants to merge 1 commit into
koajs:masterfrom
ybai08:fix/request-type-ows-case

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026 •

Copy link
Copy Markdown

ctx.request.type cuts the Content-Type value at the first ; and returns what comes before it unchanged. This gives the wrong result for two valid headers:

Content-Type request.type on master with this change
application/json ; charset=utf-8 'application/json ' (trailing space) 'application/json'
Application/JSON; charset=utf-8 'Application/JSON' 'application/json'

RFC 9110 section 8.3.1 allows optional whitespace before the ; that starts the parameters (media-type = type "/" subtype parameters, with parameters = *( OWS ";" OWS parameter )). It also says type and subtype are case-insensitive. So code like ctx.request.type === 'application/json' fails today for headers that are perfectly valid. ctx.is('json') already matches both, because type-is normalizes the value, so request.type currently disagrees with ctx.is().

The getter now trims the media type and lowercases it. That matches what content-type / type-is return for the same header. Headers that are already lowercase with no space before ; (the common case) give the same result as before.

Tests

  • Added two cases to __tests__/request/type.test.js (whitespace before parameters, mixed case). Both fail on master (actual: 'application/json ' and actual: 'Application/JSON') and pass with this change.
  • npm test: 468 tests, 468 pass (Node v26.10.0, macOS).
  • npx standard: clean.

The lowercasing could be seen as a behavior change for anyone comparing against a mixed-case literal. If you'd rather only trim, I can drop that part.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

Summary by Sourcery

Normalize Content-Type media types returned by request.type for standards-compliant, case-insensitive matching.

Bug Fixes:

  • Normalize request.type by trimming optional whitespace and lowercasing media types so valid Content-Type headers are interpreted consistently.

Tests:

  • Add coverage for whitespace before Content-Type parameters and mixed-case media types.

Summary by CodeRabbit

  • Bug Fixes
    • Request content types are now returned in lowercase, with surrounding whitespace and parameters removed.

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b18e6722-ba8e-48d7-95cc-53d7603f87ee
📥 Commits

Reviewing files that changed from the base of the PR and between 2fecd02 and e6a4d94.

📒 Files selected for processing: 2
  • __tests__/request/type.test.js
  • lib/request.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

📝 Walkthrough

request.type now removes Content-Type parameters, trims surrounding whitespace, and lowercases the media type. Added tests cover whitespace before parameters and mixed-case media types.

Suggested reviewers: jonathanong

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to e6a4d

Request media types are now trimmed and lowercased as intended, with tests covering whitespace before parameters and mixed case. No material merge risk is identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: trimming and lowercasing the media type returned by request.type.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai

sourcery-ai Bot commented Oct 17, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates request.type to return a trimmed, lowercase media type, bringing it into alignment with RFC 9110 and the normalization performed by content-type/type-is, with regression tests for whitespace and mixed-case headers.

File-Level Changes

Change Details Files
Normalize the media type returned by the request.type getter.
  • Trim optional whitespace before parameters.
  • Lowercase the type and subtype to reflect their case-insensitive semantics.
  • Continue returning an empty string when Content-Type is absent.
lib/request.js
Add regression coverage for valid Content-Type formatting variations.
  • Verify whitespace before parameters is removed.
  • Verify mixed-case media types are returned in lowercase.
__tests__/request/type.test.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (2fecd02) to head (e6a4d94).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2019   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files           9        9           
  Lines        2144     2146    +2     
=======================================
+ Hits         2142     2144    +2     
  Misses          2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@yowainwright

Copy link
Copy Markdown
Member

This is a good fix that I think can be deferred for v4.
An issue/concern is that it would change the return value.
Thank you @ybai08 !!!

@ybai08

ybai08 commented Oct 21, 2026

Copy link
Copy Markdown
Author

Thanks! Agreed. A header like Text/HTML; charset=utf-8 would now give text/html, so it changes what request.type returns. I'm happy to leave this open for the v4.0.0 milestone and rebase it when v4 work starts. I can also add a note to the breaking-changes list for v4 if that would help.

@yowainwright

Copy link
Copy Markdown
Member

@ybai08 we're good, I think/hope. I am hoping to do a patch release soon.
Then we can start on v4. Hopefully no whammies! 🤞

Great work! It's much appreciated.

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