-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.test.ts
More file actions
125 lines (108 loc) · 4 KB
/
Copy pathhttp.test.ts
File metadata and controls
125 lines (108 loc) · 4 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
import { describe, it, expect } from 'vitest'
import {
parseNetwork,
parseLimit,
parseOffset,
parseBlockOffset,
parseAddress,
escapeRegex,
sanitizeAddressValidation,
ValidationError,
MIN_LIMIT,
MAX_LIMIT,
MAX_BLOCK_OFFSET,
} from './http'
describe('parseNetwork', () => {
it('defaults to mainnet when absent', () => {
expect(parseNetwork(undefined)).toBe('mainnet')
expect(parseNetwork(null)).toBe('mainnet')
expect(parseNetwork('')).toBe('mainnet')
})
it('accepts the two supported networks', () => {
expect(parseNetwork('mainnet')).toBe('mainnet')
expect(parseNetwork('testnet')).toBe('testnet')
})
it('rejects unknown values', () => {
expect(() => parseNetwork('regtest')).toThrow(ValidationError)
expect(() => parseNetwork('mainnet; DROP TABLE')).toThrow(ValidationError)
expect(() => parseNetwork(42)).toThrow(ValidationError)
})
})
describe('parseLimit', () => {
it('returns the default when absent or unparseable', () => {
expect(parseLimit(undefined)).toBeGreaterThanOrEqual(MIN_LIMIT)
expect(parseLimit('not a number')).toBeGreaterThanOrEqual(MIN_LIMIT)
})
it('clamps to the allowed range instead of throwing', () => {
expect(parseLimit('99999')).toBe(MAX_LIMIT)
expect(parseLimit('-5')).toBe(MIN_LIMIT)
expect(parseLimit('25')).toBe(25)
})
})
describe('parseOffset', () => {
it('returns 0 when absent or unparseable', () => {
expect(parseOffset(undefined)).toBe(0)
expect(parseOffset('foo')).toBe(0)
})
it('clamps negatives to 0 and accepts large values', () => {
expect(parseOffset('-10')).toBe(0)
expect(parseOffset('1000000')).toBe(1_000_000)
})
})
describe('parseBlockOffset', () => {
it('bounds public block pagination offsets', () => {
expect(parseBlockOffset('-10')).toBe(0)
expect(parseBlockOffset('25')).toBe(25)
expect(parseBlockOffset(String(MAX_BLOCK_OFFSET + 1))).toBe(MAX_BLOCK_OFFSET)
})
})
describe('parseAddress', () => {
// A 34-char base58 address (valid shape).
const validAddress = 'fVALIDADDRESSEEEEEEEEEEEEEEEEEEEEEE'.replace(/0|O|I|l/g, 'a')
it('accepts well-formed base58 addresses', () => {
expect(parseAddress(validAddress)).toBe(validAddress)
})
it('rejects addresses that are too short or too long', () => {
expect(() => parseAddress('short')).toThrow(ValidationError)
expect(() => parseAddress('a'.repeat(100))).toThrow(ValidationError)
})
it('rejects characters outside the base58 alphabet', () => {
// '0' is not in base58.
expect(() => parseAddress('0' + validAddress.slice(1))).toThrow(ValidationError)
// SQL/regex injection attempts.
expect(() => parseAddress('abc; DROP TABLE users;')).toThrow(ValidationError)
expect(() => parseAddress(validAddress + '/../')).toThrow(ValidationError)
})
it('rejects non-string input', () => {
expect(() => parseAddress(undefined)).toThrow(ValidationError)
expect(() => parseAddress(42 as unknown)).toThrow(ValidationError)
})
})
describe('escapeRegex', () => {
it('escapes every regex metacharacter', () => {
expect(escapeRegex('a.b*c+d?')).toBe('a\\.b\\*c\\+d\\?')
expect(escapeRegex('(x)[y]{z}')).toBe('\\(x\\)\\[y\\]\\{z\\}')
expect(escapeRegex('|^$\\')).toBe('\\|\\^\\$\\\\')
})
it('leaves plain text unchanged', () => {
expect(escapeRegex('mainnet:abc123')).toBe('mainnet:abc123')
})
})
describe('sanitizeAddressValidation', () => {
it('keeps only public address validation fields', () => {
expect(sanitizeAddressValidation({
isvalid: true,
address: 'fExampleAddress',
ismine: true,
iswatchonly: true,
isscript: true,
pubkey: 'secret-local-pubkey',
account: 'operator-wallet',
labels: ['operator'],
})).toEqual({ isvalid: true, address: 'fExampleAddress' })
})
it('normalizes malformed RPC responses to an invalid public result', () => {
expect(sanitizeAddressValidation(null)).toEqual({ isvalid: false })
expect(sanitizeAddressValidation({ isvalid: 'yes', address: 42 })).toEqual({ isvalid: false })
})
})