11import { describe , expect , it , vi } from 'vitest' ;
22import { createMockObserver , createMockResponder } from './utils/mock-responder.js' ;
3- import { handleReactiveStream } from '../../src/router/ReactiveSocketRouter.js' ;
3+ import { handleReactiveStream , ReactiveStreamRequest } from '../../src/router/ReactiveSocketRouter.js' ;
44import { deserialize , serialize } from 'bson' ;
55import { RS_ENDPOINT_TYPE , ReactiveEndpoint , RequestMeta , SocketResponder } from '../../src/router/types.js' ;
6- import { ErrorCode } from '@powersync/lib-services-framework' ;
6+ import { EndpointHandlerPayload , ErrorCode } from '@powersync/lib-services-framework' ;
77
88/**
99 * Mocks the process of handling reactive routes
@@ -12,7 +12,12 @@ import { ErrorCode } from '@powersync/lib-services-framework';
1212 * @param responder a mock responder
1313 * @returns
1414 */
15- async function handleRoute ( path : string , endpoints : ReactiveEndpoint [ ] , responder : SocketResponder ) {
15+ async function handleRoute (
16+ path : string ,
17+ endpoints : ReactiveEndpoint [ ] ,
18+ responder : SocketResponder ,
19+ request ?: Partial < ReactiveStreamRequest >
20+ ) {
1621 return handleReactiveStream < { } > (
1722 { } ,
1823 {
@@ -21,15 +26,18 @@ async function handleRoute(path: string, endpoints: ReactiveEndpoint[], responde
2126 metadata : Buffer . from ( serialize ( { path } ) )
2227 } ,
2328 initialN : 1 ,
24- responder
29+ dataMimeType : 'application/bson' ,
30+ metadataMimeType : 'application/bson' ,
31+ responder,
32+ ...request
2533 } ,
2634 createMockObserver ( ) ,
2735 new AbortController ( ) ,
2836 {
2937 contextProvider : async ( ) => ( { } ) ,
3038 endpoints,
31- metaDecoder : async ( buffer ) => deserialize ( buffer ) as RequestMeta ,
32- payloadDecoder : async ( buffer ) => buffer && deserialize ( buffer )
39+ metaDecoder : async ( buffer ) => deserialize ( buffer . contents ) as RequestMeta ,
40+ payloadDecoder : async ( buffer ) => buffer && deserialize ( buffer . contents )
3341 }
3442 ) ;
3543}
@@ -133,4 +141,53 @@ describe('Requests', () => {
133141 // Should be a validation error
134142 expect ( JSON . stringify ( spy . mock . calls [ 0 ] ) ) . includes ( ErrorCode . PSYNC_S2002 ) ;
135143 } ) ;
144+
145+ it ( 'should forward mime types' , async ( ) => {
146+ const encoder = new TextEncoder ( ) ;
147+ const decoder = new TextDecoder ( ) ;
148+ const responder = createMockResponder ( ) ;
149+ const encodeJson = ( value : any ) => encoder . encode ( JSON . stringify ( value ) ) ;
150+ const path = '/test-route' ;
151+
152+ const fn = vi . fn ( async ( p : EndpointHandlerPayload < any , any > ) => {
153+ expect ( p . params ) . toStrictEqual ( { hello : 'world' } ) ;
154+ return undefined ;
155+ } ) ;
156+
157+ await handleReactiveStream < { } > (
158+ { } ,
159+ {
160+ payload : {
161+ data : Buffer . from ( encodeJson ( { hello : 'world' } ) ) ,
162+ metadata : Buffer . from ( encodeJson ( { path } ) )
163+ } ,
164+ metadataMimeType : 'application/json' ,
165+ dataMimeType : 'application/json' ,
166+ initialN : 1 ,
167+ responder
168+ } ,
169+ createMockObserver ( ) ,
170+ new AbortController ( ) ,
171+ {
172+ contextProvider : async ( ) => ( { } ) ,
173+ endpoints : [
174+ {
175+ path,
176+ type : RS_ENDPOINT_TYPE . STREAM ,
177+ handler : fn
178+ }
179+ ] ,
180+ metaDecoder : async ( buffer ) => {
181+ expect ( buffer . mimeType , 'application/json' ) ;
182+ return JSON . parse ( decoder . decode ( buffer . contents ) ) ;
183+ } ,
184+ payloadDecoder : async ( buffer ) => {
185+ expect ( buffer ! . mimeType , 'application/json' ) ;
186+ return JSON . parse ( decoder . decode ( buffer ! . contents ) ) ;
187+ }
188+ }
189+ ) ;
190+
191+ expect ( fn ) . toHaveBeenCalled ( ) ;
192+ } ) ;
136193} ) ;
0 commit comments