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,24 +12,27 @@ 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 ( path : string , endpoints : ReactiveEndpoint [ ] , responder : SocketResponder , request ?: Partial < ReactiveStreamRequest > ) {
1616 return handleReactiveStream < { } > (
1717 { } ,
1818 {
1919 payload : {
2020 data : Buffer . from ( serialize ( { } ) ) ,
21- metadata : Buffer . from ( serialize ( { path } ) )
21+ metadata : Buffer . from ( serialize ( { path } ) ) ,
2222 } ,
2323 initialN : 1 ,
24- responder
24+ dataMimeType : 'application/bson' ,
25+ metadataMimeType : 'application/bson' ,
26+ responder,
27+ ...request ,
2528 } ,
2629 createMockObserver ( ) ,
2730 new AbortController ( ) ,
2831 {
2932 contextProvider : async ( ) => ( { } ) ,
3033 endpoints,
31- metaDecoder : async ( buffer ) => deserialize ( buffer ) as RequestMeta ,
32- payloadDecoder : async ( buffer ) => buffer && deserialize ( buffer )
34+ metaDecoder : async ( buffer ) => deserialize ( buffer . contents ) as RequestMeta ,
35+ payloadDecoder : async ( buffer ) => buffer && deserialize ( buffer . contents )
3336 }
3437 ) ;
3538}
@@ -133,4 +136,53 @@ describe('Requests', () => {
133136 // Should be a validation error
134137 expect ( JSON . stringify ( spy . mock . calls [ 0 ] ) ) . includes ( ErrorCode . PSYNC_S2002 ) ;
135138 } ) ;
139+
140+ it ( 'should forward mime types' , async ( ) => {
141+ const encoder = new TextEncoder ( ) ;
142+ const decoder = new TextDecoder ( ) ;
143+ const responder = createMockResponder ( ) ;
144+ const encodeJson = ( value : any ) => encoder . encode ( JSON . stringify ( value ) ) ;
145+ const path = '/test-route' ;
146+
147+ const fn = vi . fn ( async ( p : EndpointHandlerPayload < any , any > ) => {
148+ expect ( p . params ) . toStrictEqual ( { 'hello' : 'world' } ) ;
149+ return undefined ;
150+ } ) ;
151+
152+ await handleReactiveStream < { } > (
153+ { } ,
154+ {
155+ payload : {
156+ data : Buffer . from ( encodeJson ( { 'hello' : 'world' } ) ) ,
157+ metadata : Buffer . from ( encodeJson ( { path } ) ) ,
158+ } ,
159+ metadataMimeType : 'application/json' ,
160+ dataMimeType : 'application/json' ,
161+ initialN : 1 ,
162+ responder,
163+ } ,
164+ createMockObserver ( ) ,
165+ new AbortController ( ) ,
166+ {
167+ contextProvider : async ( ) => ( { } ) ,
168+ endpoints : [
169+ {
170+ path,
171+ type : RS_ENDPOINT_TYPE . STREAM ,
172+ handler : fn ,
173+ }
174+ ] ,
175+ metaDecoder : async ( buffer ) => {
176+ expect ( buffer . mimeType , 'application/json' ) ;
177+ return JSON . parse ( decoder . decode ( buffer . contents ) ) ;
178+ } ,
179+ payloadDecoder : async ( buffer ) => {
180+ expect ( buffer ! . mimeType , 'application/json' ) ;
181+ return JSON . parse ( decoder . decode ( buffer ! . contents ) ) ;
182+ }
183+ }
184+ ) ;
185+
186+ expect ( fn ) . toHaveBeenCalled ( ) ;
187+ } ) ;
136188} ) ;
0 commit comments