Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ async function createPostRequest(
return await getResponse(req)
}

function createRequestBodyForFilepaths(
// Exported for testing.
export function createRequestBodyForFilepaths(
filepaths: string[],
basePath: string
): Array<Array<string | ReadStream>> {
Expand Down
80 changes: 79 additions & 1 deletion test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'node:path'
import nock from 'nock'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { SocketSdk } from '../dist/index.js'
import { SocketSdk, createRequestBodyForFilepaths } from '../dist/index.js'

process.on('unhandledRejection', cause => {
throw new Error('Unhandled rejection', { cause })
Expand Down Expand Up @@ -1185,6 +1185,84 @@ describe('SocketSdk', () => {
})
})

describe('Request Body Formation', () => {
it('should create properly structured request body for file uploads', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'socket-sdk-test-'))
const testFile1 = join(tempDir, 'test1.json')
const testFile2 = join(tempDir, 'test2.json')
const testContent1 = '{"test": 1}'
const testContent2 = '{"test": 2}'

try {
writeFileSync(testFile1, testContent1)
writeFileSync(testFile2, testContent2)

const result = createRequestBodyForFilepaths(
[testFile1, testFile2],
tempDir
)

// Should have 2 entries, each being an array with 3 elements
expect(Array.isArray(result)).toBe(true)
expect(result).toHaveLength(2)

// Check first file entry
expect(Array.isArray(result[0])).toBe(true)
expect(result[0]).toHaveLength(3)

const [contentDisposition1, contentType1, readStream1] = result[0]
expect(typeof contentDisposition1).toBe('string')
expect(contentDisposition1).toContain('Content-Disposition: form-data')
expect(contentDisposition1).toContain('name="test1.json"')
expect(contentDisposition1).toContain('filename="test1.json"')
expect(typeof contentType1).toBe('string')
expect(contentType1).toContain('Content-Type: application/octet-stream')
expect(readStream1).toBeDefined()

// Check second file entry
expect(Array.isArray(result[1])).toBe(true)
expect(result[1]).toHaveLength(3)

const [contentDisposition2, contentType2, readStream2] = result[1]
expect(typeof contentDisposition2).toBe('string')
expect(contentDisposition2).toContain('Content-Disposition: form-data')
expect(contentDisposition2).toContain('name="test2.json"')
expect(contentDisposition2).toContain('filename="test2.json"')
expect(typeof contentType2).toBe('string')
expect(contentType2).toContain('Content-Type: application/octet-stream')
expect(readStream2).toBeDefined()

// Test that the read streams contain the correct content
let streamContent1 = ''
readStream1.on('data', (chunk: Buffer) => {
streamContent1 += chunk.toString()
})

let streamContent2 = ''
readStream2.on('data', (chunk: Buffer) => {
streamContent2 += chunk.toString()
})

await Promise.all([
new Promise<void>(resolve => {
readStream1.on('end', () => {
expect(streamContent1).toBe(testContent1)
resolve()
})
}),
new Promise<void>(resolve => {
readStream2.on('end', () => {
expect(streamContent2).toBe(testContent2)
resolve()
})
})
])
} finally {
rmSync(tempDir, { recursive: true, force: true })
}
})
})

describe('Error Handling Edge Cases', () => {
it('should handle 429 rate limit errors', async () => {
nock('https://api.socket.dev')
Expand Down
Loading