Skip to content

Conversation

@brandur
Copy link
Contributor

@brandur brandur commented Mar 10, 2025

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)

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)
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 brandur requested a review from bgentry March 10, 2025 06:23
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
Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

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

Nice! 🎉

@brandur
Copy link
Contributor Author

brandur commented Mar 10, 2025

Awesome. Ty man.

@brandur brandur merged commit 4500740 into master Mar 10, 2025
2 checks passed
@brandur brandur deleted the brandur-api-test branch March 10, 2025 15:27
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants