@@ -32,7 +32,6 @@ describe('when userResHeaderDecorator is defined', function () {
3232 } ) ;
3333
3434 it ( 'can delete a header' , function ( done ) {
35-
3635 app . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
3736 userResHeaderDecorator : function ( headers /*, userReq, userRes, proxyReq, proxyRes */ ) {
3837 delete headers [ 'x-my-secret-header' ] ;
@@ -54,7 +53,6 @@ describe('when userResHeaderDecorator is defined', function () {
5453 } ) ;
5554
5655 it ( 'provides an interface for updating headers' , function ( done ) {
57-
5856 app . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
5957 userResHeaderDecorator : function ( headers /*, userReq, userRes, proxyReq, proxyRes */ ) {
6058 headers . boltedonheader = 'franky' ;
@@ -74,4 +72,39 @@ describe('when userResHeaderDecorator is defined', function () {
7472 . end ( done ) ;
7573 } ) ;
7674
75+ it ( 'author has option to copy proxyResponse headers to userResponse' , function ( done ) {
76+ app . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
77+ userResHeaderDecorator : function ( headers , userReq ) { // proxyReq
78+ // Copy specific headers from the proxy request to the user response
79+ //
80+ // We can copy them to new name
81+ if ( userReq . headers [ 'x-custom-header' ] ) {
82+ headers [ 'x-proxied-custom-header' ] = userReq . headers [ 'x-custom-header' ] ;
83+ }
84+ if ( userReq . headers [ 'x-user-agent' ] ) {
85+ headers [ 'x-proxied-user-agent' ] = userReq . headers [ 'x-user-agent' ] ;
86+ }
87+
88+ // We can copy them to the same name
89+ headers [ 'x-copied-header-1' ] = userReq . headers [ 'x-copied-header-1' ] ;
90+ headers [ 'x-copied-header-2' ] = userReq . headers [ 'x-copied-header-2' ] ;
91+ return headers ;
92+ }
93+ } ) ) ;
94+
95+ request ( app )
96+ . get ( '/proxy' )
97+ . set ( 'x-custom-header' , 'custom-value' )
98+ . set ( 'x-user-agent' , 'test-agent' )
99+ . set ( 'x-copied-header-1' , 'value1' )
100+ . set ( 'x-copied-header-2' , 'value2' )
101+ . expect ( function ( res ) {
102+ // Verify the original headers were proxied to the response
103+ assert . equal ( res . headers [ 'x-proxied-custom-header' ] , 'custom-value' ) ;
104+ assert . equal ( res . headers [ 'x-proxied-user-agent' ] , 'test-agent' ) ;
105+ assert . equal ( res . headers [ 'x-copied-header-1' ] , 'value1' ) ;
106+ assert . equal ( res . headers [ 'x-copied-header-2' ] , 'value2' ) ;
107+ } )
108+ . end ( done ) ;
109+ } ) ;
77110} ) ;
0 commit comments