@@ -5,7 +5,7 @@ import { join } from 'node:path'
55import nock from 'nock'
66import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
77
8- import { SocketSdk } from '../dist/index.js'
8+ import { SocketSdk , createRequestBodyForFilepaths } from '../dist/index.js'
99
1010process . on ( 'unhandledRejection' , cause => {
1111 throw new Error ( 'Unhandled rejection' , { cause } )
@@ -1185,6 +1185,84 @@ describe('SocketSdk', () => {
11851185 } )
11861186 } )
11871187
1188+ describe ( 'Request Body Formation' , ( ) => {
1189+ it ( 'should create properly structured request body for file uploads' , async ( ) => {
1190+ const tempDir = mkdtempSync ( join ( tmpdir ( ) , 'socket-sdk-test-' ) )
1191+ const testFile1 = join ( tempDir , 'test1.json' )
1192+ const testFile2 = join ( tempDir , 'test2.json' )
1193+ const testContent1 = '{"test": 1}'
1194+ const testContent2 = '{"test": 2}'
1195+
1196+ try {
1197+ writeFileSync ( testFile1 , testContent1 )
1198+ writeFileSync ( testFile2 , testContent2 )
1199+
1200+ const result = createRequestBodyForFilepaths (
1201+ [ testFile1 , testFile2 ] ,
1202+ tempDir
1203+ )
1204+
1205+ // Should have 2 entries, each being an array with 3 elements
1206+ expect ( Array . isArray ( result ) ) . toBe ( true )
1207+ expect ( result ) . toHaveLength ( 2 )
1208+
1209+ // Check first file entry
1210+ expect ( Array . isArray ( result [ 0 ] ) ) . toBe ( true )
1211+ expect ( result [ 0 ] ) . toHaveLength ( 3 )
1212+
1213+ const [ contentDisposition1 , contentType1 , readStream1 ] = result [ 0 ]
1214+ expect ( typeof contentDisposition1 ) . toBe ( 'string' )
1215+ expect ( contentDisposition1 ) . toContain ( 'Content-Disposition: form-data' )
1216+ expect ( contentDisposition1 ) . toContain ( 'name="test1.json"' )
1217+ expect ( contentDisposition1 ) . toContain ( 'filename="test1.json"' )
1218+ expect ( typeof contentType1 ) . toBe ( 'string' )
1219+ expect ( contentType1 ) . toContain ( 'Content-Type: application/octet-stream' )
1220+ expect ( readStream1 ) . toBeDefined ( )
1221+
1222+ // Check second file entry
1223+ expect ( Array . isArray ( result [ 1 ] ) ) . toBe ( true )
1224+ expect ( result [ 1 ] ) . toHaveLength ( 3 )
1225+
1226+ const [ contentDisposition2 , contentType2 , readStream2 ] = result [ 1 ]
1227+ expect ( typeof contentDisposition2 ) . toBe ( 'string' )
1228+ expect ( contentDisposition2 ) . toContain ( 'Content-Disposition: form-data' )
1229+ expect ( contentDisposition2 ) . toContain ( 'name="test2.json"' )
1230+ expect ( contentDisposition2 ) . toContain ( 'filename="test2.json"' )
1231+ expect ( typeof contentType2 ) . toBe ( 'string' )
1232+ expect ( contentType2 ) . toContain ( 'Content-Type: application/octet-stream' )
1233+ expect ( readStream2 ) . toBeDefined ( )
1234+
1235+ // Test that the read streams contain the correct content
1236+ let streamContent1 = ''
1237+ readStream1 . on ( 'data' , ( chunk : Buffer ) => {
1238+ streamContent1 += chunk . toString ( )
1239+ } )
1240+
1241+ let streamContent2 = ''
1242+ readStream2 . on ( 'data' , ( chunk : Buffer ) => {
1243+ streamContent2 += chunk . toString ( )
1244+ } )
1245+
1246+ await Promise . all ( [
1247+ new Promise < void > ( resolve => {
1248+ readStream1 . on ( 'end' , ( ) => {
1249+ expect ( streamContent1 ) . toBe ( testContent1 )
1250+ resolve ( )
1251+ } )
1252+ } ) ,
1253+ new Promise < void > ( resolve => {
1254+ readStream2 . on ( 'end' , ( ) => {
1255+ expect ( streamContent2 ) . toBe ( testContent2 )
1256+ resolve ( )
1257+ } )
1258+ } )
1259+ ] )
1260+ } finally {
1261+ rmSync ( tempDir , { recursive : true , force : true } )
1262+ }
1263+ } )
1264+ } )
1265+
11881266 describe ( 'Error Handling Edge Cases' , ( ) => {
11891267 it ( 'should handle 429 rate limit errors' , async ( ) => {
11901268 nock ( 'https://api.socket.dev' )
0 commit comments