-
Notifications
You must be signed in to change notification settings - Fork 99
Add HTTP agent #76
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
Merged
Add HTTP agent #76
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a713b1
Add HTTP agent
paulyoung d96bb4a
Fix a typo
paulyoung 369d776
Use an explicit export list
paulyoung 3b3524d
Differentiate async request fields as per the spec
paulyoung 07a2928
Fix comment about providing a nonce
paulyoung 04b463e
Merge branch 'master' into paulyoung/js-user-library-http-agent
paulyoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { BinaryBlob } from "./blob"; | ||
| import { CanisterId } from "./canisterId"; | ||
| import { Request } from "./request"; | ||
| import { SubmitRequestType } from "./submitRequestType"; | ||
|
|
||
| // The fields in a "call" submit request. | ||
| export interface CallRequest extends Request { | ||
| request_type: SubmitRequestType.Call; | ||
| canister_id: CanisterId; | ||
| method_name: string; | ||
| arg: BinaryBlob; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { BinaryBlob } from "./blob"; | ||
| import * as canisterId from "./canisterId"; | ||
| import * as cbor from "./cbor"; | ||
| import { Hex } from "./hex"; | ||
| import { makeHttpAgent } from "./index"; | ||
| import { Nonce } from "./nonce"; | ||
| import { Request } from "./request"; | ||
| import { requestIdOf } from "./requestId"; | ||
| import { RequestType } from "./requestType"; | ||
| import { SenderPubKey } from "./senderPubKey"; | ||
| import { SenderSig } from "./senderSig"; | ||
|
|
||
| test("call", async () => { | ||
| const mockFetch: jest.Mock = jest.fn((resource, init) => { | ||
| return Promise.resolve(new Response(null, { | ||
| status: 200, | ||
| })); | ||
| }); | ||
|
|
||
| const canisterIdent = "0000000000000001" as Hex; | ||
| const nonce = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce; | ||
| const senderPubKey = new Uint8Array(32) as SenderPubKey; | ||
| const senderSig = new Uint8Array(64) as SenderSig; | ||
|
|
||
| const httpAgent = makeHttpAgent({ | ||
| canisterId: canisterIdent, | ||
| fetchFn: mockFetch, | ||
| nonceFn: () => nonce, | ||
| senderPubKey, | ||
| senderSigFn: () => senderSig, | ||
| }); | ||
|
|
||
| const methodName = "greet"; | ||
| const arg = Uint8Array.from([]) as BinaryBlob; | ||
|
|
||
| const { requestId, response } = await httpAgent.call({ | ||
| methodName, | ||
| arg, | ||
| }); | ||
|
|
||
| const expectedRequest: Request = { | ||
| request_type: "call" as RequestType, | ||
| nonce, | ||
| canister_id: canisterId.fromHex(canisterIdent), | ||
| method_name: methodName, | ||
| arg, | ||
| sender_pubkey: senderPubKey, | ||
| sender_sig: senderSig, | ||
| }; | ||
|
|
||
| const expectedRequestId = await requestIdOf(expectedRequest); | ||
|
|
||
| const { calls, results } = mockFetch.mock; | ||
| expect(calls.length).toBe(1); | ||
| expect(requestId).toEqual(expectedRequestId); | ||
|
|
||
| expect(calls[0][0]).toBe("http://localhost:8000/api/v1/submit"); | ||
| expect(calls[0][1]).toEqual({ | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/cbor", | ||
| }, | ||
| body: cbor.encode(expectedRequest), | ||
| }); | ||
| }); | ||
|
|
||
| test.todo("query"); | ||
|
|
||
| test("requestStatus", async () => { | ||
| const mockResponse = { | ||
| status: "replied", | ||
| reply: { arg: Uint8Array.from([]) as BinaryBlob }, | ||
| }; | ||
|
|
||
| const mockFetch: jest.Mock = jest.fn((resource, init) => { | ||
| const body = cbor.encode(mockResponse); | ||
| return Promise.resolve(new Response(body, { | ||
| status: 200, | ||
| })); | ||
| }); | ||
|
|
||
| const canisterIdent = "0000000000000001" as Hex; | ||
| const nonce = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce; | ||
| const senderPubKey = new Uint8Array(32) as SenderPubKey; | ||
| const senderSig = new Uint8Array(64) as SenderSig; | ||
|
|
||
| const httpAgent = makeHttpAgent({ | ||
| canisterId: canisterIdent, | ||
| fetchFn: mockFetch, | ||
| nonceFn: () => nonce, | ||
| senderPubKey, | ||
| senderSigFn: () => senderSig, | ||
| }); | ||
|
|
||
| const requestId = await requestIdOf({ | ||
| request_type: "call" as RequestType, | ||
| nonce, | ||
| canister_id: canisterId.fromHex(canisterIdent), | ||
| method_name: "greet", | ||
| arg: Uint8Array.from([]), | ||
| }); | ||
|
|
||
| const response = await httpAgent.requestStatus({ | ||
| requestId, | ||
| }); | ||
|
|
||
| const expectedRequest: Request = { | ||
| request_type: "request-status" as RequestType, | ||
| nonce, | ||
| request_id: requestId, | ||
| sender_pubkey: senderPubKey, | ||
| sender_sig: senderSig, | ||
| }; | ||
|
|
||
| const { calls, results } = mockFetch.mock; | ||
| expect(calls.length).toBe(1); | ||
|
|
||
| const { | ||
| reply: { arg: responseArg }, | ||
| ...responseRest | ||
| } = response; | ||
|
|
||
| const { | ||
| reply: { arg: mockResponseArg }, | ||
| ...mockResponseRest | ||
| } = mockResponse; | ||
|
|
||
| expect(responseRest).toEqual(mockResponseRest); | ||
| expect(responseArg.equals(mockResponseArg)).toBe(true); | ||
|
|
||
| expect(calls[0][0]).toBe("http://localhost:8000/api/v1/read"); | ||
| expect(calls[0][1]).toEqual({ | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/cbor", | ||
| }, | ||
| body: cbor.encode(expectedRequest), | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
when we extend Request is there a standard way of ordering the fields in the encoding?
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.
Do you mean the CBOR encoding? I haven't given that much thought and have been relying on the
borcpackage for that. It seems to be deterministic though.