Skip to content

🧹 chore: Fix proxy middleware tests for offline environments #3467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 22, 2025

Conversation

gaby
Copy link
Member

@gaby gaby commented May 22, 2025

Summary

  • adjust proxy tests to avoid external network dependencies
  • add helper createRedirectServer used by updated tests

This change will help running the tests with agents like codex which disable internet access after environment is setup.

Testing

  • go test ./middleware/proxy -run Test_Proxy_Do_WithRealURL -v
  • go test ./middleware/proxy -run Test_Proxy_Do_WithRedirect -v
  • go test ./middleware/proxy -run Test_Proxy_DoRedirects_RestoreOriginalURL -v
  • go test ./middleware/proxy -run Test_Proxy_DoRedirects_TooManyRedirects -v
  • go test ./...

Copy link
Contributor

coderabbitai bot commented May 22, 2025

Walkthrough

A new helper function was introduced to create a local HTTP redirect server for testing. Several proxy-related tests were updated to use this local server instead of external URLs, ensuring that tests are self-contained and do not depend on external network resources. Test assertions were adjusted to reflect the local server responses.

Changes

File(s) Change Summary
middleware/proxy/proxy_test.go Added createRedirectServer helper. Updated multiple tests to use local test servers instead of external URLs. Adjusted assertions to match local server responses.

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

A server at home, no need to roam,
Redirects and proxies now test on their own.
No more googling for a test to pass,
Local responses—fast as grass!
🐇 Cheers to code that’s neat and tight,
Testing local feels just right!


📜 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 bd9c02b and 60a122a.

📒 Files selected for processing (1)
  • middleware/proxy/proxy_test.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • middleware/proxy/proxy_test.go
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: Analyse
  • GitHub Check: repeated
  • GitHub Check: Compare
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gaby gaby changed the title Fix proxy middleware tests for offline environments 🧹 chore: Fix proxy middleware tests for offline environments May 22, 2025
@gaby gaby marked this pull request as ready for review May 22, 2025 02:23
@gaby gaby requested a review from a team as a code owner May 22, 2025 02:23
Copy link

