Skip to content

feat: support batch JSON-RPC request matching in mocks#95

Merged
gregorydemay merged 9 commits intomainfrom
gdemay/DEFI-2565-json-rpc-batch-mock
Mar 20, 2026
Merged

feat: support batch JSON-RPC request matching in mocks#95
gregorydemay merged 9 commits intomainfrom
gdemay/DEFI-2565-json-rpc-batch-mock

Conversation

@gregorydemay
Copy link
Copy Markdown
Contributor

@gregorydemay gregorydemay commented Mar 11, 2026

Motivation

Extend the mock infrastructure to mock JSON-RPC batch requests and responses, where a JSON-RPC batch request is an array of individual JSON-RPC requests sent in a single HTTPS outcall.

Summary

  • Make JsonRpcRequestMatcher generic over the body matcher (JsonRpcHttpRequestMatcher<B>) to support both single and batch JSON-RPC requests
  • Extract body-matching logic into SingleJsonRpcMatcher, reusable for both single and batch matchers
  • Apply the same generic pattern to JsonRpcResponseJsonRpcHttpResponse<B> for batch responses
  • All existing APIs preserved via type aliases (JsonRpcRequestMatcher, JsonRpcResponse)

New public types

  • JsonRpcHttpRequestMatcher<B> — generic request matcher
  • SingleJsonRpcMatcher — body-level matcher for a single JSON-RPC request
  • BatchJsonRpcRequestMatcher — type alias for JsonRpcHttpRequestMatcher<Vec<SingleJsonRpcMatcher>>
  • JsonRpcHttpResponse<B> — generic response
  • BatchJsonRpcResponse — type alias for JsonRpcHttpResponse<Vec<Value>>

🤖 Generated with Claude Code

gregorydemay and others added 6 commits March 11, 2026 11:40
Extract JSON-RPC body matching fields (method, id, params) into a
dedicated `SingleJsonRpcMatcher` struct with its own builder methods
and a `matches_body` helper. This prepares for making the request
matcher generic over single vs batch JSON-RPC bodies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Rename the struct to `JsonRpcHttpRequestMatcher<B>` and introduce:
- `impl<B>` block for shared HTTP-level builders and `matches_http` helper
- `impl JsonRpcHttpRequestMatcher<SingleJsonRpcMatcher>` for body-specific
  builders (`with_method`, `with_id`, `with_params`) and the
  `CanisterHttpRequestMatcher` trait impl
- Type alias `JsonRpcRequestMatcher = JsonRpcHttpRequestMatcher<SingleJsonRpcMatcher>`
  preserving full backward compatibility

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `JsonRpcHttpRequestMatcher<Vec<SingleJsonRpcMatcher>>` with a
`batch` constructor and a `CanisterHttpRequestMatcher` impl that
deserializes the body as a JSON array and matches pairwise in order.
Introduce `BatchJsonRpcRequestMatcher` type alias for ergonomics.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `batch_json_rpc_request_matcher_tests` module with tests covering
batch matching, wrong method/size/order/params, and HTTP-level
constraints (url, host). Export new types (`BatchJsonRpcRequestMatcher`,
`JsonRpcHttpRequestMatcher`, `SingleJsonRpcMatcher`) from `lib.rs`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Rename the struct to `JsonRpcHttpResponse<B>` and introduce:
- `JsonRpcResponse` type alias for `JsonRpcHttpResponse<Value>` (single)
- `BatchJsonRpcResponse` type alias for `JsonRpcHttpResponse<Vec<Value>>`
- Generic `From<JsonRpcHttpResponse<B>> for CanisterHttpResponse` impl
- Single-specific `with_id`/`with_raw_id` and `From` conversions
- Batch-specific `From<Vec<Value>>` conversion

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
gregorydemay added a commit to dfinity/evm-rpc-canister that referenced this pull request Mar 11, 2026
Point canhttp, ic-canister-runtime, and ic-pocket-canister-runtime
to dfinity/canhttp#95 which adds BatchJsonRpcRequestMatcher for
testing batch JSON-RPC requests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gregorydemay gregorydemay marked this pull request as ready for review March 13, 2026 16:00
@gregorydemay gregorydemay requested a review from a team as a code owner March 13, 2026 16:00
@gregorydemay gregorydemay requested a review from lpahlavi March 13, 2026 16:00
Copy link
Copy Markdown
Contributor

@lpahlavi lpahlavi 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 @gregorydemay! Mostly a comment regarding naming from me, but otherwise LGTM! 💪

Copy link
Copy Markdown
Contributor Author

@gregorydemay gregorydemay 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 review @lpahlavi ! Should be ready for another round. Once merged I will proceed to cutting a new release.

@gregorydemay gregorydemay requested a review from lpahlavi March 20, 2026 10:46
Copy link
Copy Markdown
Contributor

@lpahlavi lpahlavi 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 changes @gregorydemay, PR LGTM!

@gregorydemay gregorydemay merged commit c798e27 into main Mar 20, 2026
10 checks passed
@gregorydemay gregorydemay deleted the gdemay/DEFI-2565-json-rpc-batch-mock branch March 20, 2026 13:23
@github-actions github-actions bot mentioned this pull request Mar 23, 2026
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.

2 participants