Skip to content

Fixed null ref error in MockClientFactory during test recording #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Hyak.Common;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.Common;
using Microsoft.Azure.Common.Extensions.Factories;
Expand All @@ -26,19 +29,30 @@

namespace Microsoft.WindowsAzure.Commands.Common.Test.Mocks
{
public class MockClientFactory : ClientFactory
public class MockClientFactory : IClientFactory
{
private readonly bool throwWhenNotAvailable;

public List<object> ManagementClients { get; private set; }

public MockClientFactory(IEnumerable<object> clients, bool throwIfClientNotSpecified = true)
{
UserAgents = new List<ProductInfoHeaderValue>();
ManagementClients = clients.ToList();
throwWhenNotAvailable = throwIfClientNotSpecified;
}

public override TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnvironment.Endpoint endpoint)
public TClient CreateClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
{
Debug.Assert(context != null);

SubscriptionCloudCredentials creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context);
TClient client = CreateCustomClient<TClient>(creds, context.Environment.GetEndpointAsUri(endpoint));

return client;
}

public TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
{
SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription.Id.ToString(), "fake_token");
if (HttpMockServer.GetCurrentMode() != HttpRecorderMode.Playback)
Expand All @@ -58,7 +72,7 @@ public override TClient CreateClient<TClient>(AzureSubscription subscription, Az
return CreateCustomClient<TClient>(creds, endpointUri);
}

public override TClient CreateCustomClient<TClient>(params object[] parameters)
public TClient CreateCustomClient<TClient>(params object[] parameters) where TClient : ServiceClient<TClient>
{
TClient client = ManagementClients.FirstOrDefault(o => o is TClient) as TClient;
if (client == null)
Expand All @@ -71,7 +85,8 @@ public override TClient CreateCustomClient<TClient>(params object[] parameters)
}
else
{
var realClient = base.CreateCustomClient<TClient>(parameters);
var realClientFactory = new ClientFactory();
var realClient = realClientFactory.CreateCustomClient<TClient>(parameters);
var newRealClient = realClient.WithHandler(HttpMockServer.CreateInstance());
realClient.Dispose();
return newRealClient;
Expand All @@ -81,7 +96,12 @@ public override TClient CreateCustomClient<TClient>(params object[] parameters)
return client;
}

public override HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandler effectiveHandler)
public HttpClient CreateHttpClient(string endpoint, ICredentials credentials)
{
return CreateHttpClient(endpoint, ClientFactory.CreateHttpClientHandler(endpoint, credentials));
}

public HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandler effectiveHandler)
{
if (serviceUrl == null)
{
Expand All @@ -104,5 +124,17 @@ public override HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandle

return client;
}

public void AddAction(IClientAction action)
{
// Do nothing
}

public void RemoveAction(Type actionType)
{
// Do nothing
}

public List<ProductInfoHeaderValue> UserAgents { get; set; }
}
}