@@ -400,3 +400,88 @@ func Test_RoundTrip(t *testing.T) {
400400 t .Errorf ("unexpected round trip error, exp=nil got=%s" , err )
401401 }
402402}
403+
404+ func TestExtraHeadersOptions (t * testing.T ) {
405+ remoteAddr := "8.8.8.8"
406+
407+ tests := map [string ]struct {
408+ options * Options
409+ expExtra map [string ][]string
410+ }{
411+ "if no extra headers set or client IP enabled then expect no extras" : {
412+ options : & Options {
413+ ExtraUserHeaders : nil ,
414+ ExtraUserHeadersClientIPEnabled : false ,
415+ },
416+ expExtra : nil ,
417+ },
418+ "if extra headers set but no client IP enabled then should return added extras" : {
419+ options : & Options {
420+ ExtraUserHeaders : map [string ][]string {
421+ "foo" : []string {"a" , "b" },
422+ "bar" : []string {"c" , "d" , "e" },
423+ },
424+ ExtraUserHeadersClientIPEnabled : false ,
425+ },
426+ expExtra : map [string ][]string {
427+ "Impersonate-Extra-Foo" : []string {"a" , "b" },
428+ "Impersonate-Extra-Bar" : []string {"c" , "d" , "e" },
429+ },
430+ },
431+ "if no extra headers set but client IP enabled then should return added client IP" : {
432+ options : & Options {
433+ ExtraUserHeaders : nil ,
434+ ExtraUserHeadersClientIPEnabled : true ,
435+ },
436+ expExtra : map [string ][]string {
437+ "Impersonate-Extra-Remote-Client-Ip" : []string {"8.8.8.8" },
438+ },
439+ },
440+ "if extra headers set and client IP enabled then should return extra headers and client IP" : {
441+ options : & Options {
442+ ExtraUserHeaders : map [string ][]string {
443+ "foo" : []string {"a" , "b" },
444+ "bar" : []string {"c" , "d" , "e" },
445+ },
446+ ExtraUserHeadersClientIPEnabled : true ,
447+ },
448+ expExtra : map [string ][]string {
449+ "Impersonate-Extra-Foo" : []string {"a" , "b" },
450+ "Impersonate-Extra-Bar" : []string {"c" , "d" , "e" },
451+ "Impersonate-Extra-Remote-Client-Ip" : []string {"8.8.8.8" },
452+ },
453+ },
454+ }
455+
456+ for name , test := range tests {
457+ t .Run (name , func (t * testing.T ) {
458+ p := newTestProxy (t )
459+ p .options = test .options
460+
461+ req := & http.Request {
462+ Header : http.Header {
463+ "Authorization" : []string {"bearer fake-token" },
464+ },
465+ RemoteAddr : remoteAddr ,
466+ }
467+
468+ authResponse := & authenticator.Response {
469+ User : & user.DefaultInfo {
470+ Name : "a-user" ,
471+ Groups : []string {authuser .AllAuthenticated },
472+ },
473+ }
474+
475+ p .fakeToken .EXPECT ().AuthenticateToken (gomock .Any (), "fake-token" ).Return (authResponse , true , nil )
476+
477+ p .fakeRT .expUser = "a-user"
478+ p .fakeRT .expGroup = []string {authuser .AllAuthenticated }
479+ p .fakeRT .expExtra = test .expExtra
480+
481+ _ , err := p .RoundTrip (req )
482+ if err != nil {
483+ t .Errorf ("got unexpected error: %s" , err )
484+ }
485+ })
486+ }
487+ }
0 commit comments