|
| 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