1
1
import { describe , expect , it , vi } from 'vitest' ;
2
2
import { createMockObserver , createMockResponder } from './utils/mock-responder.js' ;
3
- import { handleReactiveStream } from '../../src/router/ReactiveSocketRouter.js' ;
3
+ import { handleReactiveStream , ReactiveStreamRequest } from '../../src/router/ReactiveSocketRouter.js' ;
4
4
import { deserialize , serialize } from 'bson' ;
5
5
import { 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' ;
7
7
8
8
/**
9
9
* Mocks the process of handling reactive routes
@@ -12,7 +12,12 @@ import { ErrorCode } from '@powersync/lib-services-framework';
12
12
* @param responder a mock responder
13
13
* @returns
14
14
*/
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
+ ) {
16
21
return handleReactiveStream < { } > (
17
22
{ } ,
18
23
{
@@ -21,15 +26,18 @@ async function handleRoute(path: string, endpoints: ReactiveEndpoint[], responde
21
26
metadata : Buffer . from ( serialize ( { path } ) )
22
27
} ,
23
28
initialN : 1 ,
24
- responder
29
+ dataMimeType : 'application/bson' ,
30
+ metadataMimeType : 'application/bson' ,
31
+ responder,
32
+ ...request
25
33
} ,
26
34
createMockObserver ( ) ,
27
35
new AbortController ( ) ,
28
36
{
29
37
contextProvider : async ( ) => ( { } ) ,
30
38
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 )
33
41
}
34
42
) ;
35
43
}
@@ -133,4 +141,53 @@ describe('Requests', () => {
133
141
// Should be a validation error
134
142
expect ( JSON . stringify ( spy . mock . calls [ 0 ] ) ) . includes ( ErrorCode . PSYNC_S2002 ) ;
135
143
} ) ;
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
+ } ) ;
136
193
} ) ;
0 commit comments