codecov bot commented May 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.45%. Comparing base (81edaf0) to head (5e9eb5b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3467      +/-   ##
==========================================
+ Coverage   84.36%   84.45%   +0.09%     
==========================================
  Files         120      120              
  Lines       12157    12157              
==========================================
+ Hits        10256    10267      +11     
+ Misses       1470     1462       -8     
+ Partials      431      428       -3     
Flag Coverage Δ
unittests 84.45% <ø> (+0.09%) ⬆️

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
Contributor

@Copilot 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 PR refactors proxy middleware tests to remove reliance on external network calls by spinning up local HTTP servers for redirect and real URL scenarios.

  • Added createRedirectServer helper to simulate HTTP redirects in tests
  • Updated Test_Proxy_Do_* and Test_Proxy_DoRedirects_* to use local servers and assert against controlled responses
Comments suppressed due to low confidence (1)

middleware/proxy/proxy_test.go:62

  • [nitpick] The variable name 'addr' is quite generic; consider renaming to something like 'redirectServerAddr' to make its purpose clearer.
var addr string

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 273df5f and 23dd777.

📒 Files selected for processing (1)
  • middleware/proxy/proxy_test.go (4 hunks)
🧰 Additional context used
🪛 GitHub Check: lint
middleware/proxy/proxy_test.go

[failure] 58-58:
createRedirectServer - result 0 (*github.com/gofiber/fiber/v3.App) is never used (unparam)

🪛 GitHub Actions: golangci-lint
middleware/proxy/proxy_test.go

[error] 58-58: golangci-lint: createRedirectServer - result 0 (*github.com/gofiber/fiber/v3.App) is never used (unparam)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: unit (1.23.x, macos-13)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
🔇 Additional comments (11)
middleware/proxy/proxy_test.go (11)

527-529: Good replacement of external URL dependency!

The test now uses a local server created with createProxyTestServerIPv4 instead of an external URL, making the test more predictable and reliable.


533-533: Good update to use local test server URL!

This change ensures the test uses the local server address rather than an external URL.


545-545: Appropriate assertion update!

The expected response has been updated to match the local server's response ("real url").


552-552: Good implementation of local redirect server!

The test now uses the new createRedirectServer function instead of relying on external redirect behavior.


556-556: Proper use of local server address!

This correctly uses the address returned by createRedirectServer.


566-567: Correct assertions for local redirect server!

The expected response body ("redirect") and status code (301) now match what the local redirect server returns.


574-574: Good reuse of redirect server in another test!

Using the same redirect server helper function across multiple tests ensures consistency.


578-578: Appropriate use of DoRedirects with local server!

The test now properly tests the redirect functionality with a controlled local environment.


586-588: Proper assertions for redirect follow-through!

These assertions correctly verify that the DoRedirects function follows the redirect and receives the "final" response.


597-597: Consistent use of redirect server!

Using the same helper function across all redirect-related tests ensures testing consistency.


601-601: Good test for too many redirects!

Setting max redirects to 0 is a good way to test the too many redirects error handling.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
middleware/proxy/proxy_test.go (1)

58-79: Well-designed helper function for offline testing

The new createRedirectServer function is well-implemented, creating a local HTTP server that handles redirects without requiring external network access. This approach makes the tests more reliable and consistent.

There's a good use of t.Helper() to mark this as a test helper function, and the cleanup function properly closes the listener when the test completes.

One minor issue is that the error from ln.Close() is not being checked, as flagged by the linter. Apply this fix:

-	t.Cleanup(func() { ln.Close() })
+	t.Cleanup(func() { 
+		err := ln.Close()
+		if err != nil {
+			t.Logf("failed to close listener: %v", err)
+		}
+	})
🧰 Tools
🪛 GitHub Check: lint

[failure] 73-73:
Error return value of ln.Close is not checked (errcheck)

🪛 GitHub Actions: golangci-lint

[error] 73-73: golangci-lint: Error return value of ln.Close is not checked (errcheck)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9b3f6ad and bd9c02b.

📒 Files selected for processing (1)
  • middleware/proxy/proxy_test.go (4 hunks)
🧰 Additional context used
🪛 GitHub Check: lint
middleware/proxy/proxy_test.go

[failure] 73-73:
Error return value of ln.Close is not checked (errcheck)

🪛 GitHub Actions: golangci-lint
middleware/proxy/proxy_test.go

[error] 73-73: golangci-lint: Error return value of ln.Close is not checked (errcheck)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: repeated
🔇 Additional comments (11)
middleware/proxy/proxy_test.go (11)

527-531: Good replacement of external URL dependency

Replacing the external URL with a local test server is a great improvement. This change eliminates network dependencies and makes the test more reliable and faster.


534-535: Properly updated the test case with local server URL

The test logic is maintained while replacing the external dependency with a local server URL.


546-546: Appropriate assertion update for local server response

The assertion has been correctly updated to match the response from the local server.


553-553: Good use of the new redirect server helper

The test now uses the newly created createRedirectServer function, eliminating the dependency on external network resources.


556-557: Properly updated test logic for local redirect server

The test logic has been maintained while using the local redirect server URL.


566-567: Appropriate assertions for redirect behavior

The assertions correctly verify the redirect response and status code from the local server.


574-574: Good application of redirect server for testing complex scenarios

Using the local redirect server for testing multi-redirect scenarios is an excellent approach.


577-578: Well-structured test for redirect following

The test properly sets up the redirect limit parameter (1) to test that the proxy follows redirects as expected.


585-587: Appropriate assertions for final redirect destination

The assertions correctly verify that the final response after following redirects is as expected.


596-596: Good use of redirect server for error testing

Using the local redirect server to test error conditions is a smart approach.


599-600: Well-crafted test for redirect limits

Setting the redirect limit to 0 is an excellent way to test the "too many redirects" error condition.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 60a122a Previous: 81edaf0 Ratio
Benchmark_Compress_Levels_Parallel/Brotli_LevelBestCompression - B/op 1 B/op 0 B/op +∞

This comment was automatically generated by workflow using github-action-benchmark.

@ReneWerner87 ReneWerner87 added this to v3 May 22, 2025
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 22, 2025
@gaby gaby moved this to In Progress in v3 May 22, 2025
@ReneWerner87 ReneWerner87 merged commit 18ed68b into main May 22, 2025
18 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 May 22, 2025
@gaby gaby deleted the codex/check-and-fix-bugs-in-middlewares-directory branch June 18, 2025 02:08
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.

2 participants