-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_test.ts
More file actions
62 lines (60 loc) · 2.21 KB
/
request_test.ts
File metadata and controls
62 lines (60 loc) · 2.21 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
import { parseRequest, send } from './request.ts'
import { assertEquals } from '@std/assert'
Deno.test('parseRequest', async (t) => {
await t.step('Returns an error if not object', () => {
assertEquals(parseRequest('i am text'), 'parse-error')
})
await t.step('Marks as invalid if empty array', () => {
assertEquals(parseRequest('[]'), ['invalid'])
})
await t.step('Marks as invalid if not array of objects', () => {
assertEquals(parseRequest('["i am text"]'), ['invalid'])
})
await t.step('Marks as invalid if version is not 2.0', () => {
assertEquals(parseRequest(JSON.stringify({ method: 'hello' })), ['invalid'])
assertEquals(
parseRequest(JSON.stringify({ method: 'hello', jsonrpc: '1.0' })),
['invalid'],
)
})
await t.step('Marks as invalid if method is missing', () => {
assertEquals(parseRequest(JSON.stringify({ jsonrpc: '2.0' })), ['invalid'])
})
await t.step('Properly parses valid request', () => {
const response = { jsonrpc: '2.0', method: 'hello' }
assertEquals(parseRequest(JSON.stringify(response)), [response])
})
})
Deno.test('send', async (t) => {
await t.step('sends a single message with jsonrpc version', () => {
const sent: string[] = []
const socket = { send: (data: string) => sent.push(data) } as unknown as WebSocket
send(socket, { id: '1', result: 'ok' })
assertEquals(sent.length, 1)
assertEquals(JSON.parse(sent[0]), {
id: '1',
result: 'ok',
jsonrpc: '2.0',
})
})
await t.step('sends batch messages as array', () => {
const sent: string[] = []
const socket = { send: (data: string) => sent.push(data) } as unknown as WebSocket
send(socket, [
{ id: '1', result: 'a' },
{ id: '2', result: 'b' },
])
assertEquals(sent.length, 1)
const parsed = JSON.parse(sent[0])
assertEquals(parsed.length, 2)
assertEquals(parsed[0].jsonrpc, '2.0')
assertEquals(parsed[1].jsonrpc, '2.0')
})
await t.step('sends empty array for zero messages', () => {
const sent: string[] = []
const socket = { send: (data: string) => sent.push(data) } as unknown as WebSocket
send(socket, [])
assertEquals(sent.length, 1)
assertEquals(JSON.parse(sent[0]), [])
})
})