Skip to content

🐛 bug: Fix handling of wildcard matching in acceptsOffer#3880

Merged
ReneWerner87 merged 3 commits into
mainfrom
update-acceptsoffer-for-prefix-matching
Nov 21, 2025
Merged

🐛 bug: Fix handling of wildcard matching in acceptsOffer#3880
ReneWerner87 merged 3 commits into
mainfrom
update-acceptsoffer-for-prefix-matching

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented Nov 19, 2025

Summary

  • This pull request enhances the robustness of content negotiation by refining the logic for wildcard matching in offer specifications. It ensures that wildcard patterns like utf-* are interpreted more precisely, matching only offers that begin with the specified prefix. Concurrently, it introduces a comprehensive suite of new tests to validate this improved behavior across various scenarios, including complex negotiations with quality factors.

Copilot AI review requested due to automatic review settings November 19, 2025 13:09
@gaby gaby requested a review from a team as a code owner November 19, 2025 13:09
@gaby gaby requested a review from efectn November 19, 2025 13:09
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 19, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

The acceptsOffer function in helpers.go was changed to treat specs ending with * as prefix patterns and perform case-insensitive prefix matching against the offer (empty prefix returns true, shorter offers return false). New tests in helpers_test.go exercise wildcard utf-* cases and complex Accept-* negotiation with quality values.

Changes

Cohort / File(s) Summary
Wildcard matching logic
helpers.go
Updated acceptsOffer to treat specs ending with * as prefix patterns: compute prefix (spec without *) and perform case-insensitive prefix match against the offer; handle empty-prefix and offer-length edge cases; non-wildcard behavior remains case-insensitive equality.
Test coverage for wildcard negotiation
helpers_test.go
Added tests covering utf-* wildcard cases (match, non-match, empty results) and complex Accept-* negotiation scenarios combining multiple quality values to validate prioritized selection and no-match cases.
Module manifest
go.mod
No functional code changes; included in diff for completeness.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Negotiator as negotiation routine
    participant Helpers as acceptsOffer(spec, offer)
    Note over Helpers: Handles trailing "*" as prefix pattern (case-insensitive)
    Negotiator->>Helpers: acceptsOffer(spec, offer)
    alt spec ends with "*"
        Helpers->>Helpers: prefix = spec[:-1]
        alt prefix == ""
            Helpers-->>Negotiator: true
        else if len(offer) < len(prefix)
            Helpers-->>Negotiator: false
        else
            Helpers-->>Negotiator: return strings.HasPrefix(strings.ToLower(offer), strings.ToLower(prefix))
        end
    else
        Helpers-->>Negotiator: return strings.EqualFold(spec, offer)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay attention to edge cases: empty prefix behavior, offer shorter than prefix, and case-folding via EqualFold/ToLower consistency.
  • Verify tests cover both matching and non-matching wildcard permutations and quality-based prioritization.

