11import { Injector , inject } from '@angular/core' ;
2- import { RESPONSE } from '@analogjs/router/tokens' ;
2+ import { REQUEST , RESPONSE , type ServerRequest } from '@analogjs/router/tokens' ;
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44
55// Keep h3 real except `readBody`, whose parse-failure path is what the
@@ -22,32 +22,19 @@ import { handleServerFnRequest } from './event-handler';
2222import { serverFnRegistry } from './registry' ;
2323import { serverFn } from './server-fn' ;
2424
25- type FakeRes = {
26- statusCode : number ;
27- headers : Record < string , string | string [ ] > ;
28- setHeader ( key : string , value : string | string [ ] ) : void ;
29- } ;
30-
3125function fakeEvent ( options : {
3226 id : string ;
3327 method ?: string ;
3428 body ?: unknown ;
3529 bodyThrows ?: boolean ;
3630 headers ?: Record < string , string > ;
3731} ) {
38- const res : FakeRes = {
39- statusCode : 200 ,
40- headers : { } ,
41- setHeader ( key , value ) {
42- this . headers [ key ] = value ;
43- } ,
44- } ;
4532 return {
4633 method : options . method ?? 'GET' ,
4734 context : { params : { id : options . id } } ,
4835 node : {
4936 req : { headers : options . headers ?? { } } ,
50- res,
37+ res : { } ,
5138 } ,
5239 // `readBody(event)` reads `event._requestBody` under h3's unenv shim in
5340 // tests; model it (and the throwing case) directly.
@@ -63,19 +50,16 @@ describe('handleServerFnRequest', () => {
6350
6451 const jsonHeaders = { 'content-type' : 'application/json' } ;
6552
66- it ( 'dispatches a GET and writes the status and body' , async ( ) => {
53+ it ( 'dispatches a GET and returns the status and body as a Response ' , async ( ) => {
6754 serverFn ( { id : 'read' } , async ( ) => ( { ok : true } ) ) ;
6855
69- const event = fakeEvent ( { id : 'read' , method : 'GET' } ) ;
70- const body = await handleServerFnRequest (
71- event ,
56+ const response = await handleServerFnRequest (
57+ fakeEvent ( { id : 'read' , method : 'GET' } ) ,
7258 Injector . create ( { providers : [ ] } ) ,
7359 ) ;
7460
75- expect ( body ) . toEqual ( { ok : true } ) ;
76- expect (
77- ( event as never as { node : { res : FakeRes } } ) . node . res . statusCode ,
78- ) . toBe ( 200 ) ;
61+ expect ( response . status ) . toBe ( 200 ) ;
62+ expect ( await response . json ( ) ) . toEqual ( { ok : true } ) ;
7963 } ) ;
8064
8165 it ( 'accepts a promised injector, awaiting it before dispatch' , async ( ) => {
@@ -84,44 +68,76 @@ describe('handleServerFnRequest', () => {
8468 const event = fakeEvent ( { id : 'read2' , method : 'GET' } ) ;
8569 const injector = Promise . resolve ( Injector . create ( { providers : [ ] } ) ) ;
8670
87- await expect ( handleServerFnRequest ( event , injector ) ) . resolves . toBe ( 'ok' ) ;
71+ const response = await handleServerFnRequest ( event , injector ) ;
72+ expect ( await response . json ( ) ) . toBe ( 'ok' ) ;
8873 } ) ;
8974
90- it ( 'propagates response headers onto the h3 response ' , async ( ) => {
75+ it ( 'carries a returned Response status and headers on the Node path ' , async ( ) => {
9176 serverFn ( { id : 'redir' } , async ( ) => {
9277 return new Response ( null , {
93- status : 302 ,
78+ status : 303 ,
9479 headers : { Location : '/next' } ,
9580 } ) ;
9681 } ) ;
9782
98- const event = fakeEvent ( { id : 'redir' , method : 'GET' } ) ;
99- const res = ( event as never as { node : { res : FakeRes } } ) . node . res ;
83+ const response = await handleServerFnRequest (
84+ fakeEvent ( { id : 'redir' , method : 'GET' } ) ,
85+ Injector . create ( { providers : [ ] } ) ,
86+ ) ;
87+
88+ expect ( response . status ) . toBe ( 303 ) ;
89+ expect ( response . headers . get ( 'location' ) ) . toBe ( '/next' ) ;
90+ } ) ;
91+
92+ it ( 'answers invalid input with 422 on the Node path' , async ( ) => {
93+ serverFn (
94+ {
95+ id : 'validated' ,
96+ method : 'POST' ,
97+ input : {
98+ '~standard' : {
99+ version : 1 ,
100+ vendor : 'spec' ,
101+ validate : ( ) => ( { issues : [ { message : 'invalid' } ] } ) ,
102+ } ,
103+ } ,
104+ } ,
105+ async ( ) => 'never' ,
106+ ) ;
100107
101- await handleServerFnRequest ( event , Injector . create ( { providers : [ ] } ) ) ;
108+ const response = await handleServerFnRequest (
109+ fakeEvent ( {
110+ id : 'validated' ,
111+ method : 'POST' ,
112+ body : { } ,
113+ headers : jsonHeaders ,
114+ } ) ,
115+ Injector . create ( { providers : [ ] } ) ,
116+ ) ;
102117
103- expect ( res . statusCode ) . toBe ( 302 ) ;
104- expect ( res . headers [ 'location' ] ) . toBe ( '/next' ) ;
118+ expect ( response . status ) . toBe ( 422 ) ;
119+ expect ( await response . json ( ) ) . toEqual ( {
120+ errors : [ { message : 'invalid' } ] ,
121+ } ) ;
105122 } ) ;
106123
107124 it ( 'returns 400 with the JSON error contract when the body is malformed' , async ( ) => {
108125 serverFn ( { id : 'write' , method : 'POST' } , async ( ) => 'never' ) ;
109126
110- const event = fakeEvent ( {
111- id : 'write' ,
112- method : 'POST' ,
113- bodyThrows : true ,
114- headers : jsonHeaders ,
115- } ) ;
116- const res = ( event as never as { node : { res : FakeRes } } ) . node . res ;
117-
118- const body = await handleServerFnRequest (
119- event ,
127+ const response = await handleServerFnRequest (
128+ fakeEvent ( {
129+ id : 'write' ,
130+ method : 'POST' ,
131+ bodyThrows : true ,
132+ headers : jsonHeaders ,
133+ } ) ,
120134 Injector . create ( { providers : [ ] } ) ,
121135 ) ;
122136
123- expect ( res . statusCode ) . toBe ( 400 ) ;
124- expect ( body ) . toEqual ( { message : 'Malformed request body' } ) ;
137+ expect ( response . status ) . toBe ( 400 ) ;
138+ expect ( await response . json ( ) ) . toEqual ( {
139+ message : 'Malformed request body' ,
140+ } ) ;
125141 } ) ;
126142
127143 it ( 'serves events without a Node request from their fetch Request' , async ( ) => {
@@ -130,7 +146,7 @@ describe('handleServerFnRequest', () => {
130146 return ( input as { text : string } ) . text . toUpperCase ( ) ;
131147 } ) ;
132148
133- const response = ( await handleServerFnRequest (
149+ const response = await handleServerFnRequest (
134150 {
135151 method : 'POST' ,
136152 context : { params : { id : 'shout' } } ,
@@ -141,10 +157,35 @@ describe('handleServerFnRequest', () => {
141157 } ) ,
142158 } as never ,
143159 Injector . create ( { providers : [ ] } ) ,
144- ) ) as Response ;
160+ ) ;
145161
146162 expect ( response . status ) . toBe ( 200 ) ;
147163 expect ( response . headers . get ( 'x-shout' ) ) . toBe ( 'yes' ) ;
148164 expect ( await response . json ( ) ) . toBe ( 'HI' ) ;
149165 } ) ;
166+
167+ it ( 'exposes the fetch Request and its abort signal on REQUEST' , async ( ) => {
168+ serverFn ( { id : 'aborted' } , async ( ) => {
169+ const req = inject ( REQUEST ) as ServerRequest & { request : Request } ;
170+ return { aborted : req . signal . aborted , url : req . request . url } ;
171+ } ) ;
172+ const controller = new AbortController ( ) ;
173+ controller . abort ( ) ;
174+
175+ const response = await handleServerFnRequest (
176+ {
177+ method : 'GET' ,
178+ context : { params : { id : 'aborted' } } ,
179+ req : new Request ( 'http://localhost/_analog/fn/aborted' , {
180+ signal : controller . signal ,
181+ } ) ,
182+ } as never ,
183+ Injector . create ( { providers : [ ] } ) ,
184+ ) ;
185+
186+ expect ( await response . json ( ) ) . toEqual ( {
187+ aborted : true ,
188+ url : 'http://localhost/_analog/fn/aborted' ,
189+ } ) ;
190+ } ) ;
150191} ) ;
0 commit comments