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