@@ -25,6 +25,7 @@ internal class MockHttpMessageHandler : HttpClientHandler
25
25
public IDictionary < string , string > ExpectedRequestHeaders { get ; set ; }
26
26
public IList < string > UnexpectedRequestHeaders { get ; set ; }
27
27
public IDictionary < string , string > UnExpectedPostData { get ; set ; }
28
+ public IDictionary < string , string > NotExpectedQueryParams { get ; set ; }
28
29
public HttpMethod ExpectedMethod { get ; set ; }
29
30
30
31
public Exception ExceptionToThrow { get ; set ; }
@@ -65,7 +66,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
65
66
66
67
Assert . AreEqual ( ExpectedMethod , request . Method ) ;
67
68
68
- ValidateQueryParams ( uri ) ;
69
+ ValidateExpectedQueryParams ( uri ) ;
70
+
71
+ ValidateNotExpectedQueryParams ( uri ) ;
69
72
70
73
await ValidatePostDataAsync ( request ) . ConfigureAwait ( false ) ;
71
74
@@ -80,12 +83,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
80
83
return ResponseMessage ;
81
84
}
82
85
83
- private void ValidateQueryParams ( Uri uri )
86
+ private void ValidateExpectedQueryParams ( Uri uri )
84
87
{
85
88
if ( ExpectedQueryParams != null && ExpectedQueryParams . Any ( ) )
86
89
{
87
90
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 ) ;
89
92
Assert . AreEqual ( ExpectedQueryParams . Count , inputQp . Count , "Different number of query params." ) ;
90
93
foreach ( var key in ExpectedQueryParams . Keys )
91
94
{
@@ -95,6 +98,35 @@ private void ValidateQueryParams(Uri uri)
95
98
}
96
99
}
97
100
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
+
98
130
private async Task ValidatePostDataAsync ( HttpRequestMessage request )
99
131
{
100
132
if ( request . Method != HttpMethod . Get && request . Content != null )
0 commit comments