Skip to content

Commit 46c6686

Browse files
committed
test(sdk): add HTTP status code error handling tests
Add tests for 400, 413, and 429 status codes with actionable guidance. Tests verify error messages include specific remediation steps. Coverage improvements: - socket-sdk-class.ts statements: 70.41% → 71.28% (+0.87%) - socket-sdk-class.ts branches: 53.68% → 55.32% (+1.64%) - Overall statements: 75.49% → 76.02% (+0.53%) - Overall branches: 61.39% → 62.32% (+0.93%) - Cumulative coverage: 87.53% → 87.75% (+0.22%) - Total tests: 471 → 474 (+3 tests)
1 parent 6ad44f7 commit 46c6686

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/** @fileoverview Tests for Socket SDK HTTP status code error handling. */
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+
14+
describe('SocketSdk - HTTP Status Code Handling', () => {
15+
const getBaseUrl = setupLocalHttpServer(
16+
createRouteHandler({
17+
'/400-bad-request': jsonResponse(400, {
18+
error: 'Bad Request',
19+
message: 'Invalid parameters provided',
20+
}),
21+
'/413-payload-too-large': jsonResponse(413, {
22+
error: 'Payload Too Large',
23+
message: 'Request body exceeds maximum size',
24+
}),
25+
'/429-with-retry-after': (_req, res) => {
26+
res.writeHead(429, {
27+
'Content-Type': 'application/json',
28+
'Retry-After': '60',
29+
})
30+
res.end(JSON.stringify({ error: 'Rate limit exceeded' }))
31+
},
32+
}),
33+
)
34+
35+
const getClient = () =>
36+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 0 })
37+
38+
describe('400 Bad Request', () => {
39+
it('should provide actionable guidance for 400 errors', async () => {
40+
const result = (await getClient().getApi('/400-bad-request', {
41+
throws: false,
42+
})) as SocketSdkGenericResult<unknown>
43+
44+
expect(result.success).toBe(false)
45+
if (!result.success) {
46+
expect(result.status).toBe(400)
47+
expect(result.cause).toContain('Bad request')
48+
expect(result.cause).toContain('Invalid parameters')
49+
expect(result.cause).toContain('required parameters are provided')
50+
}
51+
})
52+
})
53+
54+
describe('413 Payload Too Large', () => {
55+
it('should provide actionable guidance for 413 errors', async () => {
56+
const result = (await getClient().getApi('/413-payload-too-large', {
57+
throws: false,
58+
})) as SocketSdkGenericResult<unknown>
59+
60+
expect(result.success).toBe(false)
61+
if (!result.success) {
62+
expect(result.status).toBe(413)
63+
expect(result.cause).toContain('Payload too large')
64+
expect(result.cause).toContain('Request exceeds size limits')
65+
expect(result.cause).toContain('Reduce the number of files')
66+
}
67+
})
68+
})
69+
70+
describe('429 Rate Limit with Retry-After', () => {
71+
it('should parse Retry-After header and include in guidance', async () => {
72+
const result = (await getClient().getApi('/429-with-retry-after', {
73+
throws: false,
74+
})) as SocketSdkGenericResult<unknown>
75+
76+
expect(result.success).toBe(false)
77+
if (!result.success) {
78+
expect(result.status).toBe(429)
79+
expect(result.cause).toContain('Rate limit exceeded')
80+
expect(result.cause).toContain('Retry after 60 seconds')
81+
}
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)