[test-improver] Improve tests for tracing package#6716
Merged
Conversation
Add missing coverage for: - parseOTLPHeadersWithDecoder: test invalid percent-encoding error path (url.PathUnescape failure → raw value preserved) - WrapHTTPHandler: test route cleared when pattern method mismatches request method (exercises else-branch setting route = "") - Additional tests for explicit decode/no-decode behaviour in parseOTLPHeadersWithDecoder Coverage: 96.4% → 97.6% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves unit test coverage in internal/tracing by adding targeted tests for previously uncovered defensive branches in OTLP header parsing and HTTP handler route extraction.
Changes:
- Added coverage for
parseOTLPHeadersWithDecoderpercent-decoding behavior (valid decode, invalid decode fallback, and no-decoding mode). - Added coverage for the
WrapHTTPHandlerbranch wherer.Patterncontains a mismatched HTTP method prefix (forcingroute = "").
Show a summary per file
| File | Description |
|---|---|
| internal/tracing/parse_headers_test.go | Adds tests for percent-decoding success/failure and decode toggle behavior in OTLP header parsing. |
| internal/tracing/http_test.go | Adds a new test to hit the defensive route = "" branch when the request pattern’s method mismatches the request method. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 6
Comment on lines
+158
to
+161
| result := parseOTLPHeadersWithDecoder("X-Token=Bearer%GZvalue,X-Other=ok", true) | ||
| // The raw (un-decoded) value must be used when decoding fails. | ||
| assert.Equal(t, "Bearer%GZvalue", result["X-Token"], "raw value should be preserved on decode failure") | ||
| assert.Equal(t, "ok", result["X-Other"], "valid pairs must still be decoded correctly") |
Comment on lines
+66
to
+70
| // TestWrapHTTPHandler_PatternMethodMismatch_ClearsRoute verifies that when a request | ||
| // pattern contains a different HTTP method than the actual request method, the | ||
| // http.route attribute is not added to the span. | ||
| // This exercises the `route = ""` branch in WrapHTTPHandler. | ||
| func TestWrapHTTPHandler_PatternMethodMismatch_ClearsRoute(t *testing.T) { |
Comment on lines
+71
to
+73
| // Build a request whose Pattern method differs from its actual Method. | ||
| // In normal mux routing this cannot happen, but direct manipulation lets us | ||
| // verify that WrapHTTPHandler handles it gracefully without setting http.route. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Collaborator
|
@copilot address review feedback |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements: tracing package
Files Analyzed
internal/tracing/parse_headers_test.go,internal/tracing/http_test.gointernal/tracingImprovements Made
1. Increased Coverage
✅ Added
TestParseOTLPHeadersWithDecoder_InvalidPercentEncoding— exercises theurl.PathUnescapeerror branch inparseOTLPHeadersWithDecoder(previously uncovered). Verifies the raw value is preserved when percent-decoding fails (e.g.%GZis not valid percent-encoding).✅ Added
TestParseOTLPHeadersWithDecoder_ValidPercentEncoding— documents and verifies that well-formed percent-encoded values are decoded whendecodeValues=true.✅ Added
TestParseOTLPHeadersWithDecoder_NoDecoding— documents and verifies that percent-encoded values are preserved as-is whendecodeValues=false.✅ Added
TestWrapHTTPHandler_PatternMethodMismatch_ClearsRoute— exercises theelse { route = "" }branch inWrapHTTPHandler(previously uncovered). This path is triggered whenr.Patterncontains a method prefix that differs from the actual request method.Previous Coverage: 96.4%
New Coverage: 97.6%
Improvement: +1.2%
2. Better Testing Patterns
Test<Func>_<Scenario>conventionTest Execution
All tests pass:
All other Go tests continue to pass (
make test-all).Why These Changes?
The
internal/tracingpackage had two meaningful uncovered branches:parseOTLPHeadersWithDecoder— Theurl.PathUnescapeerror path was untested. This is a real defensive code path that protects against invalid percent-encoded OTLP header values arriving via theOTEL_EXPORTER_OTLP_HEADERSenvironment variable.WrapHTTPHandler— Theroute = ""else-branch when the pattern's HTTP method doesn't match the request method was untested. While this rarely occurs in normal mux routing, it is a defensive code path worth covering.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
index.crates.ioSee Network Configuration for more information.