-
Notifications
You must be signed in to change notification settings - Fork 0
Add apitest package for testing service handlers
#3
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a small augmentation to the API framework intended to provide a
package for testing service handlers. Service handlers are normal
functions and can be invoked as normal functions, but `apitest` provides
some extra niceties:
* Request structs are validated and an API error is emitted in case
they're invalid.
* Response structs are validated and an error returned in case they're
invalid.
We may add more things down the road as they're needed as well. For
example, `apitest` might inject a valid looking IP address into context
to simulate a more realistic request for purposes that need an IP like
rate limiting. (Nothing we're doing needs this yet, so I didn't bother.)
Sample usage:
endpoint := &testEndpoint{}
resp, err := apitest.InvokeHandler(ctx, endpoint.Execute, &testRequest{ReqField: "string"})
require.NoError(t, err)
19e1129 to
28a3f81
Compare
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
bgentry
approved these changes
Mar 10, 2025
Contributor
bgentry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 🎉
Contributor
Author
|
Awesome. Ty man. |
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
brandur
added a commit
to riverqueue/riverui
that referenced
this pull request
Mar 10, 2025
Modify the test suite to invoke endpoints with `apitest.InvokeHandler` proposed in [1]. This isn't too different than invoking them raw, but provides a couple extra niceties like validating input request structs and returning realistic looking API errors in case of a problem. [1] riverqueue/apiframe#3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a small augmentation to the API framework intended to provide a
package for testing service handlers. Service handlers are normal
functions and can be invoked as normal functions, but
apitestprovidessome extra niceties:
Request structs are validated and an API error is emitted in case
they're invalid.
Response structs are validated and an error returned in case they're
invalid.
We may add more things down the road as they're needed as well. For
example,
apitestmight inject a valid looking IP address into contextto simulate a more realistic request for purposes that need an IP like
rate limiting. (Nothing we're doing needs this yet, so I didn't bother.)
Sample usage: