forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency-key.test.ts
More file actions
72 lines (64 loc) · 2.25 KB
/
Copy pathidempotency-key.test.ts
File metadata and controls
72 lines (64 loc) · 2.25 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
import { describe, it, expect, vi } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import { resolveLilySdkConfig } from '../src/config/resolve-config';
import type { LilySdkConfig } from '../src/config/types';
describe('Idempotency-Key header from ExecutePaymentRequest', () => {
it('sends Idempotency-Key header when idempotencyKey is set', async () => {
const calls: RequestInit[] = [];
const mockFetch = vi.fn(async (url: string, init: RequestInit) => {
calls.push(init);
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
});
});
const config: LilySdkConfig = {
baseUrl: 'https://api.lily.dev',
apiKey: 'k',
fetch: mockFetch as any,
};
const resolved = resolveLilySdkConfig(config);
const client = createFetchHttpClient(resolved);
await client.request({
method: 'POST',
path: '/v1/payments',
body: {
fromWalletId: 'w1',
toAddress: 'addr1',
amount: { assetCode: 'USDC', amount: '10.00' },
idempotencyKey: 'idem-123',
},
headers: { 'idempotency-key': 'idem-123' },
});
const headers = calls[0].headers as Record<string, string>;
expect(headers['idempotency-key']).toBe('idem-123');
});
it('does not send Idempotency-Key when not provided', async () => {
const calls: RequestInit[] = [];
const mockFetch = vi.fn(async (url: string, init: RequestInit) => {
calls.push(init);
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
});
});
const config: LilySdkConfig = {
baseUrl: 'https://api.lily.dev',
apiKey: 'k',
fetch: mockFetch as any,
};
const resolved = resolveLilySdkConfig(config);
const client = createFetchHttpClient(resolved);
await client.request({
method: 'POST',
path: '/v1/payments',
body: {
fromWalletId: 'w1',
toAddress: 'addr1',
amount: { assetCode: 'USDC', amount: '10.00' },
},
});
const headers = calls[0].headers as Record<string, string>;
expect(headers['idempotency-key']).toBeUndefined();
});
});