Skip to content

Commit 28c6306

Browse files
committed
test: add error details parsing and HTTP 413 tests
Add tests for API error response parsing: - String error details extraction (lines 480, 483) - Object error details serialization - 413 Payload Too Large error handling (line 547) Improves coverage of error message construction in #handleApiError
1 parent ac8eac0 commit 28c6306

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/** @fileoverview Tests for error details parsing and specific HTTP error codes. */
2+
3+
import { describe, expect, it } from 'vitest'
4+
5+
import { SocketSdk } from '../../src/index'
6+
import {
7+
createRouteHandler,
8+
setupLocalHttpServer,
9+
} from '../utils/local-server-helpers.mts'
10+
11+
import type { SocketSdkGenericResult } from '../../src/types'
12+
import type { IncomingMessage } from 'node:http'
13+
14+
describe('SocketSdk - Error Details and HTTP Codes', () => {
15+
describe('Error details parsing', () => {
16+
const getBaseUrl = setupLocalHttpServer(
17+
createRouteHandler({
18+
'/error-with-string-details': (_req: IncomingMessage, res) => {
19+
res.writeHead(400, { 'Content-Type': 'application/json' })
20+
res.end(
21+
JSON.stringify({
22+
error: {
23+
message: 'Validation failed',
24+
details: 'Package name is required',
25+
},
26+
}),
27+
)
28+
},
29+
'/error-with-object-details': (_req: IncomingMessage, res) => {
30+
res.writeHead(400, { 'Content-Type': 'application/json' })
31+
res.end(
32+
JSON.stringify({
33+
error: {
34+
message: 'Validation failed',
35+
details: { field: 'packageName', reason: 'required' },
36+
},
37+
}),
38+
)
39+
},
40+
}),
41+
)
42+
43+
const getClient = () =>
44+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 0 })
45+
46+
it('should parse string error details', async () => {
47+
const result = (await getClient().getApi('/error-with-string-details', {
48+
throws: false,
49+
})) as SocketSdkGenericResult<unknown>
50+
51+
expect(result.success).toBe(false)
52+
if (!result.success) {
53+
expect(result.status).toBe(400)
54+
expect(result.cause).toContain('Details: Package name is required')
55+
}
56+
})
57+
58+
it('should parse object error details', async () => {
59+
const result = (await getClient().getApi('/error-with-object-details', {
60+
throws: false,
61+
})) as SocketSdkGenericResult<unknown>
62+
63+
expect(result.success).toBe(false)
64+
if (!result.success) {
65+
expect(result.status).toBe(400)
66+
expect(result.cause).toContain('Details:')
67+
expect(result.cause).toContain('packageName')
68+
}
69+
})
70+
})
71+
72+
describe('413 Payload Too Large', () => {
73+
const getBaseUrl = setupLocalHttpServer(
74+
createRouteHandler({
75+
'/too-large': (_req: IncomingMessage, res) => {
76+
res.writeHead(413, { 'Content-Type': 'application/json' })
77+
res.end(JSON.stringify({ error: { message: 'Payload too large' } }))
78+
},
79+
}),
80+
)
81+
82+
const getClient = () =>
83+
new SocketSdk('test-token', { baseUrl: getBaseUrl(), retries: 0 })
84+
85+
it('should handle 413 Payload Too Large error', async () => {
86+
const result = (await getClient().getApi('/too-large', {
87+
throws: false,
88+
})) as SocketSdkGenericResult<unknown>
89+
90+
expect(result.success).toBe(false)
91+
if (!result.success) {
92+
expect(result.status).toBe(413)
93+
expect(result.cause).toContain('Payload too large')
94+
expect(result.cause).toContain('Reduce the number')
95+
}
96+
})
97+
})
98+
})

0 commit comments

Comments
 (0)