forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportRequest.test.ts
More file actions
128 lines (103 loc) · 4.5 KB
/
Copy pathimportRequest.test.ts
File metadata and controls
128 lines (103 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { describe, expect, it } from 'vitest';
import { curlToRpcRequest, parseCurl, parseImportFile } from './importRequest';
describe('parseCurl', () => {
it('parses method, url, headers, and JSON body from a typical curl command', () => {
const curl = `curl -X POST https://fullnode.mainnet.sui.io:443 -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"suix_getAllBalances","params":["0xabc"]}'`;
const parsed = parseCurl(curl);
expect(parsed.method).toBe('POST');
expect(parsed.url).toBe('https://fullnode.mainnet.sui.io:443');
expect(parsed.headers['Content-Type']).toBe('application/json');
expect(parsed.body).toBe(
'{"jsonrpc":"2.0","id":1,"method":"suix_getAllBalances","params":["0xabc"]}'
);
});
it('handles multi-line curl commands with backslash continuations', () => {
const curl = `curl --location 'https://fullnode.testnet.sui.io:443' \\\n--header 'Content-Type: application/json' \\\n--data '{"method":"sui_getChainIdentifier","params":[]}'`;
const parsed = parseCurl(curl);
expect(parsed.url).toBe('https://fullnode.testnet.sui.io:443');
expect(parsed.body).toBe('{"method":"sui_getChainIdentifier","params":[]}');
});
it('defaults method to POST when a data flag is present without -X', () => {
const curl = `curl https://example.com -d '{"method":"foo","params":[]}'`;
expect(parseCurl(curl).method).toBe('POST');
});
});
describe('curlToRpcRequest', () => {
it('extracts method and params from the JSON-RPC body', () => {
const curl = `curl -X POST https://fullnode.mainnet.sui.io:443 -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"suix_getAllBalances","params":["0xabc"]}'`;
expect(curlToRpcRequest(curl)).toEqual({
name: 'suix_getAllBalances',
method: 'suix_getAllBalances',
params: ['0xabc']
});
});
it('defaults params to an empty array when omitted', () => {
const curl = `curl -X POST https://example.com -d '{"method":"sui_getChainIdentifier"}'`;
expect(curlToRpcRequest(curl).params).toEqual([]);
});
it('throws when there is no data body', () => {
expect(() => curlToRpcRequest('curl https://example.com')).toThrow(/data/i);
});
it('throws when the body is not valid JSON', () => {
expect(() => curlToRpcRequest(`curl https://example.com -d 'not json'`)).toThrow(
/valid JSON/i
);
});
it('throws when the JSON body has no method field', () => {
expect(() =>
curlToRpcRequest(`curl https://example.com -d '{"params":[]}'`)
).toThrow(/method/i);
});
it('throws on an empty command', () => {
expect(() => curlToRpcRequest(' ')).toThrow(/paste a curl/i);
});
});
describe('parseImportFile', () => {
it('parses a single JSON-RPC object', () => {
const requests = parseImportFile(
JSON.stringify({ method: 'sui_getObject', params: ['0x1'] })
);
expect(requests).toEqual([{ name: 'sui_getObject', method: 'sui_getObject', params: ['0x1'] }]);
});
it('parses an array of JSON-RPC objects', () => {
const requests = parseImportFile(
JSON.stringify([
{ method: 'sui_getObject', params: ['0x1'] },
{ method: 'sui_getChainIdentifier', params: [] }
])
);
expect(requests).toHaveLength(2);
expect(requests[0].method).toBe('sui_getObject');
expect(requests[1].method).toBe('sui_getChainIdentifier');
});
it('parses txio RequestItem-shaped exports using name + rpcParams', () => {
const requests = parseImportFile(
JSON.stringify({
name: 'Get Balances',
rpcParams: { method: 'suix_getAllBalances', params: ['0xabc'] }
})
);
expect(requests).toEqual([
{ name: 'Get Balances', method: 'suix_getAllBalances', params: ['0xabc'] }
]);
});
it('extracts JSON-RPC entries from a HAR file, skipping non-RPC entries', () => {
const har = {
log: {
entries: [
{ request: { postData: { text: '{"method":"sui_getObject","params":["0x1"]}' } } },
{ request: { postData: { text: 'not json' } } },
{ request: {} }
]
}
};
const requests = parseImportFile(JSON.stringify(har));
expect(requests).toEqual([{ name: 'sui_getObject', method: 'sui_getObject', params: ['0x1'] }]);
});
it('throws on invalid JSON', () => {
expect(() => parseImportFile('not json')).toThrow(/valid JSON/i);
});
it('throws when nothing importable is found', () => {
expect(() => parseImportFile(JSON.stringify({ foo: 'bar' }))).toThrow(/no json-rpc requests/i);
});
});