🧹 chore: Fix AcceptsLanguages() RFC compliance#3672
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit 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. Warning Rate limit exceeded@gaby has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 47 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (2)
WalkthroughThis change introduces RFC 4647 Extended Filtering support for language negotiation by adding new methods and helpers for extended language matching. It updates interface comments for clarity, expands test coverage for both basic and extended filtering, and documents the new functionality. Supported content types for auto-formatting are also updated in interface comments. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Server
participant Req
participant Helpers
Client->>Server: HTTP Request with Accept-Language header
Server->>Req: Call AcceptsLanguages(offers...) or AcceptsLanguagesExtended(offers...)
Req->>Helpers: Use acceptsLanguageOfferBasic or acceptsLanguageOfferExtended
Helpers-->>Req: Return matching offer (or empty string)
Req-->>Server: Return selected language
Server-->>Client: Response based on negotiated language
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~18 minutes Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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!
I've implemented enhancements to the Accept-Language header parsing, introducing support for RFC 4647 Extended Filtering and refining the existing basic filtering to correctly reject invalid language ranges. This change also updates the AutoFormat method's documentation to reflect additional supported content types.
Highlights
- Improved Accept-Language Basic Filtering: The
AcceptsLanguagesmethod now strictly adheres to RFC 4647 Basic Filtering, which means it will reject language ranges that contain a wildcard (*) after a hyphen (e.g.,en-*). - New Extended Language Filtering: I've introduced a new method,
AcceptsLanguagesExtended, which implements RFC 4647 Extended Filtering. This allows for more flexible language range matching, including the use of wildcard subtags (e.g.,en-*or*-US). - Updated Documentation: The API documentation for
CtxandReqinterfaces, as well as thedocs/api/ctx.mdfile, have been updated to reflect the newAcceptsLanguagesExtendedmethod and clarify the behavior ofAcceptsLanguages. - Expanded AutoFormat Support: The
AutoFormatmethod's documentation has been updated to includeapplication/vnd.msgpackandapplication/cboras additional supported content types.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 or fill out our survey to provide feedback.
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
-
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. ↩
There was a problem hiding this comment.
Pull Request Overview
This PR enhances Accept-Language header processing by implementing RFC 4647 Basic and Extended Filtering standards. It rejects invalid language ranges containing wildcards after hyphens and adds support for extended filtering with wildcard subtags.
- Adds
AcceptsLanguagesExtended()method implementing RFC 4647 Extended Filtering with wildcard support - Updates existing
AcceptsLanguages()to use strict RFC 4647 Basic Filtering that rejects invalid wildcard patterns - Updates documentation to reflect expanded content type support and new language filtering methods
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| helpers.go | Implements Basic and Extended language filtering algorithms with wildcard validation |
| req.go | Adds AcceptsLanguagesExtended method and updates AcceptsLanguages to use Basic filtering |
| helpers_test.go | Updates and adds test cases for both Basic and Extended language filtering |
| ctx_test.go | Adds comprehensive tests for the new AcceptsLanguagesExtended functionality |
| docs/api/ctx.md | Documents the new AcceptsLanguagesExtended method with usage examples |
| *_interface_gen.go | Updates generated interfaces with new method signatures and documentation |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3672 +/- ##
==========================================
+ Coverage 91.85% 91.90% +0.05%
==========================================
Files 113 113
Lines 11453 11492 +39
==========================================
+ Hits 10520 10562 +42
+ Misses 669 667 -2
+ Partials 264 263 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces extended language filtering and rejects invalid language ranges. The core logic for extended filtering in helpers.go is not fully compliant with RFC 4647, as it allows prefix matching instead of requiring an equal number of subtags. I've identified this as a critical issue and provided a fix. Additionally, I've suggested improvements to the test structure for better maintainability and to the documentation for clarity.
|
Working code-review comments now |
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (3)
docs/api/ctx.md (1)
524-526: Clarify the AcceptsLanguagesExtended example with a concrete header and expected outputThe current example “depends on extended ranges” is ambiguous. Replace it with a concrete Accept-Language header and expected result. Also consider adding a second example for a different wildcard pattern.
Apply this diff:
- c.AcceptsLanguagesExtended("en-US", "fr-CA") - // depends on extended ranges in the request header + // Accept-Language: en-*, fr-CA + c.AcceptsLanguagesExtended("en-US", "de-DE") // "en-US"Optional follow-ups (nearby in the same example block):
- Example 2:
- Header: “Accept-Language: *-US”
- Call: c.AcceptsLanguagesExtended("en-US", "fr-CA") // "en-US"
- Note invalid patterns:
- Header: “Accept-Language: en-US-*” → c.AcceptsLanguagesExtended("en-US") // "" (invalid range is rejected)
ctx_test.go (1)
335-349: Refactor to table-driven tests for extended filtering scenariosThe assertions are correct and valuable. For maintainability and extensibility, convert to a table-driven test as previously suggested.
Here’s a table-driven variant:
func Test_Ctx_AcceptsLanguagesExtended(t *testing.T) { t.Parallel() app := New() c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) testCases := []struct { name string header string offers []string expected string }{ {"wildcard suffix matches", "en-*", []string{"en-US"}, "en-US"}, {"wildcard prefix matches", "*-US", []string{"en-US", "fr-CA"}, "en-US"}, {"invalid trailing wildcard", "en-US-*", []string{"en-US"}, ""}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { c.Request().Header.Set(HeaderAcceptLanguage, tc.header) require.Equal(t, tc.expected, c.AcceptsLanguagesExtended(tc.offers...)) }) } }helpers.go (1)
204-209: Micro-opt: avoid Contains; use IndexByte (duplicate of existing feedback)Use IndexByte to avoid extra allocs/scans and keep the intent explicit that only the presence of '' matters (spec == "" already handled).
- if strings.Contains(spec, "*") { // only "*" is allowed in Basic + if strings.IndexByte(spec, '*') != -1 { // only "*" is allowed in Basic return false }
🧹 Nitpick comments (3)
helpers_test.go (2)
66-73: Good Basic Filtering coverage; add a couple of edge casesLooks correct and aligns with RFC 4647 Basic Filtering. Consider adding:
- A positive case for the sole wildcard.
- A negative selection case where a wildcard range is rejected under Basic but a concrete tag is still chosen.
You can extend this block like so:
require.True(t, acceptsLanguageOfferBasic("EN", "en-us", nil)) require.False(t, acceptsLanguageOfferBasic("en", "en_US", nil)) require.Equal(t, "en-US", getOffer([]byte("fr-CA;q=0.8, en-US"), acceptsLanguageOfferBasic, "en-US", "fr-CA")) require.Equal(t, "", getOffer([]byte("xx"), acceptsLanguageOfferBasic, "en")) require.False(t, acceptsLanguageOfferBasic("en-*", "en-US", nil)) +// sole wildcard should match anything +require.True(t, acceptsLanguageOfferBasic("*", "en-US", nil)) +// wildcard range is invalid in Basic; ensure negotiation still picks a concrete alternative +require.Equal(t, "fr-CA", getOffer([]byte("en-*;q=1.0, fr-CA;q=0.8"), acceptsLanguageOfferBasic, "en-US", "fr-CA"))
74-79: Extended Filtering tests look solid; add a couple more to pin semanticsThese cases validate wildcard-per-subtag and length rules well. Two more helpful assertions:
- en-*-US should match en-Latn-US
- *-US should not match en-Latn-US (position must match)
Suggested additions:
require.True(t, acceptsLanguageOfferExtended("en-*", "en-US", nil)) require.True(t, acceptsLanguageOfferExtended("*-US", "en-US", nil)) require.False(t, acceptsLanguageOfferExtended("en-US-*", "en-US", nil)) require.Equal(t, "en-US", getOffer([]byte("fr-CA;q=0.8, en-*"), acceptsLanguageOfferExtended, "en-US", "fr-CA")) +require.True(t, acceptsLanguageOfferExtended("en-*-US", "en-Latn-US", nil)) +require.False(t, acceptsLanguageOfferExtended("*-US", "en-Latn-US", nil))helpers.go (1)
218-240: Extended Filtering: correct per-subtag wildcard semantics
- Permits wildcard as a full subtag that matches exactly one subtag.
- Requires the offer to have at least as many subtags as the range.
- Allows extra trailing subtags in the offer, which is correct for extended filtering.
Optional micro-optimization: fast-path exact match to skip splits.
func acceptsLanguageOfferExtended(spec, offer string, _ headerParams) bool { + // Fast path for exact case-insensitive match + if utils.EqualFold(spec, offer) { + return true + } if spec == "*" { return true }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
ctx_interface_gen.go(2 hunks)ctx_test.go(2 hunks)docs/api/ctx.md(2 hunks)helpers.go(1 hunks)helpers_test.go(1 hunks)req.go(1 hunks)req_interface_gen.go(1 hunks)res_interface_gen.go(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
docs/**
📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)
Review and update the contents of the
docsfolder if necessary when modifying code
Files:
docs/api/ctx.md
🧠 Learnings (11)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
Applied to files:
docs/api/ctx.mdctx_interface_gen.goctx_test.go
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Applied to files:
docs/api/ctx.mdctx_interface_gen.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/csrf/csrf_test.go:188-193
Timestamp: 2024-10-08T19:06:06.583Z
Learning: In the Fiber framework tests, using `ctx.Response.Header.Cookie` may not be suitable for parsing cookies from the response header, as it requires a `*Cookie` and fills it rather than returning a string value; thus, manual parsing of the `Set-Cookie` header may be necessary.
Applied to files:
docs/api/ctx.mdctx_test.go
📚 Learning: 2024-11-15T07:56:21.623Z
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
Applied to files:
docs/api/ctx.mdhelpers_test.goctx_test.go
📚 Learning: 2024-12-13T08:14:22.851Z
Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the `require` methods from the `testify` package for assertions instead of manual comparisons and calls to `t.Fatal` or `t.Fatalf`.
Applied to files:
helpers_test.goctx_test.go
📚 Learning: 2024-07-26T21:00:12.902Z
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Applied to files:
helpers_test.goctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests in this project use testify require.
Applied to files:
helpers_test.go
📚 Learning: 2024-11-29T12:37:27.581Z
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the `Test_App_ShutdownWithContext` function in `app_test.go`, the `clientDone` channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional `time.Sleep` calls.
Applied to files:
ctx_test.go
📚 Learning: 2024-10-16T12:12:30.506Z
Learnt from: gaby
PR: gofiber/fiber#3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
Applied to files:
ctx_test.gohelpers.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:22-25
Timestamp: 2024-10-08T19:06:06.583Z
Learning: The `encryptcookie_test.go` file contains unit tests that validate key lengths for both `EncryptCookie` and `DecryptCookie` functions, ensuring that invalid key lengths raise appropriate errors.
Applied to files:
ctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Parallel tests for `Session.Save` already exist in the `middleware/session/session_test.go` file, specifically in the `Test_Session_Save` and `Test_Session_Save_Expiration` functions.
Applied to files:
ctx_test.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). (1)
- GitHub Check: Compare
🔇 Additional comments (9)
docs/api/ctx.md (1)
447-447: Signature addition looks goodThe addition of AcceptsLanguagesExtended(offers ...string) string aligns with the new extended filtering behavior documented elsewhere in this page.
res_interface_gen.go (1)
47-47: AutoFormat comment update is correct and consistentThe additional content types (application/vnd.msgpack, application/cbor) match the implementation and tests for AutoFormat.
req_interface_gen.go (1)
19-24: Interface additions and RFC references are accurate
- AcceptsLanguages explicitly documents RFC 4647 Basic Filtering.
- AcceptsLanguagesExtended documents RFC 4647 Extended Filtering.
These match the implementation and doc examples.req.go (2)
52-57: Basic Filtering implementation swap is correctSwitching to acceptsLanguageOfferBasic enforces RFC 4647 Basic Filtering. Header extraction via joinHeaderValues remains consistent with other Accepts* methods.
59-64: Extended Filtering method is implemented as expectedAcceptsLanguagesExtended delegates to acceptsLanguageOfferExtended, enabling wildcard subtags in extended ranges, and aligns with the new tests and docs.
ctx_test.go (1)
318-323: Good coverage for invalid and wildcard ranges under Basic Filtering
- “en_US” is correctly treated as invalid.
- “en-*” correctly returns no match under Basic Filtering.
ctx_interface_gen.go (2)
162-167: Interface: Basic vs Extended filtering separation is clearAdding AcceptsLanguagesExtended and clarifying AcceptsLanguages as Basic Filtering aligns with RFC 4647 and keeps API intent explicit. LGTM.
327-327: AutoFormat docs: content types list updatedUpdated comment to include application/vnd.msgpack and application/cbor is accurate and consistent with Res docs. LGTM.
helpers.go (1)
197-216: Basic Filtering logic is correct and stricter on invalid wildcards
- Sole "" accepted; any other occurrence of "" rejected.
- Prefix match enforced at subtag boundary via hyphen.
- Case-insensitive checks. This matches RFC 4647 Basic Filtering.
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (3)
helpers.go (1)
207-209: Nit: micro-optimization for asterisk detectionAfter the explicit check for spec == "*", use IndexByte for a tiny perf win and to avoid allocating in Contains.
- if strings.Contains(spec, "*") { // only "*" is allowed in Basic + if strings.IndexByte(spec, '*') != -1 { // only "*" is allowed in Basic return false }docs/api/ctx.md (1)
525-527: Provide a concrete example for Extended Filtering“depends on extended ranges in the request header” is vague. Show an actual header and expected result to make usage obvious.
- c.AcceptsLanguagesExtended("en-US", "fr-CA") - // depends on extended ranges in the request header + // Accept-Language: en-*, fr-CA + c.AcceptsLanguagesExtended("en-US", "de-DE") // "en-US"ctx_test.go (1)
335-349: Extended filtering tests are correct; consider table-driven expansionCurrent cases validate wildcard semantics and the “exactly one subtag” rule. To scale, refactor to a table-driven test and add a few canonical RFC scenarios:
- "de-CH" matches "de-CH-1996"
- "de-*-DE" matches "de-Latn-DE", not "de-DE"
- "*-US" matches "en-US", "es-US" if offered
- "en" matches "en-US" (range shorter than tag is allowed)
Example additional cases:
t.Run("extended_examples", func(t *testing.T) { app := New() c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) cases := []struct{ header string offers []string want string }{ {"de-CH", []string{"de-CH-1996", "de"}, "de-CH-1996"}, {"de-*-DE", []string{"de-Latn-DE", "de-DE"}, "de-Latn-DE"}, {"*-US", []string{"en-US", "es-US"}, "en-US"}, {"en", []string{"en-US"}, "en-US"}, } for _, tc := range cases { c.Request().Header.Set(HeaderAcceptLanguage, tc.header) require.Equal(t, tc.want, c.AcceptsLanguagesExtended(tc.offers...)) } })
🧹 Nitpick comments (4)
helpers.go (2)
200-202: Clarify comment: any non-standalone “*” is invalid in BasicCurrent wording mentions “after a hyphen”; Basic filtering actually disallows “*” anywhere unless it's the entire range.
-// followed by a hyphen. The comparison is case-insensitive. Only a single "*" -// as the entire range is allowed. Any "*" appearing after a hyphen renders the -// range invalid and will not match. +// followed by a hyphen (case-insensitive). Only a single "*" as the entire +// range is allowed; any occurrence of "*" in any other position renders the +// range invalid and will not match.
222-240: Extended filtering correctly allows wildcard subtags and extra trailing subtags
- The “at least as many subtags as the range” guard (len(rs) > len(ts) ⇒ false) is correct.
- Wildcard matches exactly one subtag; additional subtags in the offer are allowed, which aligns with RFC 4647 Extended Filtering (e.g., "en-*" matches "en-US-CA").
Consider adding tests that demonstrate:
- "de-CH" matches "de-CH-1996" (extra subtag allowed).
- "de-*-DE" matches "de-Latn-DE" but not "de-DE" (wildcard must match exactly one subtag).
Would you like me to open a follow-up PR adding these tests to helpers/ctx tests?
req.go (1)
59-64: Extended filtering API addition looks good; consider de-dup of header readThe new
AcceptsLanguagesExtendedmaps cleanly to the extended matcher and mirrors the Basic variant. Implementation is solid.To remove duplication and centralize future changes, consider a small helper:
// outside selected lines, for illustration only func (r *DefaultReq) acceptsLang(offerFn func(string, string, headerParams) bool, offers ...string) string { header := joinHeaderValues(r.Request().Header.PeekAll(HeaderAcceptLanguage)) return getOffer(header, offerFn, offers...) } func (r *DefaultReq) AcceptsLanguages(offers ...string) string { return r.acceptsLang(acceptsLanguageOfferBasic, offers...) } func (r *DefaultReq) AcceptsLanguagesExtended(offers ...string) string { return r.acceptsLang(acceptsLanguageOfferExtended, offers...) }helpers_test.go (1)
66-79: Good coverage split between Basic and Extended; add a few edge casesThe tests clearly differentiate RFC 4647 Basic and Extended behavior. Consider adding a couple of small cases to harden coverage:
- Basic: reject other wildcard-subtag forms like "*-US".
- Extended: reject underscore separators.
- Extended: verify q-weight ordering when a wildcard competes with a specific tag.
You can append assertions like:
// Accept-Language Basic Filtering require.True(t, acceptsLanguageOfferBasic("en", "en-US", nil)) require.False(t, acceptsLanguageOfferBasic("en-US", "en", nil)) require.True(t, acceptsLanguageOfferBasic("EN", "en-us", nil)) require.False(t, acceptsLanguageOfferBasic("en", "en_US", nil)) require.Equal(t, "en-US", getOffer([]byte("fr-CA;q=0.8, en-US"), acceptsLanguageOfferBasic, "en-US", "fr-CA")) require.Equal(t, "", getOffer([]byte("xx"), acceptsLanguageOfferBasic, "en")) require.False(t, acceptsLanguageOfferBasic("en-*", "en-US", nil)) + require.False(t, acceptsLanguageOfferBasic("*-US", "en-US", nil)) // Accept-Language Extended Filtering require.True(t, acceptsLanguageOfferExtended("en-*", "en-US", nil)) require.True(t, acceptsLanguageOfferExtended("*-US", "en-US", nil)) require.False(t, acceptsLanguageOfferExtended("en-US-*", "en-US", nil)) require.Equal(t, "en-US", getOffer([]byte("fr-CA;q=0.8, en-*"), acceptsLanguageOfferExtended, "en-US", "fr-CA")) + require.False(t, acceptsLanguageOfferExtended("en_*", "en-US", nil)) + require.Equal(t, "fr-CA", getOffer([]byte("en-*;q=0.5, fr-CA;q=0.9"), acceptsLanguageOfferExtended, "en-US", "fr-CA"))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
ctx_interface_gen.go(2 hunks)ctx_test.go(2 hunks)docs/api/ctx.md(2 hunks)helpers.go(1 hunks)helpers_test.go(1 hunks)req.go(1 hunks)req_interface_gen.go(1 hunks)res_interface_gen.go(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
docs/**
📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)
Review and update the contents of the
docsfolder if necessary when modifying code
Files:
docs/api/ctx.md
🧠 Learnings (13)
📓 Common learnings
Learnt from: ReneWerner87
PR: gofiber/fiber#0
File: :0-0
Timestamp: 2024-12-01T10:28:36.011Z
Learning: Feature request #3224 has been created to add support for square bracket notation and comma-separated values in multipart form data in Fiber, while maintaining binary data transfer capabilities. This would bring parity with the existing form-urlencoded functionality.
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
Applied to files:
docs/api/ctx.mdctx_interface_gen.go
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Applied to files:
docs/api/ctx.mdctx_interface_gen.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/csrf/csrf_test.go:188-193
Timestamp: 2024-10-08T19:06:06.583Z
Learning: In the Fiber framework tests, using `ctx.Response.Header.Cookie` may not be suitable for parsing cookies from the response header, as it requires a `*Cookie` and fills it rather than returning a string value; thus, manual parsing of the `Set-Cookie` header may be necessary.
Applied to files:
docs/api/ctx.mdctx_test.go
📚 Learning: 2024-11-15T07:56:21.623Z
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
Applied to files:
docs/api/ctx.mdhelpers_test.go
📚 Learning: 2024-12-13T08:14:22.851Z
Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the `require` methods from the `testify` package for assertions instead of manual comparisons and calls to `t.Fatal` or `t.Fatalf`.
Applied to files:
helpers_test.goctx_test.go
📚 Learning: 2024-07-26T21:00:12.902Z
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Applied to files:
helpers_test.goctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests in this project use testify require.
Applied to files:
helpers_test.go
📚 Learning: 2024-10-16T12:12:30.506Z
Learnt from: gaby
PR: gofiber/fiber#3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
Applied to files:
helpers.goctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:22-25
Timestamp: 2024-10-08T19:06:06.583Z
Learning: The `encryptcookie_test.go` file contains unit tests that validate key lengths for both `EncryptCookie` and `DecryptCookie` functions, ensuring that invalid key lengths raise appropriate errors.
Applied to files:
ctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Parallel tests for `Session.Save` already exist in the `middleware/session/session_test.go` file, specifically in the `Test_Session_Save` and `Test_Session_Save_Expiration` functions.
Applied to files:
ctx_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.
Applied to files:
ctx_test.go
📚 Learning: 2024-11-29T12:37:27.581Z
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the `Test_App_ShutdownWithContext` function in `app_test.go`, the `clientDone` channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional `time.Sleep` calls.
Applied to files:
ctx_test.go
🧬 Code Graph Analysis (1)
req.go (1)
constants.go (1)
HeaderAcceptLanguage(193-193)
🔇 Additional comments (7)
helpers.go (1)
197-216: Basic filtering logic is correct; rejects invalid post-hyphen wildcards as intended
- Exact and prefix-with-hyphen matching aligns with RFC 4647 Basic Filtering.
- Rejecting any "" other than a bare "" is correct for Accept-Language.
docs/api/ctx.md (1)
447-448: API surface documentation addition looks goodAdding AcceptsLanguagesExtended alongside AcceptsLanguages clearly distinguishes Basic vs Extended filtering.
res_interface_gen.go (1)
47-47: AutoFormat supported content types comment updated consistentlyThe additions of application/vnd.msgpack and application/cbor match the behavior elsewhere. Good consistency.
req_interface_gen.go (1)
19-24: Interfaces clearly separate Basic vs Extended filteringCommenting the RFC variant used by each method removes ambiguity. LGTM.
ctx_test.go (1)
318-323: Basic filtering: invalid wildcard range rejected — good coverageThe test correctly asserts that "en-*" does not match under Basic filtering.
ctx_interface_gen.go (2)
162-167: AcceptsLanguagesExtended Generation VerifiedVerified that
AcceptsLanguagesExtendedis present across the generated interfaces and implementation without any manual edits:
- req_interface_gen.go (line 24)
- ctx_interface_gen.go (line 167)
- Implementation in DefaultReq (req.go:61)
- Covered by Test_Ctx_AcceptsLanguagesExtended in ctx_test.go
- Documented in docs/api/ctx.md
No further action needed; generator output is consistent.
325-328: No changes required: AutoFormat already supports MsgPack & CBOR
Implementation inres.go(around line 367) callsaccept := r.c.Accepts("html", "json", "txt", "xml", "msgpack", "cbor") … switch accept { case "msgpack": return r.MsgPack(body) case "cbor": return r.CBOR(body) … }Tests in
ctx_test.gocover bothAutoFormat→MsgPack and →CBOR paths. The documentation matches the implementation.
|
/gemini summary |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
ctx_test.go(2 hunks)helpers.go(1 hunks)helpers_test.go(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- helpers_test.go
- ctx_test.go
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: ReneWerner87
PR: gofiber/fiber#0
File: :0-0
Timestamp: 2024-12-01T10:28:36.011Z
Learning: Feature request #3224 has been created to add support for square bracket notation and comma-separated values in multipart form data in Fiber, while maintaining binary data transfer capabilities. This would bring parity with the existing form-urlencoded functionality.
📚 Learning: 2024-10-16T12:12:30.506Z
Learnt from: gaby
PR: gofiber/fiber#3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
Applied to files:
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: lint
- GitHub Check: Analyse
- GitHub Check: Compare
- GitHub Check: unit (1.24.x, windows-latest)
- GitHub Check: unit (1.24.x, macos-latest)
- GitHub Check: repeated
🔇 Additional comments (1)
helpers.go (1)
197-216: Basic Filtering logic looks correct and RFC 4647-compliant
- Only full-range "" is allowed; any other '' makes the range invalid.
- Case-insensitive exact or prefix-with-hyphen match.
LGTM.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors language negotiation to be more compliant with RFC 4647. It correctly separates Basic Filtering into AcceptsLanguages and introduces AcceptsLanguagesExtended for Extended Filtering. The changes are well-tested and documented. I have one suggestion to improve performance in the new acceptsLanguageOfferExtended function.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request does a great job of improving RFC compliance for language tag matching by implementing Basic and Extended Filtering from RFC 4647. The introduction of AcceptsLanguagesExtended is a valuable addition, and the test coverage for both filtering methods is thorough. The documentation has also been updated clearly. I have one suggestion regarding performance in the new acceptsLanguageOfferExtended function to avoid memory allocations on a potentially hot path.
done |
Summary
AcceptsLanguages()by only allowingBasic Filteringas defined inRFC 4647AcceptsLanguagesExtended()Related #3383