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

feat: mock transport for tests #507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shramee
Copy link

@shramee shramee commented Nov 26, 2023

Testing functionality based on RPC response

Tests require setting up an RPC server with appropriate state to test different cases.
This makes tests slow and setup tedious.

This PR adds a mock transport mode to mock certain requests.

Features

Flow

JsonRpcTransport::send_request is implemented to,

  1. Tries to find mocked response for request_json in self.mocked_requests.
  2. Tries to find mocked response for request method mock in self.mocked_methods.
  3. Otherwise, Falls back to http_transport if available.
  4. Else throws an error (all requests must be resolved).

Optional HttpTransport fallback

Adding an http_transport allows falling back to HttpTransport for requests not mocked.

Mocking

Responses can be mocked for

  1. specific request json string (high priority) or
  2. all requests if w specific JsonRpcMethod.

Dev Experience

Helpful prints are added to stdout to help build mocks for the requests received and responses from fallback HTTP.

Use this code to mock this request

mock_transport.mock_request(
    "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"starknet_getBlo...}".into(),
    "{\"jsonrpc\":\"2.0\",\"result\":{\"block_hash\":\"0x5cc6b9...}".into()
);

image

@shramee shramee force-pushed the master branch 3 times, most recently from 010f65c to 8d4a802 Compare December 2, 2023 06:23
mock: use raw strings
@shramee
Copy link
Author

shramee commented Dec 2, 2023

Raw strings for cleaner JSON...

Use this code to mock this request

mock_transport.mock_request(
    r#"{"id":1,"jsonrpc":"2.0","method":"starknet_getBlockW...}"#.into(),
    r#"{"jsonrpc":"2.0","result":{"block_hash":"0x5b586405c...}"#.into()
);
image

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.

None yet

1 participant