This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
fromJson
and toJson
options to HTTP transport
- Loading branch information
1 parent
3a681f1
commit 0690002
Showing
5 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@solana/rpc-transport-http': patch | ||
--- | ||
|
||
Add `fromJson` and `toJson` options to the HTTP transport |
This file contains 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
31 changes: 31 additions & 0 deletions
31
packages/rpc-transport-http/src/__tests__/http-transport-from-json-test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { RpcTransport } from '@solana/rpc-spec'; | ||
|
||
describe('createHttpTransport and `fromJson` function', () => { | ||
let fromJson: jest.Mock; | ||
let fetchSpy: jest.SpyInstance; | ||
let makeHttpRequest: RpcTransport; | ||
beforeEach(async () => { | ||
await jest.isolateModulesAsync(async () => { | ||
fromJson = jest.fn(); | ||
fetchSpy = jest.spyOn(globalThis, 'fetch'); | ||
fetchSpy.mockResolvedValue({ ok: true, text: () => '{"ok":true}' }); | ||
const { createHttpTransport } = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('../http-transport'); | ||
makeHttpRequest = createHttpTransport({ fromJson, url: 'http://localhost' }); | ||
}); | ||
}); | ||
it('uses the `fromJson` function to parse the response from a JSON string', async () => { | ||
expect.assertions(1); | ||
const request = { methodName: 'foo', params: 123 }; | ||
await makeHttpRequest(request); | ||
expect(fromJson).toHaveBeenCalledWith('{"ok":true}', request); | ||
}); | ||
it('returns the value parsed by `fromJson`', async () => { | ||
expect.assertions(1); | ||
const request = { methodName: 'foo', params: 123 }; | ||
fromJson.mockReturnValueOnce({ result: 456 }); | ||
await expect(makeHttpRequest(request)).resolves.toEqual({ result: 456 }); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/rpc-transport-http/src/__tests__/http-transport-to-json-test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { RpcTransport } from '@solana/rpc-spec'; | ||
import { createRpcMessage } from '@solana/rpc-spec-types'; | ||
|
||
describe('createHttpTransport and `toJson` function', () => { | ||
let toJson: jest.Mock; | ||
let fetchSpy: jest.SpyInstance; | ||
let makeHttpRequest: RpcTransport; | ||
beforeEach(async () => { | ||
await jest.isolateModulesAsync(async () => { | ||
toJson = jest.fn(value => JSON.stringify(value)); | ||
fetchSpy = jest.spyOn(globalThis, 'fetch'); | ||
fetchSpy.mockResolvedValue({ json: () => ({ ok: true }), ok: true }); | ||
const { createHttpTransport } = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('../http-transport'); | ||
makeHttpRequest = createHttpTransport({ toJson, url: 'http://localhost' }); | ||
}); | ||
}); | ||
it('uses the `toJson` function to transform the payload to a JSON string', () => { | ||
const request = { methodName: 'foo', params: 123 }; | ||
makeHttpRequest(request); | ||
expect(toJson).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
...createRpcMessage(request.methodName, request.params), | ||
id: expect.any(Number), | ||
}), | ||
request, | ||
); | ||
}); | ||
it('uses passes the JSON string to the fetch API', () => { | ||
toJson.mockReturnValueOnce('{"someAugmented":"jsonString"}'); | ||
makeHttpRequest({ methodName: 'foo', params: 123 }); | ||
expect(fetchSpy).toHaveBeenCalledWith( | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: '{"someAugmented":"jsonString"}', | ||
}), | ||
); | ||
}); | ||
}); |
This file contains 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