@@ -9,7 +9,7 @@ import type { AnyRouter } from '@orpc/server'
99import { RPCHandler } from '@orpc/server/fetch'
1010import type { ResponseHeadersHandlerPluginContext } from '@orpc/server/plugins'
1111import { ResponseHeadersHandlerPlugin } from '@orpc/server/plugins'
12- import { http , HttpResponse } from 'msw'
12+ import { getResponse , http , HttpResponse } from 'msw'
1313import { setupServer } from 'msw/node'
1414import * as z from 'zod'
1515
@@ -63,7 +63,7 @@ describe('handler', () => {
6363 expect ( planet . discoveredAt ) . toBeInstanceOf ( Date )
6464 } )
6565
66- it ( 'exposes msw resolver info through the context option' , async ( ) => {
66+ it ( 'exposes msw request info through the context option' , async ( ) => {
6767 const fn = vi . fn ( ( { input } : { input : { id : number } } ) => ( {
6868 id : input . id ,
6969 name : 'Earth' ,
@@ -74,20 +74,61 @@ describe('handler', () => {
7474 origin : 'http://localhost:3000' ,
7575 prefix : '/rpc' ,
7676 // clone so the raw body stays readable after input deserialization
77- context : ( info ) => ( { request : info . request . clone ( ) , requestId : info . requestId } ) ,
77+ context : ( info ) => ( { request : info . request . clone ( ) , cookies : info . cookies } ) ,
7878 handler : ( router ) => new RPCHandler ( router ) ,
7979 } )
8080
8181 server . use ( infoORPC . planet . find . handler ( fn ) )
82- await client . planet . find ( { id : 1 } )
82+ await fetch ( 'http://localhost:3000/rpc/planet/find' , {
83+ method : 'POST' ,
84+ headers : { 'content-type' : 'application/json' , 'cookie' : 'session=abc' } ,
85+ body : JSON . stringify ( { json : { id : 1 } } ) ,
86+ } )
8387
8488 const { context } = fn . mock . calls [ 0 ] ! [ 0 ] as any
8589 expect ( context . request ) . toBeInstanceOf ( Request )
8690 expect ( context . request . url ) . toBe ( 'http://localhost:3000/rpc/planet/find' )
87- expect ( context . requestId ) . toBeTypeOf ( 'string' )
91+ expect ( context . cookies ) . toEqual ( { session : 'abc' } )
8892 await expect ( context . request . json ( ) ) . resolves . toEqual ( { json : { id : 1 } } )
8993 } )
9094
95+ it ( 'responds with a response thrown from the context option' , async ( ) => {
96+ const throwingORPC = createHTTPUtils ( contract , {
97+ origin : 'http://localhost:3000' ,
98+ prefix : '/rpc' ,
99+ context : ( ) => {
100+ throw HttpResponse . text ( 'denied' , { status : 401 } )
101+ } ,
102+ handler : ( router ) => new RPCHandler ( router ) ,
103+ } )
104+
105+ const response = await getResponse (
106+ [ throwingORPC . planet . list . handler ( ( ) => [ ] ) ] ,
107+ new Request ( 'http://localhost:3000/rpc/planet/list' , { method : 'POST' } ) ,
108+ )
109+
110+ expect ( response ?. status ) . toBe ( 401 )
111+ await expect ( response ?. text ( ) ) . resolves . toBe ( 'denied' )
112+ } )
113+
114+ it ( 'rethrows other errors thrown from the context option' , async ( ) => {
115+ const throwingORPC = createHTTPUtils ( contract , {
116+ origin : 'http://localhost:3000' ,
117+ prefix : '/rpc' ,
118+ context : ( ) => {
119+ throw new Error ( 'context failed' )
120+ } ,
121+ handler : ( router ) => new RPCHandler ( router ) ,
122+ } )
123+
124+ await expect (
125+ getResponse (
126+ [ throwingORPC . planet . list . handler ( ( ) => [ ] ) ] ,
127+ new Request ( 'http://localhost:3000/rpc/planet/list' , { method : 'POST' } ) ,
128+ ) ,
129+ ) . rejects . toThrow ( 'context failed' )
130+ } )
131+
91132 it ( 'validates input like the real rpc handler' , async ( ) => {
92133 server . use (
93134 orpc . planet . find . handler ( ( { input } ) => ( {
@@ -282,6 +323,26 @@ describe('handler', () => {
282323 await expect ( response . text ( ) ) . resolves . toBe ( 'fallback' )
283324 } )
284325
326+ it ( 'resolves a relative origin against the msw base url' , async ( ) => {
327+ const relativeORPC = createHTTPUtils ( contract , {
328+ origin : '' ,
329+ prefix : '/rpc' ,
330+ handler : ( router ) => new RPCHandler ( router ) ,
331+ } )
332+
333+ const response = await getResponse (
334+ [ relativeORPC . planet . list . handler ( ( ) => [ ] ) ] ,
335+ new Request ( 'http://localhost:3000/rpc/planet/list' , { method : 'POST' } ) ,
336+ { baseUrl : 'http://localhost:3000' } ,
337+ )
338+
339+ await expect ( response ?. json ( ) ) . resolves . toEqual ( { json : [ ] } )
340+ } )
341+
342+ it ( 'labels the handler with its url pattern' , ( ) => {
343+ expect ( orpc . planet . list . handler ( ( ) => [ ] ) . info . header ) . toContain ( 'http://localhost:3000/rpc/*' )
344+ } )
345+
285346 it ( 'leaves unmatched requests to other msw handlers' , async ( ) => {
286347 server . use (
287348 orpc . planet . list . handler ( ( ) => [ ] ) ,
@@ -293,6 +354,20 @@ describe('handler', () => {
293354
294355 await expect ( response . text ( ) ) . resolves . toBe ( 'fallback' )
295356 } )
357+
358+ it ( 'leaves requests for other procedures unhandled' , async ( ) => {
359+ const onUnhandled = vi . fn ( )
360+ server . events . on ( 'request:unhandled' , onUnhandled )
361+
362+ try {
363+ server . use ( orpc . planet . list . handler ( ( ) => [ ] ) )
364+
365+ await expect ( client . planet . find ( { id : 1 } ) ) . rejects . toThrow ( )
366+ expect ( onUnhandled ) . toHaveBeenCalledTimes ( 1 )
367+ } finally {
368+ server . events . removeListener ( 'request:unhandled' , onUnhandled )
369+ }
370+ } )
296371} )
297372
298373describe ( 'error' , ( ) => {
@@ -336,33 +411,28 @@ describe('passthrough', () => {
336411 } )
337412
338413 server . use (
339- realORPC . planet . list . passthrough ( ) ,
340- realORPC . planet . find . handler ( ( { input } ) => ( {
341- id : input . id ,
342- name : 'Earth' ,
343- discoveredAt : new Date ( '2020-01-01' ) ,
344- } ) ) ,
414+ realORPC . planet . find . passthrough ( ) ,
415+ realORPC . planet . list . handler ( ( ) => [ { id : 1 , name : 'Mars' } ] ) ,
345416 )
346417
347- // the body must survive the matching attempt to reach the real server
348- const response = await fetch ( `http://localhost:${ port } /rpc/planet/list` , {
418+ // matching reads the body to decode input, so it must not consume the original
419+ const body = JSON . stringify ( { json : { id : 1 } } )
420+ const response = await fetch ( `http://localhost:${ port } /rpc/planet/find` , {
349421 method : 'POST' ,
350- body : '{}' ,
422+ headers : { 'content-type' : 'application/json' } ,
423+ body,
351424 } )
352425
353- await expect ( response . text ( ) ) . resolves . toBe ( ' real:{}' )
426+ await expect ( response . text ( ) ) . resolves . toBe ( ` real:${ body } ` )
354427
355428 // other procedures fall through the passthrough handler and stay mocked
356- const mocked = await fetch ( `http://localhost:${ port } /rpc/planet/find ` , {
429+ const mocked = await fetch ( `http://localhost:${ port } /rpc/planet/list ` , {
357430 method : 'POST' ,
358431 headers : { 'content-type' : 'application/json' } ,
359- body : JSON . stringify ( { json : { id : 1 } } ) ,
432+ body : '{}' ,
360433 } )
361434
362- await expect ( mocked . json ( ) ) . resolves . toEqual ( {
363- json : { id : 1 , name : 'Earth' , discoveredAt : '2020-01-01T00:00:00.000Z' } ,
364- meta : [ [ 'date' , 'discoveredAt' ] ] ,
365- } )
435+ await expect ( mocked . json ( ) ) . resolves . toEqual ( { json : [ { id : 1 , name : 'Mars' } ] } )
366436 } finally {
367437 realServer . close ( )
368438 }
0 commit comments