Skip to content
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

🔥 Feature: Add TestConfig to app.Test() for configurable testing #3161

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

grivera64
Copy link
Contributor

@grivera64 grivera64 commented Oct 9, 2024

Description

This pull request modifies app.Test() in v3 to use a TestConfig{} struct to configure internal testing of app routes. This is due to the introduction of c.SendStreamWriter() in PR #3131, we need support for reading the response of a route after a certain timeout to mimic a client disconnection. It is important to keep this in account when streaming a response to the client, since you may need to do cleanup or rollback after a closed connection.

Fixes #3149

Changes introduced

  • Documentation Update: Update old documentation of app.Test() in /docs/api/app.md
  • Changelog/What's New: Add TestConfig struct to app.Test() for configurable testing
  • Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
    • May be a good idea to include this change in the migration guide.
  • Examples:
app := fiber.New()
app.Get("/", func(c Ctx) error {
  return c.Status(fiber.StatusOK).SendString("Hello World!")
}
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), TestConfig{
  Timeout:       0, // No timeout
  FailOnTimeout: false,
})

Type of change

  • New feature (non-breaking change which adds functionality)
    • Adds the preservation of a timed-out response
    • Adds thread-safety to testConn
  • Enhancement (improvement to existing features and functionality)
    • Add a TestConfig{} struct to perform the same functionality as before
    • Add unit tests for testConn
  • Documentation update (changes to documentation)
    • Adds details about using the new version of the app.Test() method

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

This commit is summarized as:
- Add the struct `TestConfig` as a parameter for `app.Test()` instead of `timeout`
- Add documentation of `TestConfig` to docs/api/app.md and in-line
- Modify middleware to use `TestConfig` instead of the previous implementation

Fixes gofiber#3149
- Fixes Test_Utils_TestConn_Closed_Write
- Fixes missing regular write test
@grivera64 grivera64 requested a review from a team as a code owner October 9, 2024 19:03
@grivera64 grivera64 requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team October 9, 2024 19:03
Copy link
Contributor

coderabbitai bot commented Oct 9, 2024

Walkthrough

The changes introduce a new TestConfig struct to the fiber package, enhancing the app.Test method by allowing it to accept configuration settings for testing, including timeout duration and error handling for timeouts. This modification is reflected across various test files, where existing timeout parameters are replaced with instances of TestConfig. Additionally, several new tests and benchmarks have been added to improve coverage and performance measurement, particularly for context methods and connection handling.

Changes

File Change Summary
app.go Introduced TestConfig struct; modified Test method to accept TestConfig instead of timeout.
app_test.go Updated Test_App_Test to use TestConfig; added Test_App_Test_timeout_empty_response test case.
ctx_test.go Added benchmark tests and new test cases for context methods; enhanced error handling tests.
docs/api/app.md Updated Test method documentation to reflect changes to TestConfig.
helpers.go Modified testConn struct for thread safety; added locking mechanisms to Read, Write, and Close.
helpers_test.go Added tests for testConn operations; included benchmarks for new test functions.
middleware/compress/compress_test.go Updated tests to use TestConfig for timeout handling.
middleware/idempotency/idempotency_test.go Updated doReq function to use TestConfig in Test_Idempotency.
middleware/keyauth/keyauth_test.go Updated tests to utilize TestConfig for timeout settings.
middleware/logger/logger_test.go Updated logger tests to use TestConfig for request testing.
middleware/pprof/pprof_test.go Updated tests to utilize TestConfig for timeout settings.
middleware/proxy/proxy_test.go Updated tests to use TestConfig for request testing.
middleware/static/static_test.go Updated static file tests to use TestConfig for timeout and error handling.

Assessment against linked issues

Objective Addressed Explanation
Add TestConfig struct as an optional parameter to app.Test() for testing.
Allow app.Test() to specify error handling behavior for timeouts.
Ensure that existing functionality of app.Test() remains unchanged.

Possibly related PRs

  • 📚 Doc: Clarify SendFile Docs #3172: The changes in this PR enhance the documentation for the SendFile function, which is directly related to the modifications made in the main PR regarding the Test_App_Test function in app_test.go, as both involve handling timeout configurations and response behaviors.
  • 📚 Doc: Updates to API documentation and README #3205: The updates to the API documentation and README include clarifications on the Test method, which is relevant to the changes made in the main PR that also involve the Test method's signature and its usage in tests.
  • 📚 Doc: Updates to Context documentation #3206: The updates to the Context documentation include changes to method signatures and descriptions that are relevant to the modifications made in the main PR, particularly regarding the handling of context in tests and the introduction of new test cases.

Suggested labels

🧹 Updates

Suggested reviewers

  • sixcolors
  • gaby
  • efectn
  • ReneWerner87

Poem

🐰 In the meadow where the code does play,
A new TestConfig hops in today!
With timeouts set and errors in sight,
Testing is clearer, everything feels right.
So let’s cheer for the changes, oh what a delight! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.85%. Comparing base (b6ecd63) to head (b6daa78).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3161      +/-   ##
==========================================
+ Coverage   82.70%   82.85%   +0.15%     
==========================================
  Files         114      114              
  Lines       11163    11189      +26     
==========================================
+ Hits         9232     9271      +39     
+ Misses       1529     1520       -9     
+ Partials      402      398       -4     
Flag Coverage Δ
unittests 82.85% <100.00%> (+0.15%) ⬆️

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.


🚨 Try these New Features:

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: 3

🧹 Outside diff range and nitpick comments (13)
middleware/idempotency/idempotency_test.go (1)

85-88: Excellent update to use fiber.TestConfig!

This change aligns perfectly with the PR objectives by introducing the TestConfig struct for configurable testing. The update maintains the existing 15-second timeout while adding the ErrOnTimeout: true setting. This enhancement will improve error handling during tests, especially for scenarios involving streaming or long-running operations.

Consider adding a test case that specifically verifies the timeout behavior with this new configuration. This would ensure that the ErrOnTimeout setting is working as expected and would provide coverage for timeout scenarios.

middleware/compress/compress_test.go (1)

Line range hint 42-192: Consider creating a helper function for TestConfig

The changes consistently apply fiber.TestConfig across all test functions, which is excellent. To further improve code maintainability and reduce duplication, consider creating a helper function that returns the common TestConfig. This would centralize the configuration and make future changes easier.

Example:

func getTestConfig() fiber.TestConfig {
    return fiber.TestConfig{
        Timeout:      10 * time.Second,
        ErrOnTimeout: true,
    }
}

Then, in each test:

resp, err := app.Test(req, getTestConfig())

This approach would make it easier to modify the test configuration globally if needed in the future.

docs/api/app.md (3)

543-544: LGTM! Consider adding a brief explanation of the TestConfig parameter.

The updated method signature correctly reflects the introduction of the TestConfig struct, which aligns with the PR objectives. This change enhances the testing capabilities of the framework by allowing for more flexible configuration.

Consider adding a brief explanation of the TestConfig parameter immediately after the signature to help users understand its purpose and usage.


569-593: Improve structure and clarity of TestConfig information.

The added information about TestConfig is crucial for users to understand the default behavior and potential pitfalls. However, the structure and presentation of this information could be improved for better clarity and readability.

Consider the following suggestions:

  1. Move the default TestConfig information (lines 569-576) closer to the method signature for immediate context.
  2. Restructure the caution note (lines 578-593) to make it more concise and easier to understand. For example:
:::caution
Be careful when using an empty `TestConfig{}`. It is not equivalent to the default configuration and may result in unexpected behavior:

- Default: `Timeout: 1 second, ErrOnTimeout: true`
- Empty `TestConfig{}`: `Timeout: 0, ErrOnTimeout: false`

An empty `TestConfig{}` will cause the test to time out immediately, always resulting in a "test: empty response" error.
:::
  1. Consider adding a brief example of how to use TestConfig with custom values to illustrate its practical application.

Line range hint 543-594: Enhance integration of new TestConfig information with existing documentation.

While the changes to the Test method documentation are consistent with the PR objectives, there's an opportunity to improve the integration of the new information with the existing content.

Consider the following suggestions:

  1. Update the introductory paragraph (lines 543-545) to mention the new TestConfig option alongside the existing timeout information.

  2. Modify the example (lines 547-568) to demonstrate the use of TestConfig. For instance:

// Example with default TestConfig
resp, _ := app.Test(req)

// Example with custom TestConfig
customConfig := fiber.TestConfig{
    Timeout:      2 * time.Second,
    ErrOnTimeout: false,
}
resp, _ := app.Test(req, customConfig)
  1. Add a brief explanation of when and why a user might want to use custom TestConfig settings, to provide context for this new feature.

These changes will help users understand the new TestConfig feature in the context of the existing Test method functionality.

🧰 Tools
🪛 LanguageTool

[grammar] ~597-~597: Did you mean “are” or “were”?
Context: ... response" error. ::: ## Hooks Hooks is a method to return hooks ...

(SENT_START_NNS_IS)

helpers_test.go (3)

517-538: LGTM! Consider adding error case tests.

The Test_Utils_TestConn_ReadWrite function effectively tests both read and write operations on the testConn structure. It verifies data integrity in both directions, which is crucial for ensuring the correct behavior of the connection.

Consider adding test cases for error scenarios, such as reading or writing with a closed connection, to ensure robust error handling.


540-557: LGTM! Consider handling Close() error and adding read test.

The Test_Utils_TestConn_Closed_Write function effectively tests the behavior of writing to a closed connection. It verifies that writing after closing fails and that only data from successful writes is present.

  1. Consider handling the error from conn.Close() instead of ignoring it with a linter directive. This would make the test more robust.
  2. Add a test case for reading from a closed connection to ensure comprehensive coverage of closed connection behavior.

Example improvement for point 1:

-	conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here
+	err = conn.Close()
+	require.NoError(t, err, "Failed to close connection")

517-557: Overall, the new test functions enhance the test coverage.

The addition of Test_Utils_TestConn_ReadWrite and Test_Utils_TestConn_Closed_Write significantly improves the test coverage for the testConn structure. These tests effectively verify the behavior of read and write operations, including edge cases like writing to a closed connection.

To further improve the robustness of these tests, consider:

  1. Adding error case scenarios for both read and write operations.
  2. Ensuring all errors are properly handled and checked, including those from Close() operations.
  3. Adding a test for reading from a closed connection to complement the existing write test.

These additions would provide a more comprehensive test suite for the testConn structure, ensuring its reliability across various scenarios.

helpers.go (3)

629-637: LGTM! Thread-safe Write method with closed state check.

The Write method has been improved with two key changes:

  1. It now uses a mutex lock to ensure thread-safety when writing to the buffer.
  2. It checks the isClosed state before writing, preventing writes to a closed connection.

These changes enhance the reliability and correctness of the Write operation.

Consider using a custom error type or a package-level error variable for the "testConn is closed" error. This would allow for easier error checking by consumers of this method. For example:

var ErrConnClosed = errors.New("testConn is closed")

// In the Write method
if c.isClosed {
    return 0, ErrConnClosed
}

639-645: LGTM! Thread-safe Close method implementation.

The Close method has been updated with important improvements:

  1. It now uses a mutex lock to ensure thread-safety when closing the connection.
  2. It sets the isClosed state to true, preventing further operations on the closed connection.

These changes enhance the reliability and correctness of the Close operation.

Consider adding a check to prevent multiple calls to Close from changing the state:

func (c *testConn) Close() error {
    c.Lock()
    defer c.Unlock()

    if !c.isClosed {
        c.isClosed = true
    }
    return nil
}

This ensures that isClosed is set to true only once, which could be useful for debugging or tracking the connection state.


616-645: Overall improvements in thread-safety and connection state management.

The changes made to the testConn struct and its methods (Read, Write, and Close) significantly enhance the reliability and correctness of the test connection implementation:

  1. Thread-safety: The addition of sync.Mutex and its usage in all methods prevents race conditions in concurrent scenarios.
  2. Connection state tracking: The isClosed flag helps manage the connection state, preventing operations on closed connections.
  3. Consistent locking pattern: All methods now use the same locking pattern, ensuring uniform behavior across the struct.

These improvements make the testConn more robust and suitable for use in multi-threaded test environments. The changes align well with best practices for concurrent programming in Go.

Consider adding a Reset() method to the testConn struct. This would allow reusing the same testConn instance across multiple tests, potentially improving performance in test suites with many connection-based tests.

app_test.go (2)

1139-1142: Approved: TestConfig usage enhances timeout testing.

The use of TestConfig here effectively tests the new timeout functionality. Setting ErrOnTimeout: true allows testing of the error-on-timeout feature.

Consider adding a comment explaining the purpose of the 20ms timeout, e.g.:

// Set a short timeout to trigger the timeout scenario quickly
Timeout: 20 * time.Millisecond,

1479-1493: Excellent addition: Test case for timeout with empty response.

This new test function enhances the test coverage by specifically addressing the scenario of a timeout with an empty response. It effectively uses the new TestConfig struct and verifies the correct error message.

Consider adding a brief comment explaining the purpose of this test case, e.g.:

// Test_App_Test_timeout_empty_response verifies that a timeout with an empty response
// returns the expected error message when ErrOnTimeout is false.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c86c3c0 and 6112c04.

📒 Files selected for processing (13)
  • app.go (4 hunks)
  • app_test.go (4 hunks)
  • ctx_test.go (1 hunks)
  • docs/api/app.md (2 hunks)
  • helpers.go (1 hunks)
  • helpers_test.go (1 hunks)
  • middleware/compress/compress_test.go (6 hunks)
  • middleware/idempotency/idempotency_test.go (1 hunks)
  • middleware/keyauth/keyauth_test.go (4 hunks)
  • middleware/logger/logger_test.go (2 hunks)
  • middleware/pprof/pprof_test.go (2 hunks)
  • middleware/proxy/proxy_test.go (11 hunks)
  • middleware/static/static_test.go (5 hunks)
🧰 Additional context used
🪛 GitHub Check: lint
app.go

[failure] 960-960:
fmt.Errorf can be replaced with errors.New (perfsprint)

🔇 Additional comments (35)
middleware/pprof/pprof_test.go (1)

108-111: Improved test configuration with fiber.TestConfig

The changes here align well with the PR objectives. By introducing fiber.TestConfig, you've enhanced the test's flexibility and error handling capabilities. The ErrOnTimeout flag set to true is particularly useful for testing timeout scenarios, ensuring that the test can properly handle and report timed-out responses.

This modification maintains the existing timeout duration while adding more granular control over the test behavior. It's a positive step towards more robust and configurable testing.

middleware/compress/compress_test.go (6)

42-45: LGTM: TestConfig usage enhances test configurability

The change from a simple timeout to fiber.TestConfig aligns with the PR objectives. The new ErrOnTimeout: true setting allows for more precise control over timeout behavior, which is particularly useful for testing streaming responses.


78-81: LGTM: Consistent TestConfig usage

The change to use fiber.TestConfig is consistent with the previous test function and aligns with the PR objectives. This consistency across tests is good for maintainability.


108-111: LGTM: Consistent TestConfig implementation

The change to use fiber.TestConfig maintains consistency with the previous test functions and adheres to the PR objectives. This uniformity across different compression algorithms (here, deflate) is commendable.


135-138: LGTM: TestConfig applied consistently

The implementation of fiber.TestConfig for the Brotli compression test maintains the consistency observed in previous test functions. This uniform approach across different compression algorithms enhances the overall test suite coherence.


162-165: LGTM: TestConfig applied to Zstd compression test

The use of fiber.TestConfig in the Zstd compression test maintains the consistency seen throughout the file. This uniform approach across all compression algorithms ensures a standardized testing methodology.


189-192: LGTM: TestConfig applied to disabled compression scenario

The consistent use of fiber.TestConfig extends to the disabled compression test, maintaining uniformity across all test scenarios. This is particularly important as it ensures that the new configuration doesn't inadvertently affect scenarios where compression is intentionally disabled.

docs/api/app.md (1)

Line range hint 543-594: Overall assessment: Documentation updates align with PR objectives but could be further improved.

The changes introduced in this file successfully document the new TestConfig feature for the Test method, aligning well with the PR objectives. The updated method signature and the addition of TestConfig information enhance the testing capabilities of the Fiber framework.

However, there are opportunities to improve the documentation:

  1. Restructure the presentation of TestConfig information for better clarity.
  2. Integrate the new feature more seamlessly with the existing content.
  3. Provide more examples and context for using custom TestConfig settings.

These improvements would make the new feature more accessible and understandable to users of the Fiber framework.

🧰 Tools
🪛 LanguageTool

[grammar] ~597-~597: Did you mean “are” or “were”?
Context: ... response" error. ::: ## Hooks Hooks is a method to return hooks ...

(SENT_START_NNS_IS)

middleware/keyauth/keyauth_test.go (5)

107-110: LGTM: Updated app.Test call with TestConfig

The change to use fiber.TestConfig in the app.Test method call is consistent with the PR objectives. The configuration maintains the previous behavior:

  • Timeout: -1 indicates no timeout, which is equivalent to the previous implementation.
  • ErrOnTimeout: false ensures that no error is returned on timeout, preserving the existing functionality.

This update enhances the test's configurability while maintaining backward compatibility.


215-218: LGTM: Consistent update to app.Test call

This change is consistent with the previous update to the app.Test method call. It introduces fiber.TestConfig with the same configuration:

  • Timeout: -1 (no timeout)
  • ErrOnTimeout: false (no error on timeout)

The consistency in applying these changes across the test file is commendable.


235-238: LGTM: Consistent application of TestConfig

This change maintains consistency with the previous updates to the app.Test method calls. The fiber.TestConfig is applied with the same parameters:

  • Timeout: -1
  • ErrOnTimeout: false

The uniform application of these changes throughout the test file demonstrates a thorough and systematic approach to updating the test configurations.


362-365: LGTM: Consistent TestConfig implementation

This change continues the pattern of updating app.Test method calls with fiber.TestConfig. The configuration remains consistent:

  • Timeout: -1
  • ErrOnTimeout: false

The uniform application of these changes throughout the entire test file demonstrates a comprehensive approach to implementing the new TestConfig feature.


Line range hint 1-665: Overall: Successful implementation of TestConfig

The changes in this file consistently update all app.Test method calls to use the new fiber.TestConfig structure. Key points:

  1. All updates use the same configuration (Timeout: -1, ErrOnTimeout: false), maintaining the previous behavior.
  2. The changes are applied systematically throughout the file, demonstrating thoroughness.
  3. No modifications were made to the existing test logic, preserving test coverage and behavior.

These updates successfully implement the TestConfig feature as outlined in the PR objectives, enhancing the configurability of the tests while maintaining backward compatibility.

middleware/proxy/proxy_test.go (12)

113-116: LGTM: Improved test configuration

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is a good improvement. This change enhances the test's reliability and error handling capabilities.


178-181: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous change. This improves the test's reliability and maintains consistency across test cases.


204-207: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


233-236: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


414-417: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


441-444: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


542-545: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


583-586: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


607-610: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. This maintains the improved test reliability and consistency across test cases.


654-657: LGTM: Consistent test configuration improvement with a note

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the previous changes. However, note that this test uses a 1-second timeout instead of the 2-second timeout used in other tests. Ensure this difference is intentional and aligns with the specific requirements of this test case.


750-753: LGTM: Consistent test configuration improvement

The update to use fiber.TestConfig with a specific timeout and error handling for timeouts is consistent with the majority of the previous changes. This maintains the improved test reliability and consistency across test cases.


Line range hint 1-824: Overall improvement in test reliability and consistency

The changes made to this file consistently update the app.Test() calls to use fiber.TestConfig with explicit timeout handling. This improvement enhances the reliability of the tests and provides better error handling for timeout scenarios. The consistent application of these changes across multiple test cases is commendable and aligns with good testing practices.

One test case (lines 654-657) uses a 1-second timeout instead of the 2-second timeout used in other tests. Ensure this difference is intentional and aligns with the specific requirements of that test case.

These changes contribute to a more robust and maintainable test suite for the proxy middleware.

helpers.go (2)

616-619: LGTM! Thread-safe connection state tracking added.

The addition of isClosed boolean and sync.Mutex to the testConn struct improves its functionality:

  1. isClosed allows tracking the connection state.
  2. sync.Mutex ensures thread-safety for concurrent operations on the connection.

These changes enhance the reliability and safety of the testConn struct in multi-threaded scenarios.


622-627: LGTM! Thread-safe Read method implementation.

The Read method has been updated to use a mutex lock, which ensures thread-safety when reading from the buffer. The defer statement guarantees that the lock is always released, even if an error occurs during the read operation.

This change prevents potential race conditions in concurrent scenarios, improving the overall reliability of the testConn struct.

app_test.go (2)

1127-1130: LGTM! TestConfig struct improves test configurability.

The introduction of the TestConfig struct enhances the flexibility of the app.Test method. Setting Timeout: -1 and ErrOnTimeout: false maintains the existing behavior while allowing for future extensibility.


Line range hint 1127-1493: Overall assessment: Well-implemented TestConfig functionality.

The changes in this file effectively introduce and utilize the new TestConfig struct, aligning perfectly with the PR objectives. The modifications enhance the testing capabilities of the Fiber framework, particularly for configurable timeouts and error handling in streaming scenarios. The new test cases provide good coverage for different timeout situations, including empty responses.

app.go (3)

868-881: Add TestConfig struct to enhance test configuration.

The introduction of the TestConfig struct provides flexibility in configuring the app.Test method, allowing customization of timeout durations and error handling on timeouts. This enhances the testing capabilities and allows for more granular control during tests.


885-894: Update app.Test method to accept TestConfig.

The app.Test method now accepts a variadic TestConfig parameter instead of a timeout duration. This change enhances functionality by allowing more configuration options. Ensure that existing code that calls app.Test is updated accordingly, and consider documenting this change clearly for users.

If backward compatibility is a concern, you might want to verify that existing calls to app.Test with timeout durations are handled properly.


933-941: Implement timeout handling using TestConfig.

The updated logic correctly utilizes the Timeout and ErrOnTimeout fields from TestConfig to manage timeouts during testing. This provides flexible error handling and improves the robustness of the test method.

ctx_test.go (1)

3145-3148: 🛠️ Refactor suggestion

Improve test validation by checking Content-Encoding

Instead of comparing the Content-Length to a hard-coded value, consider verifying that the Content-Encoding header matches the expected compression algorithm. This ensures that the response is compressed using the expected algorithm and makes the test more robust.

Apply this diff to improve the test:

-               require.NotEqual(t, "58726", resp.Header.Get(HeaderContentLength))
+               require.Equal(t, algo, resp.Header.Get(HeaderContentEncoding))

Likely invalid or redundant comment.

middleware/logger/logger_test.go (2)

303-306: Correct implementation of fiber.TestConfig in test.

The test has been appropriately updated to use fiber.TestConfig, enhancing control over timeout and error handling during testing, in line with the new TestConfig enhancements.


348-351: Consistent application of fiber.TestConfig in tests.

The use of fiber.TestConfig in this test function ensures consistency and takes advantage of the new testing configuration options.

Comment on lines 138 to 141
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), fiber.TestConfig{
Timeout: 5 * time.Second,
ErrOnTimeout: true,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consistent use of fiber.TestConfig across tests

The changes here mirror those in the Test_Pprof_Subs function, which is good for consistency. The use of fiber.TestConfig brings the same benefits of improved flexibility and error handling to this test function.

While the current implementation is correct, there's an opportunity to reduce code duplication:

Consider extracting the common test configuration into a shared variable or helper function. This would make the tests more maintainable and reduce the risk of inconsistencies if the configuration needs to be updated in the future. For example:

var defaultTestConfig = fiber.TestConfig{
    Timeout:      5 * time.Second,
    ErrOnTimeout: true,
}

// Then in your tests:
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), defaultTestConfig)

This approach would make it easier to maintain consistent test configurations across multiple test functions.

Comment on lines 741 to 744
resp, err := app.Test(req, fiber.TestConfig{
Timeout: 10 * time.Second,
ErrOnTimeout: true,
})
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 9, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor repeated fiber.TestConfig instances to adhere to DRY principle

The fiber.TestConfig{Timeout: 10 * time.Second, ErrOnTimeout: true} configuration is repeated multiple times across your test functions. To enhance maintainability and reduce code duplication, consider defining this configuration once and reusing it.

You can define a variable at the beginning of your test file or within a common setup function:

var testConfig = fiber.TestConfig{
    Timeout:      10 * time.Second,
    ErrOnTimeout: true,
}

Then, update your test calls to use the shared configuration:

- resp, err := app.Test(req, fiber.TestConfig{
-     Timeout:      10 * time.Second,
-     ErrOnTimeout: true,
- })
+ resp, err := app.Test(req, testConfig)

This refactoring improves code readability and makes it easier to manage timeout settings across your tests.

Also applies to: 754-757, 778-781, 804-807, 848-851

Copy link
Member

Choose a reason for hiding this comment

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

@grivera64 Can you make this change

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaby I've made this change for a few of the files in the middleware that use a single testConfig. A few other middleware used 2 or more different configs, so I didn't use a variable for those to avoid confusion between the configs.

app.go Outdated Show resolved Hide resolved
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

🧹 Outside diff range and nitpick comments (2)
app.go (2)

885-894: LGTM! Improved flexibility with TestConfig.

The updated Test method now accepts a more versatile TestConfig, enhancing test customization. The default configuration is correctly set up, and user-provided config is properly applied if available.

Consider adding a comment explaining that the first TestConfig in the variadic parameter is used if provided, for added clarity:

 // Override config if provided
 if len(config) > 0 {
+    // Use the first TestConfig if multiple are provided
     cfg = config[0]
 }

933-941: LGTM! Improved timeout handling with TestConfig.

The updated timeout handling logic correctly utilizes the TestConfig fields, providing more flexibility and control over test behavior. The inclusion of the actual timeout duration in the error message is a nice touch for debugging.

For consistency with the rest of the codebase, consider using errors.New instead of fmt.Errorf for the static error message:

 if cfg.ErrOnTimeout {
-    return nil, fmt.Errorf("test: timeout error after %s", cfg.Timeout)
+    return nil, errors.New("test: timeout error after " + cfg.Timeout.String())
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6112c04 and 4a97d7b.

📒 Files selected for processing (1)
  • app.go (4 hunks)
🧰 Additional context used
🔇 Additional comments (2)
app.go (2)

868-880: LGTM! Well-structured and documented TestConfig struct.

The new TestConfig struct is a great addition, providing users with more control over the test behavior. The clear documentation for each field, including default values, enhances usability.


959-961: LGTM! Consistent error handling.

The update to use errors.New for the static error message is a good change. It improves consistency with the rest of the codebase and addresses the previous review comment.

@gaby gaby added this to the v3 milestone Oct 10, 2024
app.go Show resolved Hide resolved
app.go Outdated
case <-time.After(cfg.Timeout):
conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here
if cfg.ErrOnTimeout {
return nil, fmt.Errorf("test: timeout error after %s", cfg.Timeout)
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we could replace with os.ErrDeadlineExceeded() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaby I believe we can, ErrDeadlineExceeded seems to be used by the net package according to the os package comment. To follow common convention, we can return this error.

Should we also wrap this error with any extra details about the timeout? Like for example:

fmt.Errorf("%w: failed after %s", os.ErrDeadlineExceeded, cfg.Timeout)
// Returns wrapped error: "i/o timeout: failed after 1s"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaby I've change it to use os.ErrDeadlineExceeded as the error type. Thanks for the recommendation!

- Rename TestConfig.ErrOnTimeout to TestConfig.FailOnTimeout
- Update documentation to use changed name
  - Also fix stale documentation about passing Timeout as a
    single argument
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

🧹 Outside diff range and nitpick comments (1)
middleware/logger/logger_test.go (1)

348-351: Consider standardizing timeout duration across similar tests.

While the implementation is correct, I notice that this test uses a 2-second timeout while the previous test uses 3 seconds. Consider standardizing the timeout duration across similar test cases for consistency.

-			Timeout:       2 * time.Second,
+			Timeout:       3 * time.Second,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 4a97d7b and 78e32a7.

📒 Files selected for processing (11)
  • app.go (4 hunks)
  • app_test.go (4 hunks)
  • ctx_test.go (1 hunks)
  • docs/api/app.md (2 hunks)
  • middleware/compress/compress_test.go (6 hunks)
  • middleware/idempotency/idempotency_test.go (1 hunks)
  • middleware/keyauth/keyauth_test.go (4 hunks)
  • middleware/logger/logger_test.go (2 hunks)
  • middleware/pprof/pprof_test.go (2 hunks)
  • middleware/proxy/proxy_test.go (11 hunks)
  • middleware/static/static_test.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
  • app_test.go
  • ctx_test.go
  • docs/api/app.md
  • middleware/compress/compress_test.go
  • middleware/idempotency/idempotency_test.go
  • middleware/keyauth/keyauth_test.go
  • middleware/pprof/pprof_test.go
  • middleware/proxy/proxy_test.go
  • middleware/static/static_test.go
🧰 Additional context used
🔇 Additional comments (4)
middleware/logger/logger_test.go (1)

303-306: LGTM! Clean transition to TestConfig.

The update properly implements the new TestConfig struct with appropriate timeout and failure behavior settings.

app.go (3)

868-880: LGTM! Well-structured TestConfig implementation.

The struct is well-designed with clear documentation and sensible defaults, perfectly aligning with the PR objectives for configurable testing.


883-894: LGTM! Clean implementation of TestConfig handling.

The method signature change and default configuration setup are well-implemented, maintaining backward compatibility while adding new functionality.


959-961: 🛠️ Refactor suggestion

Enhance empty response error handling.

As suggested in the past review, we should use errors.New for static error messages and provide more context.

Apply this improvement:

- return nil, errors.New("test: got empty response")
+ return nil, errors.New("test: received empty response from server")

@grivera64 grivera64 requested a review from gaby November 13, 2024 05:25
@grivera64
Copy link
Contributor Author

grivera64 commented Nov 13, 2024

I have added commits to address previous comments. I think the only failing check is due to an unrelated method, so this should be good from the checks perspective. Please let me know if you would like to make any more changes to the PR.

@gaby
Copy link
Member

gaby commented Nov 13, 2024

@grivera64 The benchmarks are failing related to the github-action we are using. I have reported the issue but there's no fix yet.

Related: benchmark-action/github-action-benchmark#264

@gaby
Copy link
Member

gaby commented Nov 13, 2024

@grivera64 Don't forget to update the "What's New" section, since this changes how using app.Test() works.

https://github.com/gofiber/fiber/blob/main/docs/whats_new.md

@grivera64
Copy link
Contributor Author

@grivera64 The benchmarks are failing related to the github-action we are using. I have reported the issue but there's no fix yet.

Related: benchmark-action/github-action-benchmark#264

Interesting, would this affect the ability to work on Pull Requests?

@grivera64
Copy link
Contributor Author

@grivera64 Don't forget to update the "What's New" section, since this changes how using app.Test() works.

https://github.com/gofiber/fiber/blob/main/docs/whats_new.md

Thanks for the reminder @gaby , I've added an entry for app.Test() in the docs/whats_new.md file.

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

🧹 Outside diff range and nitpick comments (2)
docs/whats_new.md (2)

189-190: Enhance the introduction of TestConfig.

The current introduction could be more informative. Consider adding details about the key benefits and use cases of the new TestConfig.

-The `app.Test()` method now allows users to customize their test configurations:
+The `app.Test()` method now accepts a `TestConfig` struct that allows users to customize test behavior, including:
+- Timeout duration for test execution
+- Control over response handling when timeouts occur
+- Enhanced testing capabilities for streaming responses

214-220: Add more context about the signature change.

The documentation should explain why the signature was changed and the benefits it brings.

-To provide configurable testing capabilities, we had to change
-the signature of the `Test` method.
+The signature of the `Test` method has been updated to provide more control over test behavior:
+
+- The new `TestConfig` struct replaces the simple timeout parameter
+- This change enables advanced features like:
+  - Configurable timeout handling for streaming responses
+  - Control over response preservation on timeout
+  - Future extensibility for additional test configurations
+
 ```diff
-    Test(req *http.Request, timeout ...time.Duration) (*http.Response, error)
+    Test(req *http.Request, config ...fiber.TestConfig) (*http.Response, error)

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 438b6302de020557ae165d42ba006f7e412bfa70 and 0326c07a1b17b1134987b37a906efa36e9316896.

</details>

<details>
<summary>📒 Files selected for processing (1)</summary>

* `docs/whats_new.md` (2 hunks)

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

docs/whats_new.md Outdated Show resolved Hide resolved
app_test.go Outdated Show resolved Hide resolved
Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

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

thanks for the PR
we are close to the merge just need to adjust the hints

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

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

thanks for the PR
we are close to the merge just need to adjust the hints

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

🧹 Outside diff range and nitpick comments (1)
ctx_test.go (1)

3246-3249: Good addition of test timeout configuration.

The explicit timeout configuration with FailOnTimeout helps prevent test hangs and ensures proper test failure behavior. Consider also adding test cases to verify timeout behavior:

// Test timeout scenario
resp, err := app.Test(req, TestConfig{
    Timeout: 1 * time.Millisecond,
    FailOnTimeout: true,
})
require.Error(t, err)
require.True(t, errors.Is(err, ErrTimeout))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0326c07 and 6b6674f.

📒 Files selected for processing (4)
  • ctx_test.go (1 hunks)
  • docs/api/app.md (2 hunks)
  • docs/whats_new.md (2 hunks)
  • middleware/logger/logger_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/api/app.md
  • middleware/logger/logger_test.go
🔇 Additional comments (1)
docs/whats_new.md (1)

194-210: Enhance the example to better demonstrate TestConfig's capabilities.

A past review comment already suggested improving this example to better showcase the streaming and timeout handling capabilities. The suggestion remains valid and aligns with the PR objectives of demonstrating how TestConfig helps with testing streaming responses and timeout scenarios.

docs/whats_new.md Show resolved Hide resolved
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

🧹 Outside diff range and nitpick comments (4)
docs/whats_new.md (2)

78-79: Clarify the timeout value change.

The documentation about timeout values needs more clarity. Consider adding an explanation of why the timeout representation changed from -1 to 0.

 - Test -> Replaced timeout with a config parameter
-  - -1 represents no timeout -> 0 represents no timeout
+  - Changed timeout representation: Previously -1 represented no timeout, now 0 represents no timeout
+  - This change aligns with Go's common practice of using 0 to represent "no timeout" in time.Duration

188-214: Enhance the TestConfig example for better clarity.

The current example doesn't effectively demonstrate the key features of TestConfig, particularly the timeout and error handling capabilities.

 ```go
 // Create a test app with a handler to test
 app := fiber.New()
-app.Get("/", func(c fiber.Ctx) {
-  return c.SendString("hello world")
+app.Get("/", func(c fiber.Ctx) error {
+  // Simulate a slow response
+  time.Sleep(100 * time.Millisecond)
+  return c.SendString("hello world")
 })

 // Define the HTTP request and custom TestConfig to test the handler
 req := httptest.NewRequest(MethodGet, "/", nil)
 testConfig := fiber.TestConfig{
-  Timeout:       0,
-  FailOnTimeout: false,
+  // Set timeout to 50ms to simulate a timeout scenario
+  Timeout:       50 * time.Millisecond,
+  // When false, returns partial response even if timeout occurs
+  FailOnTimeout: false,
 }

 // Test the handler using the request and testConfig
 resp, err := app.Test(req, testConfig)
+if err != nil {
+  // Handle timeout error
+  log.Printf("Test error: %v", err)
+}

</blockquote></details>
<details>
<summary>app_test.go (2)</summary><blockquote>

`1128-1130`: **Consider using a more explicit value for "no timeout".**

Using `Timeout: 0` is not immediately clear whether it means "no timeout" or "immediate timeout". Consider using `-1` or a named constant to make the intention more explicit.

```diff
-		Timeout: 0,
+		Timeout: -1, // No timeout

1139-1142: Consider increasing the timeout duration to prevent flaky tests.

Using a very short timeout of 20ms could lead to flaky tests on slower systems or under high load. Consider increasing this to a more reasonable duration (e.g., 100ms) while still keeping the test fast.

-		Timeout:       20 * time.Millisecond,
+		Timeout:       100 * time.Millisecond,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6b6674f and 334dbe8.

📒 Files selected for processing (5)
  • app.go (4 hunks)
  • app_test.go (5 hunks)
  • docs/api/app.md (2 hunks)
  • docs/whats_new.md (2 hunks)
  • middleware/keyauth/keyauth_test.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/api/app.md
  • middleware/keyauth/keyauth_test.go
🧰 Additional context used
📓 Learnings (1)
app.go (1)
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`.
🔇 Additional comments (7)
docs/whats_new.md (1)

215-221: 🛠️ Refactor suggestion

Add context about TestConfig struct fields.

The signature change section should include details about the available TestConfig fields and their purposes.

Note: This comment was previously made in a past review and is still valid. The documentation should explain the TestConfig struct fields to help users understand the available configuration options.

app.go (4)

906-918: LGTM! Well-structured and documented TestConfig.

The struct is well-designed with clear documentation and appropriate field names. The FailOnTimeout field name is a good choice, making it clear that it controls the error behavior on timeout.


923-932: LGTM! Clean implementation of the new Test method signature.

The method properly handles the new TestConfig parameter with sensible defaults. Breaking change is acceptable as this is for version 3 of the framework.


971-979: LGTM! Proper timeout handling with os.ErrDeadlineExceeded.

The timeout implementation is clean and follows best practices:

  • Uses select for timeout handling
  • Returns os.ErrDeadlineExceeded as recommended
  • Properly closes the connection on timeout

997-999: 🛠️ Refactor suggestion

Simplify error creation using errors.New.

For static error messages without formatting, it's more efficient to use errors.New.

Apply this diff:

-			return nil, errors.New("test: got empty response")
+			return nil, errors.New("test: got empty response")

Likely invalid or redundant comment.

app_test.go (2)

1471-1476: LGTM! Good error handling test.

The test properly verifies that the correct error type (os.ErrDeadlineExceeded) is returned when a timeout occurs. The timeout duration is reasonable, and the test provides good coverage of the error handling path.


1478-1491: LGTM! Good test coverage for empty response scenario.

This test case nicely complements the timeout tests by covering the specific case where FailOnTimeout is false and an empty response is received. The error message is clear and descriptive, making debugging easier.

@grivera64
Copy link
Contributor Author

Thanks for the feedback @ReneWerner87 ! I've updated the code and documentation per the requests. Please let me know if we should make any more changes or additions to the PR.

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

🧹 Outside diff range and nitpick comments (2)
docs/whats_new.md (2)

223-229: Add blank lines around lists for better readability.

The list of configuration options should be surrounded by blank lines for better formatting and readability.

The `TestConfig` struct provides the following configuration options:
+
- `Timeout`:
  - The duration to wait before timing out the test. Use 0 for no timeout.
- `FailOnTimeout`:
  - When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration.
  - When false, the test will return the partial response received before timing out.
+
🧰 Tools
🪛 LanguageTool

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: - When true, the test will return an ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 Markdownlint

224-224: null
Lists should be surrounded by blank lines

(MD032, blanks-around-lists)

🪛 GitHub Check: markdownlint

[failure] 224-224: Lists should be surrounded by blank lines
docs/whats_new.md:224 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Timeout:"] https://github.com/DavidAnson/markdownlint/blob/v0.36.1/doc/md032.md


188-249: Documentation looks good with clear examples and explanations!

The Test Config section effectively documents the new feature with:

  • Clear explanation of the API changes
  • Well-structured example code
  • Detailed configuration options
  • Default values and important notes

Consider adding cross-references to related sections or examples where this feature is particularly useful, such as streaming response testing or connection timeout scenarios.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: - When true, the test will return an ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 Markdownlint

224-224: null
Lists should be surrounded by blank lines

(MD032, blanks-around-lists)

🪛 GitHub Check: markdownlint

[failure] 224-224: Lists should be surrounded by blank lines
docs/whats_new.md:224 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Timeout:"] https://github.com/DavidAnson/markdownlint/blob/v0.36.1/doc/md032.md

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 334dbe8 and b389aaa.

📒 Files selected for processing (1)
  • docs/whats_new.md (2 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/whats_new.md

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: - When true, the test will return an ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 Markdownlint
docs/whats_new.md

224-224: null
Lists should be surrounded by blank lines

(MD032, blanks-around-lists)

🪛 GitHub Check: markdownlint
docs/whats_new.md

[failure] 224-224: Lists should be surrounded by blank lines
docs/whats_new.md:224 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Timeout:"] https://github.com/DavidAnson/markdownlint/blob/v0.36.1/doc/md032.md

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

🧹 Outside diff range and nitpick comments (1)
docs/whats_new.md (1)

225-230: Fix formatting inconsistencies in configuration options.

The bullet points for configuration options have inconsistent formatting with extra spaces. Consider standardizing the format:

-  - `Timeout`:
-    - The duration to wait before timing out the test. Use 0 for no timeout.
-  - `FailOnTimeout`:
-    - When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration.
-    - When false, the test will return the partial response received before timing out.
+- `Timeout`: The duration to wait before timing out the test. Use 0 for no timeout.
+- `FailOnTimeout`: Controls the behavior when a timeout occurs:
+  - When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration.
+  - When false, the test will return the partial response received before timing out.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~225-~225: Loose punctuation mark.
Context: ...wing configuration options: - Timeout: - The duration to wait before timing ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~227-~227: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: - When true, the test will return an ...

(UNLIKELY_OPENING_PUNCTUATION)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b389aaa and be650d6.

📒 Files selected for processing (1)
  • docs/whats_new.md (2 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/whats_new.md

[uncategorized] ~225-~225: Loose punctuation mark.
Context: ...wing configuration options: - Timeout: - The duration to wait before timing ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~227-~227: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: - When true, the test will return an ...

(UNLIKELY_OPENING_PUNCTUATION)

🔇 Additional comments (1)
docs/whats_new.md (1)

195-211: Enhance example to demonstrate practical TestConfig usage.

The current example doesn't effectively showcase the key benefits of TestConfig, particularly for streaming responses and timeout handling.

Note: A previous review comment already suggested an improved example that better demonstrates TestConfig's features, particularly for streaming responses. That suggestion remains valid and applicable.

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

🧹 Outside diff range and nitpick comments (3)
docs/whats_new.md (3)

195-211: Enhance the example to demonstrate practical use cases.

The current example is too basic and doesn't showcase the key benefits of the new TestConfig. Consider adding examples that demonstrate:

  • Testing streaming responses with timeouts
  • Handling partial responses
  • Testing error scenarios
 // Create a test app with a handler to test
 app := fiber.New()
-app.Get("/", func(c fiber.Ctx) {
-  return c.SendString("hello world")
+app.Get("/stream", func(c fiber.Ctx) error {
+  c.Set("Content-Type", "text/event-stream")
+  
+  // Simulate a slow streaming response
+  return c.SendStreamWriter(func(w *bufio.Writer) {
+    for i := 0; i < 3; i++ {
+      fmt.Fprintf(w, "data: Message %d\n\n", i)
+      w.Flush()
+      time.Sleep(500 * time.Millisecond)
+    }
+  })
 })

 // Define the HTTP request and custom TestConfig to test the handler
-req := httptest.NewRequest(MethodGet, "/", nil)
+req := httptest.NewRequest(MethodGet, "/stream", nil)
 testConfig := fiber.TestConfig{
-  Timeout:       0,
-  FailOnTimeout: false,
+  // Set timeout to 1 second to capture partial response
+  Timeout:       time.Second,
+  // Don't fail on timeout to examine partial response
+  FailOnTimeout: false,
 }

 // Test the handler using the request and testConfig
 resp, err := app.Test(req, testConfig)
+if err != nil {
+  log.Fatal(err)
+}
+
+// Read the partial response received before timeout
+body, _ := io.ReadAll(resp.Body)
+fmt.Println(string(body))

223-229: Improve formatting of configuration options.

The configuration options section needs better formatting for clarity. Consider using a table format or better bullet points.

 The `TestConfig` struct provides the following configuration options:
 
-  - `Timeout`: The duration to wait before timing out the test. Use 0 for no timeout.
-  - `FailOnTimeout`: Controls the behavior when a timeout occurs:
-    - When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration.
-    - When false, the test will return the partial response received before timing out.
+| Option | Type | Description |
+|--------|------|-------------|
+| `Timeout` | `time.Duration` | The duration to wait before timing out the test. Use 0 for no timeout. |
+| `FailOnTimeout` | `bool` | Controls the behavior when a timeout occurs:<br>• `true`: Returns `os.ErrDeadlineExceeded` if test exceeds `Timeout`<br>• `false`: Returns partial response received before timeout |
🧰 Tools
🪛 LanguageTool

[uncategorized] ~225-~225: Loose punctuation mark.
Context: ...wing configuration options: - Timeout: The duration to wait before timing out ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~226-~226: Loose punctuation mark.
Context: ... Use 0 for no timeout. - FailOnTimeout: Controls the behavior when a timeout oc...

(UNLIKELY_OPENING_PUNCTUATION)


230-249: Clarify default configuration behavior.

The section about default configuration needs to better explain the behavioral differences between using default config vs empty config.

 If a custom `TestConfig` isn't provided, then the following will be used:
 
 ```go
 testConfig := fiber.TestConfig{
   Timeout:       time.Second,
   FailOnTimeout: true,
 }

-Note: Using this default is NOT the same as providing an empty TestConfig as an argument to app.Test().
+Important Behavior Differences:
+1. Default Config (when no config is provided):

    • Times out after 1 second
    • Returns error on timeout
    • Suitable for most test cases

-An empty TestConfig is the equivalent of:
+2. Empty Config (when fiber.TestConfig{} is provided):

    • Never times out (equivalent to infinite timeout)
    • Never fails on timeout
    • Useful for long-running tests or streaming responses

-```go
-testConfig := fiber.TestConfig{

  • Timeout: 0,
  • FailOnTimeout: false,
    -}
    -```
    +Choose the appropriate configuration based on your testing needs.

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between be650d6276ec4d659176098eae0eb6d3ab992594 and 250b836f094e8753a021f837f6d52ec51846a425.

</details>

<details>
<summary>📒 Files selected for processing (1)</summary>

* `docs/whats_new.md` (2 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🪛 LanguageTool</summary>

<details>
<summary>docs/whats_new.md</summary>

[uncategorized] ~225-~225: Loose punctuation mark.
Context: ...wing configuration options:  - `Timeout`: The duration to wait before timing out ...

(UNLIKELY_OPENING_PUNCTUATION)

---

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ... Use 0 for no timeout. - `FailOnTimeout`: Controls the behavior when a timeout oc...

(UNLIKELY_OPENING_PUNCTUATION)

</details>

</details>

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@grivera64
Copy link
Contributor Author

Thanks for the approval @efectn !

For some reason, the change request from a few days ago hasn't been cleared after pushing changes and resolving the comment. @ReneWerner87 Please let me know if I missed something from the change request that could be causing this issue.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

📝 [Proposal]: Add TestConfig to app.Test() for configurable testing
4 participants