Skip to content

Commit 50d1a7e

Browse files
committed
test(sdk): add error branch coverage tests
Add tests for error handling and method variations: - 401/403 errors with no retry behavior - 404 error handling - sendApi with POST/PUT/PATCH methods and empty body These tests validate error handling paths and HTTP method variations to improve branch coverage.
1 parent 12dad58 commit 50d1a7e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/** @fileoverview Tests for uncovered error branches in Socket SDK. */
2+
3+
import { describe, expect, it } from 'vitest'
4+
5+
import { SocketSdk } from '../../src/index'
6+
import {
7+
createRouteHandler,
8+
jsonResponse,
9+
setupLocalHttpServer,
10+
} from '../utils/local-server-helpers.mts'
11+
12+
import type { SocketSdkGenericResult } from '../../src/types'
13+
import type { IncomingMessage } from 'node:http'
14+
15+
describe('SocketSdk - Error Branch Coverage', () => {
16+
describe('401/403 errors (no retry)', () => {
17+
const getBaseUrl = setupLocalHttpServer(
18+
createRouteHandler({
19+
'/401-test': jsonResponse(401, { error: 'Unauthorized' }),
20+
'/403-test': jsonResponse(403, { error: 'Forbidden' }),
21+
}),
22+
)
23+
24+
const getClient = () =>
25+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 3 })
26+
27+
it('should not retry 401 errors', async () => {
28+
const result = (await getClient().getApi('/401-test', {
29+
throws: false,
30+
})) as SocketSdkGenericResult<unknown>
31+
32+
expect(result.success).toBe(false)
33+
if (!result.success) {
34+
expect(result.status).toBe(401)
35+
expect(result.cause).toContain('Authentication failed')
36+
}
37+
})
38+
39+
it('should not retry 403 errors', async () => {
40+
const result = (await getClient().getApi('/403-test', {
41+
throws: false,
42+
})) as SocketSdkGenericResult<unknown>
43+
44+
expect(result.success).toBe(false)
45+
if (!result.success) {
46+
expect(result.status).toBe(403)
47+
expect(result.cause).toContain('Authorization failed')
48+
}
49+
})
50+
})
51+
52+
describe('404 errors', () => {
53+
const getBaseUrl = setupLocalHttpServer(
54+
createRouteHandler({
55+
'/404-test': jsonResponse(404, { error: 'Not Found' }),
56+
}),
57+
)
58+
59+
const getClient = () =>
60+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 0 })
61+
62+
it('should handle 404 errors', async () => {
63+
const result = (await getClient().getApi('/404-test', {
64+
throws: false,
65+
})) as SocketSdkGenericResult<unknown>
66+
67+
expect(result.success).toBe(false)
68+
if (!result.success) {
69+
expect(result.status).toBe(404)
70+
expect(result.cause).toContain('Resource not found')
71+
}
72+
})
73+
})
74+
75+
describe('sendApi with different methods', () => {
76+
const getBaseUrl = setupLocalHttpServer(
77+
createRouteHandler({
78+
'/test-endpoint': (_req: IncomingMessage, res) => {
79+
res.writeHead(200, { 'Content-Type': 'application/json' })
80+
res.end(JSON.stringify({ success: true, data: { id: 123 } }))
81+
},
82+
}),
83+
)
84+
85+
const getClient = () =>
86+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 0 })
87+
88+
it('should handle sendApi with POST method', async () => {
89+
const result = await getClient().sendApi('/test-endpoint', {
90+
body: { test: 'data' },
91+
method: 'POST',
92+
})
93+
94+
expect(result.success).toBe(true)
95+
})
96+
97+
it('should handle sendApi with PUT method', async () => {
98+
const result = await getClient().sendApi('/test-endpoint', {
99+
body: { test: 'data' },
100+
method: 'PUT',
101+
})
102+
103+
expect(result.success).toBe(true)
104+
})
105+
106+
it('should handle sendApi with empty body', async () => {
107+
const result = await getClient().sendApi('/test-endpoint', {
108+
body: {},
109+
method: 'POST',
110+
})
111+
112+
expect(result.success).toBe(true)
113+
})
114+
115+
it('should handle sendApi with null in body', async () => {
116+
const result = (await getClient().sendApi('/test-endpoint', {
117+
body: { value: null },
118+
method: 'POST',
119+
})) as SocketSdkGenericResult<unknown>
120+
121+
expect(result.success).toBe(true)
122+
})
123+
})
124+
})

0 commit comments

Comments
 (0)