-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathresend.spec.ts
More file actions
135 lines (105 loc) · 4.32 KB
/
resend.spec.ts
File metadata and controls
135 lines (105 loc) · 4.32 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
129
130
131
132
133
134
135
import createFetchMock from 'vitest-fetch-mock';
import { Resend } from './resend';
import { mockSuccessResponse } from './test-utils/mock-fetch';
const fetchMocker = createFetchMock(vi);
fetchMocker.enableMocks();
describe('Resend', () => {
afterEach(() => fetchMock.resetMocks());
afterAll(() => fetchMocker.disableMocks());
describe('constructor options', () => {
it('uses default baseUrl and userAgent when no options provided', () => {
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
expect(resend.baseUrl).toBe('https://api.resend.com');
expect(resend.userAgent).toMatch(/^resend-node:/);
});
it('uses custom baseUrl when options.baseUrl is provided', () => {
const customBaseUrl = 'https://eu.api.resend.com';
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
baseUrl: customBaseUrl,
});
expect(resend.baseUrl).toBe(customBaseUrl);
});
it('uses custom userAgent when options.userAgent is provided', () => {
const customUserAgent = 'my-app/1.0';
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
userAgent: customUserAgent,
});
expect(resend.userAgent).toBe(customUserAgent);
});
it('uses both custom baseUrl and userAgent when both provided', () => {
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
baseUrl: 'https://custom.api.com',
userAgent: 'custom-agent/2.0',
});
expect(resend.baseUrl).toBe('https://custom.api.com');
expect(resend.userAgent).toBe('custom-agent/2.0');
});
it('uses RESEND_BASE_URL from env when no options.baseUrl provided', () => {
const originalEnv = process.env;
process.env = {
...originalEnv,
RESEND_BASE_URL: 'https://env-base-url.example.com',
};
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
expect(resend.baseUrl).toBe('https://env-base-url.example.com');
process.env = originalEnv;
});
it('uses RESEND_USER_AGENT from env when no options.userAgent provided', () => {
const originalEnv = process.env;
process.env = {
...originalEnv,
RESEND_USER_AGENT: 'env-user-agent/1.0',
};
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
expect(resend.userAgent).toBe('env-user-agent/1.0');
process.env = originalEnv;
});
it('options.baseUrl overrides RESEND_BASE_URL env', () => {
const originalEnv = process.env;
process.env = {
...originalEnv,
RESEND_BASE_URL: 'https://env-base-url.example.com',
};
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
baseUrl: 'https://options-base-url.example.com',
});
expect(resend.baseUrl).toBe('https://options-base-url.example.com');
process.env = originalEnv;
});
it('options.userAgent overrides RESEND_USER_AGENT env', () => {
const originalEnv = process.env;
process.env = {
...originalEnv,
RESEND_USER_AGENT: 'env-user-agent/1.0',
};
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
userAgent: 'options-user-agent/2.0',
});
expect(resend.userAgent).toBe('options-user-agent/2.0');
process.env = originalEnv;
});
});
describe('fetchRequest with custom options', () => {
it('sends request to custom baseUrl', async () => {
const customBaseUrl = 'https://custom.api.resend.com';
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
baseUrl: customBaseUrl,
});
mockSuccessResponse({ id: 'key-123' }, { headers: {} });
await resend.apiKeys.list();
const [url] = fetchMock.mock.calls[0];
expect(url).toBe(`${customBaseUrl}/api-keys`);
});
it('sends custom User-Agent in request headers', async () => {
const customUserAgent = 'my-integration/3.0';
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', {
userAgent: customUserAgent,
});
mockSuccessResponse({ id: 'key-123' }, { headers: {} });
await resend.apiKeys.list();
const requestOptions = fetchMock.mock.calls[0][1];
const headers = requestOptions?.headers as Headers;
expect(headers.get('User-Agent')).toBe(customUserAgent);
});
});
});