14
14
15
15
using System ;
16
16
using System . Collections . Generic ;
17
+ using System . Diagnostics ;
17
18
using System . Linq ;
18
19
using System . Net ;
19
20
using System . Net . Http ;
21
+ using System . Net . Http . Headers ;
22
+ using Hyak . Common ;
20
23
using Microsoft . Azure . Test . HttpRecorder ;
21
24
using Microsoft . Azure . Common ;
22
25
using Microsoft . Azure . Common . Extensions . Factories ;
26
29
27
30
namespace Microsoft . WindowsAzure . Commands . Common . Test . Mocks
28
31
{
29
- public class MockClientFactory : ClientFactory
32
+ public class MockClientFactory : IClientFactory
30
33
{
31
34
private readonly bool throwWhenNotAvailable ;
32
35
33
36
public List < object > ManagementClients { get ; private set ; }
34
37
35
38
public MockClientFactory ( IEnumerable < object > clients , bool throwIfClientNotSpecified = true )
36
39
{
40
+ UserAgents = new List < ProductInfoHeaderValue > ( ) ;
37
41
ManagementClients = clients . ToList ( ) ;
38
42
throwWhenNotAvailable = throwIfClientNotSpecified ;
39
43
}
40
44
41
- public override TClient CreateClient < TClient > ( AzureSubscription subscription , AzureEnvironment . Endpoint endpoint )
45
+ public TClient CreateClient < TClient > ( AzureContext context , AzureEnvironment . Endpoint endpoint ) where TClient : ServiceClient < TClient >
46
+ {
47
+ Debug . Assert ( context != null ) ;
48
+
49
+ SubscriptionCloudCredentials creds = AzureSession . AuthenticationFactory . GetSubscriptionCloudCredentials ( context ) ;
50
+ TClient client = CreateCustomClient < TClient > ( creds , context . Environment . GetEndpointAsUri ( endpoint ) ) ;
51
+
52
+ return client ;
53
+ }
54
+
55
+ public TClient CreateClient < TClient > ( AzureSubscription subscription , AzureEnvironment . Endpoint endpoint ) where TClient : ServiceClient < TClient >
42
56
{
43
57
SubscriptionCloudCredentials creds = new TokenCloudCredentials ( subscription . Id . ToString ( ) , "fake_token" ) ;
44
58
if ( HttpMockServer . GetCurrentMode ( ) != HttpRecorderMode . Playback )
@@ -58,7 +72,7 @@ public override TClient CreateClient<TClient>(AzureSubscription subscription, Az
58
72
return CreateCustomClient < TClient > ( creds , endpointUri ) ;
59
73
}
60
74
61
- public override TClient CreateCustomClient < TClient > ( params object [ ] parameters )
75
+ public TClient CreateCustomClient < TClient > ( params object [ ] parameters ) where TClient : ServiceClient < TClient >
62
76
{
63
77
TClient client = ManagementClients . FirstOrDefault ( o => o is TClient ) as TClient ;
64
78
if ( client == null )
@@ -71,7 +85,8 @@ public override TClient CreateCustomClient<TClient>(params object[] parameters)
71
85
}
72
86
else
73
87
{
74
- var realClient = base . CreateCustomClient < TClient > ( parameters ) ;
88
+ var realClientFactory = new ClientFactory ( ) ;
89
+ var realClient = realClientFactory . CreateCustomClient < TClient > ( parameters ) ;
75
90
var newRealClient = realClient . WithHandler ( HttpMockServer . CreateInstance ( ) ) ;
76
91
realClient . Dispose ( ) ;
77
92
return newRealClient ;
@@ -81,7 +96,12 @@ public override TClient CreateCustomClient<TClient>(params object[] parameters)
81
96
return client ;
82
97
}
83
98
84
- public override HttpClient CreateHttpClient ( string serviceUrl , HttpMessageHandler effectiveHandler )
99
+ public HttpClient CreateHttpClient ( string endpoint , ICredentials credentials )
100
+ {
101
+ return CreateHttpClient ( endpoint , ClientFactory . CreateHttpClientHandler ( endpoint , credentials ) ) ;
102
+ }
103
+
104
+ public HttpClient CreateHttpClient ( string serviceUrl , HttpMessageHandler effectiveHandler )
85
105
{
86
106
if ( serviceUrl == null )
87
107
{
@@ -104,5 +124,17 @@ public override HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandle
104
124
105
125
return client ;
106
126
}
127
+
128
+ public void AddAction ( IClientAction action )
129
+ {
130
+ // Do nothing
131
+ }
132
+
133
+ public void RemoveAction ( Type actionType )
134
+ {
135
+ // Do nothing
136
+ }
137
+
138
+ public List < ProductInfoHeaderValue > UserAgents { get ; set ; }
107
139
}
108
140
}
0 commit comments