File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -108,7 +108,7 @@ function fastifyIp (
108
108
done ( )
109
109
}
110
110
111
- instance . addHook ( 'preHandler ' , redefineIpDecorator )
111
+ instance . addHook ( 'onRequest ' , redefineIpDecorator )
112
112
} else {
113
113
instance . decorateRequest ( 'ip' , ipDecorator )
114
114
}
Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ tap.test('Plugin#Decoration', scope => {
112
112
} )
113
113
114
114
tap . test ( 'Plugin#Request IP' , scope => {
115
- scope . plan ( 9 )
115
+ scope . plan ( 10 )
116
116
117
117
scope . test ( 'Should infer the header based on default priority' , async t => {
118
118
const app = fastify ( )
@@ -508,4 +508,26 @@ tap.test('Plugin#Request IP', scope => {
508
508
}
509
509
} )
510
510
} )
511
+
512
+ scope . test ( 'Should set the IP in the "onRequest" hook when trustProxy option is set to true' , async t => {
513
+ const app = fastify ( { trustProxy : true } )
514
+ const expectedIP = faker . internet . ip ( )
515
+
516
+ app . register ( plugin )
517
+ app . addHook ( 'onRequest' , async ( req ) => {
518
+ t . equal ( req . ip , expectedIP )
519
+ } )
520
+ app . get ( '/' , async ( req ) => {
521
+ t . equal ( req . ip , expectedIP )
522
+ } )
523
+
524
+ t . plan ( 2 )
525
+
526
+ await app . inject ( {
527
+ path : '/' ,
528
+ headers : {
529
+ 'x-client-ip' : expectedIP
530
+ }
531
+ } )
532
+ } )
511
533
} )
You can’t perform that action at this time.
0 commit comments