Possibly related PRs

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐰 I nibble prefixes, careful and spry,
Stars trimmed to patterns, no longer sky-high.
I fold cases softly, hop tests in a row,
Wildcards behave now — a neat little show. ✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description is minimal and only provides a brief summary. It lacks required sections from the template such as issue reference, detailed changes, type of change, and verification checklist. Add required template sections including issue reference (Fixes #), detailed changes list with checklist items, type of change selection, and completion of the verification checklist.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: fixing wildcard matching in acceptsOffer. It is concise, specific, and directly relates to the core modification in the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch update-acceptsoffer-for-prefix-matching

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 44d3b31 and d909d33.

📒 Files selected for processing (1)
  • helpers.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • helpers.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: CodeQL analysis (go)
  • GitHub Check: Agent
  • GitHub Check: unit (1.25.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: lint
  • GitHub Check: Compare

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 and usage tips.

@ReneWerner87 ReneWerner87 added this to v3 Nov 19, 2025
@ReneWerner87 ReneWerner87 added this to the v3 milestone Nov 19, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of content negotiation by refining the logic for wildcard matching in offer specifications. It ensures that wildcard patterns like utf-* are interpreted more precisely, matching only offers that begin with the specified prefix. Concurrently, it introduces a comprehensive suite of new tests to validate this improved behavior across various scenarios, including complex negotiations with quality factors.

Highlights

  • Wildcard Offer Logic Refinement: The acceptsOffer function now correctly handles prefix-based wildcards (e.g., utf-*) by ensuring the offer starts with the specified prefix, rather than matching any offer.
  • Expanded Test Coverage: New test cases have been added for Accept-Charset wildcards, including basic prefix matching, prefix-mismatch regressions, and complex negotiation scenarios with varying quality weights.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes a bug in wildcard matching for acceptsOffer and adds a comprehensive set of tests to cover various wildcard scenarios, including prefix matching and complex content negotiation with quality weights. The changes are solid, and the added tests are thorough. I have one suggestion to simplify the implementation in acceptsOffer for better readability and maintainability.

Comment thread helpers.go
@gaby gaby changed the title Expand wildcard offer tests 🧹 chore: Improve handling of prefix matching in acceptsOffer Nov 19, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.71%. Comparing base (1aa8423) to head (d909d33).
⚠️ Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3880      +/-   ##
==========================================
+ Coverage   91.68%   91.71%   +0.03%     
==========================================
  Files         115      115              
  Lines        9967     9970       +3     
==========================================
+ Hits         9138     9144       +6     
+ Misses        530      527       -3     
  Partials      299      299              
Flag Coverage Δ
unittests 91.71% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Copy Markdown
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 pull request expands test coverage for wildcard pattern matching in content negotiation, specifically for Accept-Charset header handling. The implementation correctly handles prefix-based wildcard matching with quality weight negotiation.

Key Changes

  • Enhanced wildcard matching logic to properly validate prefix matches rather than accepting all wildcards unconditionally
  • Added comprehensive test coverage for basic wildcard prefix matching scenarios
  • Added complex negotiation tests with overlapping quality weights to ensure correct selection priority

Reviewed Changes

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

File Description
helpers.go Fixed wildcard matching in acceptsOffer to verify that offers actually start with the wildcard prefix, preventing false positive matches for patterns like utf-*
helpers_test.go Added 11 new test cases covering wildcard prefix validation (basic cases, prefix mismatch scenarios) and complex quality-weighted negotiation with multiple competing patterns

@gaby gaby changed the title 🧹 chore: Improve handling of prefix matching in acceptsOffer 🐛 bug: Fix handling of wildcard matching in acceptsOffer Nov 19, 2025
Copy link
Copy Markdown
Member

@renanbastos93 renanbastos93 left a comment

Choose a reason for hiding this comment

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

Suggested Improvement

Great fix! The logic is now correctly aligned with the expected behavior for prefix wildcards (e.g., utf-*).

I’d like to suggest one improvement to increase readability, maintainability, and test quality: extract the wildcard-matching logic into a dedicated helper function.

Right now, acceptsOffer is correct, but it handles two responsibilities at once (exact matching and wildcard matching). Splitting this into smaller helpers makes the intent clearer and makes unit testing far easier, especially for edge cases. This also reduces the chance of future regressions.

Example:

func acceptsOffer(spec, offer string, _ headerParams) bool {
    if hasWildcardSuffix(spec) {
        return matchWildcardOffer(spec, offer)
    }
    return utils.EqualFold(spec, offer)
}

func hasWildcardSuffix(s string) bool {
    return len(s) > 0 && s[len(s)-1] == '*'
}

func matchWildcardOffer(spec, offer string) bool {
    // spec is ASCII-based (e.g. "utf-*"), last byte is always '*'
    prefix := spec[:len(spec)-1]

    // '*' alone accepts anything
    if prefix == "" {
        return true
    }

    if len(offer) < len(prefix) {
        return false
    }

    return utils.EqualFold(prefix, offer[:len(prefix)])
}

Benefits of extracting the logic:

  • Improved readability: the flow becomes easier to understand at a glance.
  • Better testability: matchWildcardOffer and hasWildcardSuffix can be tested independently.
  • More granular test coverage: easier to cover edge cases for prefix matching vs. exact matching.
  • Simpler future maintenance: if new wildcard patterns need to be supported later, the structure already accommodates this cleanly.

Overall, the fix is great — this is just an incremental improvement to make it even more robust. If it helps, I’m happy to open a follow-up PR with the refactor.

@gaby gaby requested a review from Copilot November 20, 2025 04:46
@gaby
Copy link
Copy Markdown
Member Author

gaby commented Nov 20, 2025

/gemini review

@gaby
Copy link
Copy Markdown
Member Author

gaby commented Nov 20, 2025

@codex review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a bug in the wildcard matching logic within the acceptsOffer function. The previous implementation, which incorrectly accepted any offer for a wildcard specification, has been replaced with proper prefix-based matching. The change is clear, correct, and well-contained. Furthermore, the addition of extensive test cases in helpers_test.go provides solid validation for the fix, covering various wildcard scenarios and complex content negotiation cases. Overall, this is an excellent contribution that improves the robustness of the framework.

Copy link
Copy Markdown
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

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

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ReneWerner87 ReneWerner87 merged commit 4f1c033 into main Nov 21, 2025
21 of 25 checks passed
@ReneWerner87 ReneWerner87 deleted the update-acceptsoffer-for-prefix-matching branch November 21, 2025 09:45
@github-project-automation github-project-automation Bot moved this to Done in v3 Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants