这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@coury-clark
Copy link
Contributor

@coury-clark coury-clark commented Jun 5, 2024

This PR extends the humatest.TestAPI interface to allow for a custom context.Context to be passed into the request. This is useful for applications where the context is used to store / propagate request specific values, such as authentication / authorization information. This is essentially porting a wrapper I had to write internally back into the mainline, it seems universally useful enough to me - let me know if you disagree!

This does introduce a breaking change in the TestAPI interface, which is difficult to avoid given the humatest.New function returns the interface. If that isn't acceptable, please let me know!

Alternative naming options are similar to http package - ie. GetWithContext. Let me know if you'd prefer that style.

Thanks for huma!

Summary by CodeRabbit

  • New Features

    • Introduced context handling in API methods to support more advanced request scenarios.
    • Added new Ctx methods for Do, Get, Post, Put, Patch, and Delete to accept a context.Context parameter.
  • Documentation

    • Updated the tutorial to include a new section on using custom context.Context in Go tests for scenarios like testing authenticated routes.

@coderabbitai
Copy link

coderabbitai bot commented Jun 5, 2024

Warning

Rate limit exceeded

@coury-clark has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 8 minutes and 6 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between da24dac and 577406f.

Walkthrough

The recent changes introduce context handling in the humatest/humatest.go API methods by adding context parameters. This update includes new methods suffixed with Ctx that accept a context.Context parameter. Additionally, the tutorial documentation is updated to demonstrate using custom contexts in Go tests, particularly for scenarios like testing authenticated routes.

Changes

Files Change Summary
humatest/humatest.go Introduced context handling in API methods by adding context parameters and creating new methods suffixed with Ctx.
docs/docs/tutorial/writing-tests.md Added a new section demonstrating sending requests with a custom context.Context in Go tests for specific request scenarios.

Poem

In the land where code does flow,
Context joins the API show.
With Ctx in place, requests align,
Authenticated routes now shine.
Tests with context, clear and bright,
Guide our journeys, day and night.
🐰✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 158b076 and 755d418.

Files selected for processing (1)
  • humatest/humatest.go (9 hunks)
Additional comments not posted (8)
humatest/humatest.go (8)

7-7: Added import for context package.

This is necessary for the new context handling features.


49-49: Ensure backward compatibility with the new DoCtx method and its wrapper Do.

The addition of DoCtx alongside the existing Do method, which now wraps DoCtx using context.Background(), maintains backward compatibility while extending functionality.

Also applies to: 51-51


64-64: Ensure backward compatibility with the new GetCtx method and its wrapper Get.

The GetCtx method is correctly implemented with a context parameter, and the Get method is updated to wrap this new method, ensuring backward compatibility.

Also applies to: 66-66


79-79: Ensure backward compatibility with the new PostCtx method and its wrapper Post.

Similar to GetCtx, the PostCtx method introduces context handling, and the Post method wraps it appropriately.

Also applies to: 81-81


94-94: Ensure backward compatibility with the new PutCtx method and its wrapper Put.

The PutCtx method adds context support, and the Put method wraps it, maintaining the existing API's functionality.

Also applies to: 96-96


109-109: Ensure backward compatibility with the new PatchCtx method and its wrapper Patch.

The PatchCtx method is a new addition that handles context, and the Patch method is updated to use this new method, ensuring no disruption to existing functionality.

Also applies to: 111-111


124-124: Ensure backward compatibility with the new DeleteCtx method and its wrapper Delete.

The DeleteCtx method correctly implements the new context handling feature, and the Delete method wraps it, preserving existing behavior.

Also applies to: 126-126


141-141: Review implementation of context handling in DoCtx and its usage in other methods.

The DoCtx method is well-implemented with comprehensive handling of different argument types and proper context integration. This method is then correctly used as the underlying implementation for other context-specific methods (GetCtx, PostCtx, PutCtx, PatchCtx, DeleteCtx), ensuring consistency and reducing code duplication.

Also applies to: 164-164, 195-195, 198-198, 205-205, 208-208, 215-215, 218-218, 225-225, 228-228, 235-235, 238-238

// api.Put("/foo", "X-My-Header: my-value", MyBody{Foo: "bar"})
PutCtx(ctx context.Context, path string, args ...any) *httptest.ResponseRecorder

// Put wraps [TestAPI.PutCtx] using [context.Background].

Choose a reason for hiding this comment

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

For this one and others I think you should address these:

  • the comment should be taken from existing code, here your changes lead to have less information in the comment, so in the godoc
  • you should add something to existing comment to mention PutCtx should be preferred when there is an existing context

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't realize comments inside interfaces don't support links in pkgsite (it does in most editors, for example). I've copied the comment over and edited them to reflect the correct signature.

Choose a reason for hiding this comment

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

I think you missed or ignored my second comment.

The fact you could use in each old code there is a version with a context that should be preferred

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sorry it seemed I missed or ignored this. Let me clarify!

The comments do make some case for the scenarios you'd use it, perhaps implicitly

Get performs a GET request against the API using [context.Background] in the
// [http.Request]

While it's true this is an implicit instruction, I'd expect anyone familiar with Go to understand when they do or do not need to provide their own context.

To address preference, I don't necessarily think from the perspective of the library there is a preference - this is simply a utility after all. Similarly, http has no preference if users use NewRequest or NewRequestWithContext, it's just an option. I'd expect anyone using this library to be able to make their own choices about whether or not they need to provide a context. That said, I didn't update the website documentation (which in practice, is where I get most of the examples from), so please look at da24dac.

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 755d418 and d2a53de.

Files selected for processing (1)
  • humatest/humatest.go (7 hunks)
Files skipped from review as they are similar to previous changes (1)
  • humatest/humatest.go

@coury-clark coury-clark requested a review from ccoVeille June 7, 2024 15:54
Copy link

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

One small remark, nothing really important, so I'm OK 👍

Copy link

@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: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d2a53de and da24dac.

Files selected for processing (1)
  • docs/docs/tutorial/writing-tests.md (1 hunks)
Additional context used
LanguageTool
docs/docs/tutorial/writing-tests.md

[uncategorized] ~163-~163: A comma might be missing here. (AI_EN_LECTOR_MISSING_PUNCTUATION_COMMA)
Context: ...ttps://pkg.go.dev/context#Context), for example you may need to test an authenticated r...


[uncategorized] ~163-~163: When ‘request-specific’ is used as a modifier, it is usually spelled with a hyphen. (SPECIFIC_HYPHEN)
Context: ...ticated route, or test using some other request specific values. ```go func TestGetGreeting(t *...

Markdownlint
docs/docs/tutorial/writing-tests.md

17-17: Column: 1 (MD010, no-hard-tabs)
Hard tabs


18-18: Column: 1 (MD010, no-hard-tabs)
Hard tabs


19-19: Column: 1 (MD010, no-hard-tabs)
Hard tabs


21-21: Column: 1 (MD010, no-hard-tabs)
Hard tabs


22-22: Column: 1 (MD010, no-hard-tabs)
Hard tabs


23-23: Column: 1 (MD010, no-hard-tabs)
Hard tabs


24-24: Column: 1 (MD010, no-hard-tabs)
Hard tabs


26-26: Column: 1 (MD010, no-hard-tabs)
Hard tabs


31-31: Column: 1 (MD010, no-hard-tabs)
Hard tabs


36-36: Column: 1 (MD010, no-hard-tabs)
Hard tabs


37-37: Column: 1 (MD010, no-hard-tabs)
Hard tabs


38-38: Column: 1 (MD010, no-hard-tabs)
Hard tabs


43-43: Column: 1 (MD010, no-hard-tabs)
Hard tabs


44-44: Column: 1 (MD010, no-hard-tabs)
Hard tabs


45-45: Column: 1 (MD010, no-hard-tabs)
Hard tabs


46-46: Column: 1 (MD010, no-hard-tabs)
Hard tabs


47-47: Column: 1 (MD010, no-hard-tabs)
Hard tabs


51-51: Column: 1 (MD010, no-hard-tabs)
Hard tabs


52-52: Column: 1 (MD010, no-hard-tabs)
Hard tabs


53-53: Column: 1 (MD010, no-hard-tabs)
Hard tabs


54-54: Column: 1 (MD010, no-hard-tabs)
Hard tabs


55-55: Column: 1 (MD010, no-hard-tabs)
Hard tabs


56-56: Column: 1 (MD010, no-hard-tabs)
Hard tabs


57-57: Column: 1 (MD010, no-hard-tabs)
Hard tabs


58-58: Column: 1 (MD010, no-hard-tabs)
Hard tabs


59-59: Column: 1 (MD010, no-hard-tabs)
Hard tabs


60-60: Column: 1 (MD010, no-hard-tabs)
Hard tabs


61-61: Column: 1 (MD010, no-hard-tabs)
Hard tabs


62-62: Column: 1 (MD010, no-hard-tabs)
Hard tabs


63-63: Column: 1 (MD010, no-hard-tabs)
Hard tabs


64-64: Column: 1 (MD010, no-hard-tabs)
Hard tabs


65-65: Column: 1 (MD010, no-hard-tabs)
Hard tabs


67-67: Column: 1 (MD010, no-hard-tabs)
Hard tabs


68-68: Column: 1 (MD010, no-hard-tabs)
Hard tabs


69-69: Column: 1 (MD010, no-hard-tabs)
Hard tabs


70-70: Column: 1 (MD010, no-hard-tabs)
Hard tabs


71-71: Column: 1 (MD010, no-hard-tabs)
Hard tabs


72-72: Column: 1 (MD010, no-hard-tabs)
Hard tabs


73-73: Column: 1 (MD010, no-hard-tabs)
Hard tabs


74-74: Column: 1 (MD010, no-hard-tabs)
Hard tabs


75-75: Column: 1 (MD010, no-hard-tabs)
Hard tabs


76-76: Column: 1 (MD010, no-hard-tabs)
Hard tabs


77-77: Column: 1 (MD010, no-hard-tabs)
Hard tabs


78-78: Column: 1 (MD010, no-hard-tabs)
Hard tabs


82-82: Column: 1 (MD010, no-hard-tabs)
Hard tabs


83-83: Column: 1 (MD010, no-hard-tabs)
Hard tabs


84-84: Column: 1 (MD010, no-hard-tabs)
Hard tabs


85-85: Column: 1 (MD010, no-hard-tabs)
Hard tabs


86-86: Column: 1 (MD010, no-hard-tabs)
Hard tabs


88-88: Column: 1 (MD010, no-hard-tabs)
Hard tabs


90-90: Column: 1 (MD010, no-hard-tabs)
Hard tabs


91-91: Column: 1 (MD010, no-hard-tabs)
Hard tabs


92-92: Column: 1 (MD010, no-hard-tabs)
Hard tabs


93-93: Column: 1 (MD010, no-hard-tabs)
Hard tabs


94-94: Column: 1 (MD010, no-hard-tabs)
Hard tabs


95-95: Column: 1 (MD010, no-hard-tabs)
Hard tabs


97-97: Column: 1 (MD010, no-hard-tabs)
Hard tabs


98-98: Column: 1 (MD010, no-hard-tabs)
Hard tabs


110-110: Column: 1 (MD010, no-hard-tabs)
Hard tabs


111-111: Column: 1 (MD010, no-hard-tabs)
Hard tabs


113-113: Column: 1 (MD010, no-hard-tabs)
Hard tabs


117-117: Column: 1 (MD010, no-hard-tabs)
Hard tabs


119-119: Column: 1 (MD010, no-hard-tabs)
Hard tabs


121-121: Column: 1 (MD010, no-hard-tabs)
Hard tabs


122-122: Column: 1 (MD010, no-hard-tabs)
Hard tabs


123-123: Column: 1 (MD010, no-hard-tabs)
Hard tabs


124-124: Column: 1 (MD010, no-hard-tabs)
Hard tabs


128-128: Column: 1 (MD010, no-hard-tabs)
Hard tabs


130-130: Column: 1 (MD010, no-hard-tabs)
Hard tabs


132-132: Column: 1 (MD010, no-hard-tabs)
Hard tabs


133-133: Column: 1 (MD010, no-hard-tabs)
Hard tabs


134-134: Column: 1 (MD010, no-hard-tabs)
Hard tabs


135-135: Column: 1 (MD010, no-hard-tabs)
Hard tabs


137-137: Column: 1 (MD010, no-hard-tabs)
Hard tabs


138-138: Column: 1 (MD010, no-hard-tabs)
Hard tabs


139-139: Column: 1 (MD010, no-hard-tabs)
Hard tabs


143-143: Column: 1 (MD010, no-hard-tabs)
Hard tabs


145-145: Column: 1 (MD010, no-hard-tabs)
Hard tabs


147-147: Column: 1 (MD010, no-hard-tabs)
Hard tabs


148-148: Column: 1 (MD010, no-hard-tabs)
Hard tabs


149-149: Column: 1 (MD010, no-hard-tabs)
Hard tabs


151-151: Column: 1 (MD010, no-hard-tabs)
Hard tabs


152-152: Column: 1 (MD010, no-hard-tabs)
Hard tabs


153-153: Column: 1 (MD010, no-hard-tabs)
Hard tabs


167-167: Column: 1 (MD010, no-hard-tabs)
Hard tabs


169-169: Column: 1 (MD010, no-hard-tabs)
Hard tabs


170-170: Column: 1 (MD010, no-hard-tabs)
Hard tabs


171-171: Column: 1 (MD010, no-hard-tabs)
Hard tabs


173-173: Column: 1 (MD010, no-hard-tabs)
Hard tabs


174-174: Column: 1 (MD010, no-hard-tabs)
Hard tabs


175-175: Column: 1 (MD010, no-hard-tabs)
Hard tabs


176-176: Column: 1 (MD010, no-hard-tabs)
Hard tabs


180-180: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


160-160: null (MD014, commands-show-output)
Dollar signs used before commands without showing output

@ccoVeille
Copy link

LGTM 👍

@codecov
Copy link

codecov bot commented Jun 8, 2024

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.82%. Comparing base (158b076) to head (577406f).
Report is 2 commits behind head on main.

Files Patch % Lines
humatest/humatest.go 95.65% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #469      +/-   ##
==========================================
- Coverage   92.84%   92.82%   -0.03%     
==========================================
  Files          22       22              
  Lines        3761     3778      +17     
==========================================
+ Hits         3492     3507      +15     
- Misses        225      227       +2     
  Partials       44       44              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

@coury-clark this looks great, thank you! 👍

FYI you can already do something similar by making your own http.Request and then using api.Adapter().ServeHTTP(w, r) but this is much simpler!

@danielgtaylor danielgtaylor merged commit 40bda46 into danielgtaylor:main Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants