1
- // ----------------------------------------------------------------------------------
2
- //
3
- // Copyright Microsoft Corporation
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
- // ----------------------------------------------------------------------------------
14
-
15
- using Microsoft . Azure . Management . Internal . Resources . Utilities . Models ;
1
+ // ----------------------------------------------------------------------------------
2
+ //
3
+ // Copyright Microsoft Corporation
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ // ----------------------------------------------------------------------------------
14
+
16
15
using Microsoft . Azure . Test . HttpRecorder ;
17
16
using System ;
18
17
using System . Collections . Generic ;
19
- using System . Diagnostics ;
20
18
using System . Linq ;
21
19
using System . Text ;
22
20
using System . Text . RegularExpressions ;
23
21
24
22
namespace Microsoft . WindowsAzure . Commands . ScenarioTest
25
23
{
26
- // Excludes api version when matching mocked records.
24
+ // Excludes api version when matching mocked records.
27
25
// If alternate api version is provided, uses that to match records else removes the api-version matching.
28
26
public class PermissiveRecordMatcherWithApiExclusion : IRecordMatcher
29
27
{
30
28
protected bool _ignoreGenericResource ;
31
29
protected Dictionary < string , string > _providersToIgnore ;
32
30
protected Dictionary < string , string > _userAgentsToIgnore ;
33
- protected string [ ] _resourcesToIgnore ;
34
31
35
32
public PermissiveRecordMatcherWithApiExclusion ( bool ignoreResourcesClient , Dictionary < string , string > providers )
36
33
{
@@ -48,30 +45,18 @@ public PermissiveRecordMatcherWithApiExclusion(
48
45
_userAgentsToIgnore = userAgents ;
49
46
}
50
47
51
- public PermissiveRecordMatcherWithApiExclusion (
52
- bool ignoreResourcesClient ,
53
- Dictionary < string , string > providers ,
54
- Dictionary < string , string > userAgents ,
55
- string [ ] resourcesToIgnore )
56
- {
57
- _ignoreGenericResource = ignoreResourcesClient ;
58
- _providersToIgnore = providers ;
59
- _userAgentsToIgnore = userAgents ;
60
- _resourcesToIgnore = resourcesToIgnore ;
61
- }
62
-
63
48
public virtual string GetMatchingKey ( System . Net . Http . HttpRequestMessage request )
64
49
{
65
- var requestUri = request . RequestUri . PathAndQuery ;
66
- if ( requestUri . Contains ( "?&" ) )
50
+ var path = request . RequestUri . PathAndQuery ;
51
+ if ( path . Contains ( "?&" ) )
67
52
{
68
- requestUri = requestUri . Replace ( "?&" , "?" ) ;
53
+ path = path . Replace ( "?&" , "?" ) ;
69
54
}
70
55
71
56
string version ;
72
- if ( ContainsIgnoredProvider ( requestUri , out version ) )
57
+ if ( ContainsIgnoredProvider ( path , out version ) )
73
58
{
74
- requestUri = RemoveOrReplaceApiVersion ( requestUri , version ) ;
59
+ path = RemoveOrReplaceApiVersion ( path , version ) ;
75
60
}
76
61
else if ( _userAgentsToIgnore != null && _userAgentsToIgnore . Any ( ) )
77
62
{
@@ -82,62 +67,46 @@ public virtual string GetMatchingKey(System.Net.Http.HttpRequestMessage request)
82
67
{
83
68
if ( agent . Value . Any ( v => v . StartsWith ( userAgnet . Key , StringComparison . OrdinalIgnoreCase ) ) )
84
69
{
85
- requestUri = RemoveOrReplaceApiVersion ( requestUri , userAgnet . Value ) ;
70
+ path = RemoveOrReplaceApiVersion ( path , userAgnet . Value ) ;
86
71
break ;
87
72
}
88
73
}
89
74
}
90
75
}
91
76
92
- var encodedPath = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( requestUri ) ) ;
77
+ var encodedPath = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( path ) ) ;
93
78
return string . Format ( "{0} {1}" , request . Method , encodedPath ) ;
94
79
}
95
80
96
81
public virtual string GetMatchingKey ( RecordEntry recordEntry )
97
82
{
98
83
var encodedPath = recordEntry . EncodedRequestUri ;
99
- var requestUri = recordEntry . RequestUri ;
84
+ var path = recordEntry . RequestUri ;
100
85
var changed = false ;
101
- if ( requestUri . Contains ( "?&" ) )
86
+ if ( path . Contains ( "?&" ) )
102
87
{
103
- requestUri = recordEntry . RequestUri . Replace ( "?&" , "?" ) ;
88
+ path = recordEntry . RequestUri . Replace ( "?&" , "?" ) ;
104
89
changed = true ;
105
90
}
106
91
107
92
string version ;
108
- if ( ContainsIgnoredProvider ( requestUri , out version ) )
93
+ if ( ContainsIgnoredProvider ( path , out version ) )
109
94
{
110
- requestUri = RemoveOrReplaceApiVersion ( requestUri , version ) ;
95
+ path = RemoveOrReplaceApiVersion ( path , version ) ;
111
96
changed = true ;
112
97
}
113
98
114
99
if ( changed )
115
100
{
116
- encodedPath = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( requestUri ) ) ;
101
+ encodedPath = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( path ) ) ;
117
102
}
118
103
119
104
return string . Format ( "{0} {1}" , recordEntry . RequestMethod , encodedPath ) ;
120
105
}
121
106
122
- public bool ContainsIgnoredProvider ( string requestUri , out string version )
107
+ protected bool ContainsIgnoredProvider ( string requestUri , out string version )
123
108
{
124
- return ContainsIgnoredProvider ( requestUri , _ignoreGenericResource , _providersToIgnore , _resourcesToIgnore , apiVersion : out version ) ;
125
- }
126
-
127
- /// <summary>
128
- /// Helper method to determine whether or not this request api version should be ignored
129
- /// </summary>
130
- /// <param name="requestUri">The request uri</param>
131
- /// <param name="apiVersion">The api verison to use</paraam>
132
- /// <returns></returns>
133
- public static bool ContainsIgnoredProvider (
134
- string requestUri ,
135
- bool shouldIgnoreGenericResource ,
136
- Dictionary < string , string > providersToIgnore ,
137
- string [ ] resourcesToIgnore ,
138
- out string apiVersion )
139
- {
140
- if ( shouldIgnoreGenericResource &&
109
+ if ( _ignoreGenericResource &&
141
110
! requestUri . Contains ( "providers/" ) &&
142
111
! requestUri . StartsWith ( "/certificates?" , StringComparison . InvariantCultureIgnoreCase ) &&
143
112
! requestUri . StartsWith ( "/pools" , StringComparison . InvariantCultureIgnoreCase ) &&
@@ -147,68 +116,22 @@ public static bool ContainsIgnoredProvider(
147
116
! requestUri . Contains ( "/servicePrincipals?" ) &&
148
117
! requestUri . StartsWith ( "/webhdfs/v1/?aclspec" , StringComparison . InvariantCultureIgnoreCase ) )
149
118
{
150
- apiVersion = String . Empty ;
119
+ version = String . Empty ;
151
120
return true ;
152
121
}
153
122
154
- // Ignore resource providers
155
- foreach ( var provider in providersToIgnore )
123
+ foreach ( var provider in _providersToIgnore )
156
124
{
157
125
var providerString = string . Format ( "providers/{0}" , provider . Key ) ;
158
126
if ( requestUri . Contains ( providerString ) )
159
127
{
160
- apiVersion = provider . Value ;
128
+ version = provider . Value ;
161
129
return true ;
162
130
}
163
131
}
164
132
165
- if ( resourcesToIgnore != null && resourcesToIgnore . Any ( ) )
166
- {
167
- // If we're looking at a specific provider and we have top level resource from this provider to ignore
168
- foreach ( var resourceToIgnore in resourcesToIgnore )
169
- {
170
- string [ ] segments = requestUri . Split ( new char [ ] { '/' } , options : StringSplitOptions . RemoveEmptyEntries ) ;
171
-
172
- // /subscriptions/.../resourceGroups/.../providers/Microsoft.X/resourceType...?api-version=Y
173
- var regex = new Regex ( @"\/subscriptions\/[0-9A-Fa-f-]*\/resourceGroups\/[a-zA-Z0-9_-]*\/providers\/[a-zA-Z0-9_-]*.[a-zA-Z0-9_-]*\/.*[?api-version=[0-9-]*]?" ) ;
174
- if ( regex . IsMatch ( requestUri ) )
175
- {
176
- if ( segments . Length > 7 )
177
- {
178
- var resourceIdentifier = new ResourceIdentifier ( requestUri ) ;
179
- if ( resourceIdentifier . ResourceType == resourceToIgnore )
180
- {
181
- apiVersion = String . Empty ;
182
- return true ;
183
- }
184
- }
185
- else if ( segments . Length == 7 )
186
- {
187
- var resourceType = $ "{ segments [ 5 ] } /{ segments [ 6 ] } ";
188
- if ( resourceType . Contains ( resourceToIgnore ) )
189
- {
190
- apiVersion = String . Empty ;
191
- return true ;
192
- }
193
- }
194
- }
195
-
196
- // /subscriptions/.../providers/Microsoft.Provider/resourceType
197
- regex = new Regex ( @"\/subscriptions\/[0-9A-Fa-f-]*\/providers\/[a-zA-Z0-9_-]*.[a-zA-Z0-9_-]*\/.*?[api-version=[0-9-]*]?" ) ;
198
- if ( regex . IsMatch ( requestUri ) )
199
- {
200
- var resourceType = $ "{ segments [ 3 ] } /{ segments [ 4 ] } ";
201
- if ( resourceType . Contains ( resourceToIgnore ) )
202
- {
203
- apiVersion = String . Empty ;
204
- return true ;
205
- }
206
- }
207
- }
208
- }
209
-
210
133
211
- apiVersion = string . Empty ;
134
+ version = string . Empty ;
212
135
return false ;
213
136
}
214
137
0 commit comments