[test] Add tests for oidc.fetchToken#7986
Merged
Merged
Conversation
Cover the two previously-untested error paths in fetchToken: - nil context path: http.NewRequestWithContext returns an error when passed a nil context (TestProvider_NilContext) - body-read error path: io.ReadAll fails when the server closes the TCP connection after sending headers but before the body (TestProvider_BodyReadError, uses HTTP Hijacker) fetchToken coverage: 93.9% → 100.0% oidc package total: 97.1% → 100.0% 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 for internal/oidc.Provider.fetchToken, targeting previously uncovered error branches so the internal/oidc package reaches 100% coverage.
Changes:
- Add a test that passes a
nilcontext to exercise thehttp.NewRequestWithContexterror path. - Add a test that hijacks the HTTP connection and closes it after sending headers to force an
io.ReadAllbody-read failure.
Show a summary per file
| File | Description |
|---|---|
| internal/oidc/provider_test.go | Adds two new unit tests covering fetchToken error paths (nil context request creation failure; response body read failure). |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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 Coverage Improvement:
oidc.fetchTokenFunction Analyzed
internal/oidcfetchToken(method on*Provider)Why This Function?
fetchTokenwas the most complex under-tested function in the codebase. It contains 8 distinct error paths covering URL parsing, HTTP request construction, network I/O, response body reading, JSON parsing, and JWT expiry extraction. Two branches were previously uncovered:http.NewRequestWithContexterror path — triggered whenfetchTokenreceives anilcontext, which causesnet/httpto return"net/http: nil Context".io.ReadAllbody-read error path — triggered when the server sends valid HTTP 200 headers but then closes the TCP connection before delivering the body. The HTTP client returns a successfulDo()(status 200 is received) but the subsequentio.ReadAll(resp.Body)fails withio.ErrUnexpectedEOFbecauseContent-Lengthdeclared more bytes than were actually sent.Tests Added
TestProvider_NilContext— passesnilas the context toprovider.Token(), exercises the"failed to create OIDC token request"error return infetchTokenTestProvider_BodyReadError— useshttp.Hijackerto send a 200 response withContent-Length: 1000then immediately close the connection, exercising the"failed to read OIDC token response"error returnCoverage Report
Test Execution
Generated by Test Coverage Improver
Next run will target the next most complex under-tested function
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgSee Network Configuration for more information.