@@ -10,27 +10,45 @@ import (
1010 "google.golang.org/grpc/metadata"
1111)
1212
13- func TestAnnotateContext (t * testing.T ) {
13+ const (
14+ emptyForwardMetaCount = 2
15+ )
16+
17+ func TestAnnotateContext_WorksWithEmpty (t * testing.T ) {
1418 ctx := context .Background ()
1519
16- request , err := http .NewRequest ("GET" , "http://localhost " , nil )
20+ request , err := http .NewRequest ("GET" , "http://www.example.com " , nil )
1721 if err != nil {
18- t .Fatalf ("http.NewRequest(%q, %q, nil) failed with %v; want success" , "GET" , "http://localhost " , err )
22+ t .Fatalf ("http.NewRequest(%q, %q, nil) failed with %v; want success" , "GET" , "http://www.example.com " , err )
1923 }
24+ // Make sure we set a remote.
25+ request .RemoteAddr = "192.168.0.1:12345"
26+
2027 request .Header .Add ("Some-Irrelevant-Header" , "some value" )
2128 annotated := runtime .AnnotateContext (ctx , request )
22- if annotated != ctx {
23- t .Errorf ("AnnotateContext(ctx, request) = %v; want %v" , annotated , ctx )
29+ md , ok := metadata .FromContext (annotated )
30+ if ! ok || len (md ) != emptyForwardMetaCount {
31+ t .Errorf ("Expected 2 metadata items in context; got %v" , md )
2432 }
33+ }
2534
35+ func TestAnnotateContext_ForwardsGrpcMetadata (t * testing.T ) {
36+ ctx := context .Background ()
37+ request , err := http .NewRequest ("GET" , "http://www.example.com" , nil )
38+ if err != nil {
39+ t .Fatalf ("http.NewRequest(%q, %q, nil) failed with %v; want success" , "GET" , "http://www.example.com" , err )
40+ }
41+ request .RemoteAddr = "192.168.0.1:12345"
42+
43+ request .Header .Add ("Some-Irrelevant-Header" , "some value" )
2644 request .Header .Add ("Grpc-Metadata-FooBar" , "Value1" )
2745 request .Header .Add ("Grpc-Metadata-Foo-BAZ" , "Value2" )
2846 request .Header .Add ("Grpc-Metadata-foo-bAz" , "Value3" )
2947 request .Header .Add ("Authorization" , "Token 1234567890" )
30- annotated = runtime .AnnotateContext (ctx , request )
48+ annotated : = runtime .AnnotateContext (ctx , request )
3149 md , ok := metadata .FromContext (annotated )
32- if ! ok || len (md ) != 3 {
33- t .Errorf ("Expected 3 metadata items in context; got %v" , md )
50+ if ! ok || len (md ) != emptyForwardMetaCount + 3 {
51+ t .Errorf ("Expected 5 metadata items in context; got %v" , md )
3452 }
3553 if got , want := md ["foobar" ], []string {"Value1" }; ! reflect .DeepEqual (got , want ) {
3654 t .Errorf (`md["foobar"] = %q; want %q` , got , want )
@@ -42,3 +60,26 @@ func TestAnnotateContext(t *testing.T) {
4260 t .Errorf (`md["authorization"] = %q want %q` , got , want )
4361 }
4462}
63+
64+ func TestAnnotateContext_XForwardedFor (t * testing.T ) {
65+ ctx := context .Background ()
66+ request , err := http .NewRequest ("GET" , "http://bar.foo.example.com" , nil )
67+ if err != nil {
68+ t .Fatalf ("http.NewRequest(%q, %q, nil) failed with %v; want success" , "GET" , "http://bar.foo.example.com" , err )
69+ }
70+ request .Header .Add ("X-Forwarded-For" , "192.168.0.100" ) // client
71+ request .RemoteAddr = "8.8.8.8:12345" // proxy
72+
73+ annotated := runtime .AnnotateContext (ctx , request )
74+ md , ok := metadata .FromContext (annotated )
75+ if ! ok || len (md ) != emptyForwardMetaCount {
76+ t .Errorf ("Expected 2 metadata items in context; got %v" , md )
77+ }
78+ if got , want := md ["x-forwarded-host" ], []string {"bar.foo.example.com" }; ! reflect .DeepEqual (got , want ) {
79+ t .Errorf ("md[\" host\" ] = %v; want %v" , got , want )
80+ }
81+ // Note: it must be in order client, proxy1, proxy2
82+ if got , want := md ["x-forwarded-for" ], []string {"192.168.0.100, 8.8.8.8" }; ! reflect .DeepEqual (got , want ) {
83+ t .Errorf ("md[\" x-forwarded-for\" ] = %v want %v" , got , want )
84+ }
85+ }
0 commit comments