@@ -33,7 +33,6 @@ describe('when userResHeaderDecorator is defined', function () {
33
33
} ) ;
34
34
35
35
it ( 'can delete a header' , function ( done ) {
36
-
37
36
app . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
38
37
userResHeaderDecorator : function ( headers /*, userReq, userRes, proxyReq, proxyRes */ ) {
39
38
delete headers [ 'x-my-secret-header' ] ;
@@ -55,7 +54,6 @@ describe('when userResHeaderDecorator is defined', function () {
55
54
} ) ;
56
55
57
56
it ( 'provides an interface for updating headers' , function ( done ) {
58
-
59
57
app . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
60
58
userResHeaderDecorator : function ( headers /*, userReq, userRes, proxyReq, proxyRes */ ) {
61
59
headers . boltedonheader = 'franky' ;
@@ -75,4 +73,39 @@ describe('when userResHeaderDecorator is defined', function () {
75
73
. end ( done ) ;
76
74
} ) ;
77
75
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
+ } ) ;
78
111
} ) ;
0 commit comments