@@ -25,6 +25,7 @@ internal class MockHttpMessageHandler : HttpClientHandler
2525 public IDictionary < string , string > ExpectedRequestHeaders { get ; set ; }
2626 public IList < string > UnexpectedRequestHeaders { get ; set ; }
2727 public IDictionary < string , string > UnExpectedPostData { get ; set ; }
28+ public IDictionary < string , string > NotExpectedQueryParams { get ; set ; }
2829 public HttpMethod ExpectedMethod { get ; set ; }
2930
3031 public Exception ExceptionToThrow { get ; set ; }
@@ -65,7 +66,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
6566
6667 Assert . AreEqual ( ExpectedMethod , request . Method ) ;
6768
68- ValidateQueryParams ( uri ) ;
69+ ValidateExpectedQueryParams ( uri ) ;
70+
71+ ValidateNotExpectedQueryParams ( uri ) ;
6972
7073 await ValidatePostDataAsync ( request ) . ConfigureAwait ( false ) ;
7174
@@ -80,12 +83,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
8083 return ResponseMessage ;
8184 }
8285
83- private void ValidateQueryParams ( Uri uri )
86+ private void ValidateExpectedQueryParams ( Uri uri )
8487 {
8588 if ( ExpectedQueryParams != null && ExpectedQueryParams . Any ( ) )
8689 {
8790 Assert . IsFalse ( string . IsNullOrEmpty ( uri . Query ) , $ "Provided url ({ uri . AbsoluteUri } ) does not contain query parameters as expected.") ;
88- var inputQp = CoreHelpers . ParseKeyValueList ( uri . Query . Substring ( 1 ) , '&' , false , null ) ;
91+ Dictionary < string , string > inputQp = CoreHelpers . ParseKeyValueList ( uri . Query . Substring ( 1 ) , '&' , false , null ) ;
8992 Assert . AreEqual ( ExpectedQueryParams . Count , inputQp . Count , "Different number of query params." ) ;
9093 foreach ( var key in ExpectedQueryParams . Keys )
9194 {
@@ -95,6 +98,35 @@ private void ValidateQueryParams(Uri uri)
9598 }
9699 }
97100
101+ private void ValidateNotExpectedQueryParams ( Uri uri )
102+ {
103+ if ( NotExpectedQueryParams != null && NotExpectedQueryParams . Any ( ) )
104+ {
105+ // Parse actual query params again (or reuse inputQp if you like)
106+ Dictionary < string , string > actualQueryParams = CoreHelpers . ParseKeyValueList ( uri . Query . Substring ( 1 ) , '&' , false , null ) ;
107+ List < string > unexpectedKeysFound = new List < string > ( ) ;
108+
109+ foreach ( KeyValuePair < string , string > kvp in NotExpectedQueryParams )
110+ {
111+ // Check if the request's query has this key
112+ if ( actualQueryParams . TryGetValue ( kvp . Key , out string value ) )
113+ {
114+ // Optionally, also check if we care about matching the *value*:
115+ if ( string . Equals ( value , kvp . Value , StringComparison . OrdinalIgnoreCase ) )
116+ {
117+ unexpectedKeysFound . Add ( kvp . Key ) ;
118+ }
119+ }
120+ }
121+
122+ // Fail if any "not expected" key/value pairs were found
123+ Assert . IsTrue (
124+ unexpectedKeysFound . Count == 0 ,
125+ $ "Did not expect to find these query parameter keys/values: { string . Join ( ", " , unexpectedKeysFound ) } "
126+ ) ;
127+ }
128+ }
129+
98130 private async Task ValidatePostDataAsync ( HttpRequestMessage request )
99131 {
100132 if ( request . Method != HttpMethod . Get && request . Content != null )
0 commit comments