@@ -29,69 +29,68 @@ private static class EventIds
2929 {
3030 { null , null } ,
3131 { "http://q.app/foo" , "http://q.app/foo" } ,
32+ { "http://q.app:123/foo" , "http://q.app:123/foo" } ,
3233 { "http://user:xxx@q.app/foo" , "http://q.app/foo" } , // has user info
3334 { "http://q.app/foo?" , "http://q.app/foo?" } ,
3435 { "http://q.app/foo?XXX" , "http://q.app/foo?*" } ,
3536 { "http://q.app/a/b/c?a=b%20c&x=1" , "http://q.app/a/b/c?*" } ,
36- { "http://q.app/#" , "http://q.app/#" } , // Has empty fragment.
37- { "http://q.app#f" , "http://q.app/#f" } , // Has fragment.
38- { "http://q.app#f?a=b" , "http://q.app/#f?a=b" } , // Has fragment with a '?'.
39- { "http://q.app/?a=b#f?a=b" , "http://q.app/?*#f?a=b" } , // Has query and fragment with a '?'.
40- { "http://q.app?#f" , "http://q.app/?#f" } , // Has empty query and fragment.
37+ { "http://q.app:4242/a/b/c?a=b%20c&x=1" , "http://q.app:4242/a/b/c?*" } ,
4138 { "/cat/1/2" , "*" } , // Relative Uris are fully redacted.
4239 { "/cat/1/2?a=b%20c&x=1" , "*" } ,
4340 } ;
4441
4542 [ Theory ]
4643 [ MemberData ( nameof ( GetRedactedUriString_Data ) ) ]
47- public void GetRedactedUriString ( string original , string expected )
44+ public void GetRedactedUriString_RedactsUriByDefault ( string original , string expected )
4845 {
4946 Uri ? uri = original != null ? new Uri ( original , UriKind . RelativeOrAbsolute ) : null ;
5047 string ? actual = LogHelper . GetRedactedUriString ( uri ) ;
5148
5249 Assert . Equal ( expected , actual ) ;
5350 }
5451
55- public static TheoryData < bool , bool , string > Handlers_LogExpectedUri_Data ( )
52+ [ ConditionalTheory ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
53+ [ InlineData ( "AppCtx" ) ] // AppContext switch System.Net.Http.DisableUriRedaction = true
54+ [ InlineData ( "EnvVar1" ) ] // Env. var DOTNET_SYSTEM_NET_DISABLEURIREDACTION = "1"
55+ [ InlineData ( "EnvVarTrue" ) ] // Env. var DOTNET_SYSTEM_NET_DISABLEURIREDACTION = "true"
56+ public void GetRedactedUriString_DisableUriRedaction_DoesNotRedactUri ( string queryRedactionDisabler )
5657 {
57- TheoryData < bool , bool , string > result = new ( ) ;
58- bool [ ] booleans = { true , false } ;
59- bool [ ] syncApiVals =
60- #if NET
61- booleans ;
62- #else
63- { false } ;
64- #endif
65- foreach ( bool syncApi in syncApiVals )
58+ RemoteExecutor . Invoke ( static queryRedactionDisabler =>
6659 {
67- foreach ( bool scopeHandler in booleans )
60+ switch ( queryRedactionDisabler )
6861 {
69- // valid values for logQueryStringEnabler:
70- // "" - Do not enable query string logging.
71- // "AppCtx" - Enable via AppContext switch.
72- // "EnvVarTrue" - Enable by setting the environment *_DISABLEURIQUERYREDACTION variable to 'true'.
73- // "EnvVar1" - Enable by setting the environment *DISABLEURIQUERYREDACTION variable to '1'.
74- string [ ] lqs = [ "" , "AppCtx" ] ;
75- foreach ( string queryRedactionDisabler in lqs )
76- {
77- result . Add ( syncApi , scopeHandler , queryRedactionDisabler ) ;
78- }
62+ case "AppCtx" :
63+ AppContext . SetSwitch ( "System.Net.Http.DisableUriRedaction" , true ) ;
64+ break ;
65+ case "EnvVarTrue" :
66+ Environment . SetEnvironmentVariable ( "DOTNET_SYSTEM_NET_HTTP_DISABLEURIREDACTION" , "true" ) ;
67+ break ;
68+ case "EnvVar1" :
69+ Environment . SetEnvironmentVariable ( "DOTNET_SYSTEM_NET_HTTP_DISABLEURIREDACTION" , "1" ) ;
70+ break ;
7971 }
80- }
8172
82- result . Add ( false , false , "EnvVarTrue" ) ;
83- result . Add ( false , false , "EnvVar1" ) ;
84- result . Add ( false , true , "EnvVarTrue" ) ;
85- result . Add ( false , true , "EnvVar1" ) ;
73+ Uri [ ] uris = GetRedactedUriString_Data . Select ( a => a [ 0 ] == null ? null : new Uri ( ( string ) a [ 0 ] , UriKind . RelativeOrAbsolute ) ) . ToArray ( ) ;
8674
87- return result ;
75+ foreach ( Uri uri in uris )
76+ {
77+ string ? expected = uri != null ? uri . IsAbsoluteUri ? uri . AbsoluteUri : uri . ToString ( ) : null ;
78+ string ? actual = LogHelper . GetRedactedUriString ( uri ) ;
79+ Assert . Equal ( expected , actual ) ;
80+ }
81+ } , queryRedactionDisabler ) . Dispose ( ) ;
8882 }
8983
9084 [ ConditionalTheory ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
91- [ MemberData ( nameof ( Handlers_LogExpectedUri_Data ) ) ]
92- public async Task Handlers_LogExpectedUri ( bool syncApi , bool scopeHandler , string queryRedactionDisabler )
85+ [ InlineData ( false , false ) ]
86+ [ InlineData ( false , true ) ]
87+ #if NET
88+ [ InlineData ( true , false ) ]
89+ [ InlineData ( true , true ) ]
90+ #endif
91+ public async Task Handlers_LogExpectedUri ( bool syncApi , bool scopeHandler )
9392 {
94- await RemoteExecutor . Invoke ( static async ( syncApiStr , scopeHandlerStr , queryRedactionDisabler ) =>
93+ await RemoteExecutor . Invoke ( static async ( syncApiStr , scopeHandlerStr ) =>
9594 {
9695 bool syncApi = bool . Parse ( syncApiStr ) ;
9796 bool scopeHandler = bool . Parse ( scopeHandlerStr ) ;
@@ -100,19 +99,6 @@ await RemoteExecutor.Invoke(static async (syncApiStr, scopeHandlerStr, queryReda
10099 const string queryString = "term=Western%20Australia" ;
101100 string destinationUri = $ "{ baseUri } ?{ queryString } ";
102101
103- switch ( queryRedactionDisabler )
104- {
105- case "AppCtx" :
106- AppContext . SetSwitch ( "Microsoft.Extensions.Http.DisableUriQueryRedaction" , true ) ;
107- break ;
108- case "EnvVarTrue" :
109- Environment . SetEnvironmentVariable ( "DOTNET_MICROSOFT_EXTENSIONS_HTTP_DISABLEURIQUERYREDACTION" , "True" ) ;
110- break ;
111- case "EnvVar1" :
112- Environment . SetEnvironmentVariable ( "DOTNET_MICROSOFT_EXTENSIONS_HTTP_DISABLEURIQUERYREDACTION" , "1" ) ;
113- break ;
114- }
115-
116102 var sink = new TestSink ( ) ;
117103 var logger = new TestLogger ( "test" , sink , enabled : true ) ;
118104
@@ -134,20 +120,18 @@ await RemoteExecutor.Invoke(static async (syncApiStr, scopeHandlerStr, queryReda
134120 _ = await invoker . SendAsync ( request , default ) ;
135121 }
136122
137- string expectedUri = ! string . IsNullOrEmpty ( queryRedactionDisabler ) ? destinationUri : $ "{ baseUri } ?*";
138-
139123 if ( scopeHandler )
140124 {
141125 var pipelineStartMessage = Assert . Single ( sink . Writes . Where ( m => m . EventId == EventIds . PipelineStart ) ) ;
142- Assert . Equal ( $ "HTTP GET { expectedUri } ", pipelineStartMessage . Scope . ToString ( ) ) ;
143- Assert . Equal ( $ "Start processing HTTP request GET { expectedUri } ", pipelineStartMessage . Message ) ;
126+ Assert . Equal ( $ "HTTP GET { baseUri } ?* ", pipelineStartMessage . Scope . ToString ( ) ) ;
127+ Assert . Equal ( $ "Start processing HTTP request GET { baseUri } ?* ", pipelineStartMessage . Message ) ;
144128 }
145129 else
146130 {
147131 var requestStartMessage = Assert . Single ( sink . Writes . Where ( m => m . EventId == EventIds . RequestStart ) ) ;
148- Assert . Equal ( $ "Sending HTTP request GET { expectedUri } ", requestStartMessage . Message ) ;
132+ Assert . Equal ( $ "Sending HTTP request GET { baseUri } ?* ", requestStartMessage . Message ) ;
149133 }
150- } , syncApi . ToString ( ) , scopeHandler . ToString ( ) , queryRedactionDisabler ) . DisposeAsync ( ) ;
134+ } , syncApi . ToString ( ) , scopeHandler . ToString ( ) ) . DisposeAsync ( ) ;
151135 }
152136
153137 [ ConditionalTheory ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
@@ -170,7 +154,7 @@ await RemoteExecutor.Invoke(static async (syncApiStr, disableUriQueryRedactionSt
170154
171155 if ( disableUriQueryRedaction )
172156 {
173- AppContext . SetSwitch ( "Microsoft.Extensions .Http.DisableUriQueryRedaction " , true ) ;
157+ AppContext . SetSwitch ( "System.Net .Http.DisableUriRedaction " , true ) ;
174158 }
175159
176160 var sink = new TestSink ( ) ;
0 commit comments