Home > database >  "mocking" a class when in development
"mocking" a class when in development

Time:10-20

I have this problem where I need to make an external API call, however when in production I have to use a certificate for the API call and in development I don't really need one. I was wondering how to implement a solution where I can have separation between production code and development code so I don't have to comment the part out and possible forget to change it. Any tips would be appreciated!

public async Task<GetPersonResponse> PostPerson(PersonDto dto) 
{
    try {
        var httpHandler = new HttpClientHandler();

        var certificate = new X509Certificate2(
            Path.Join(_certOptions.Path, _certOptions.Name),
            _certOptions.Password, 
            X509KeyStorageFlags.MachineKeySet);

        httpHandler.ClientCertificates.Add(certificate);

        var client = new HttpClient(httpHandler);

        var soapRequest = GenerateRequest(dto);

        var request = new HttpRequestMessage() {
            RequestUri = new Uri(_options.Value.URL),
            Method = HttpMethod.Post,
            Content = new StringContent(soapRequest.ToString(), Encoding.UTF8, "text/xml"),
        };

        var response = await client.SendAsync(request);
    }
}

CodePudding user response:

Well couple things would help. First you should look at HttpClientFactory which would let you pass in the httpclient and have it already configured correctly - right now you're leaking sockets with the code you showed.

For your more general question of how to have the code handle changing behavior based on environment there are two pretty common approaches.

First is to abstract out the behavior into one interface and implementations for each environment. Then change the implementation based on the environment. So with your web app you would register a specific implementation based on the runtime environment. Then your controller would get the interface in it's constructor.

The second would be to pass in a configuration element that had the runtime info and then conditionally execute the code. In that case the controller would take the configuration element in the constructor.

I would avoid using conditional compilation as that's much harder to test.

CodePudding user response:

You could use conditional preprocessors #if #else #endif like that:

#if DEBUG
    Console.WriteLine("Code will executed only in a debug configuration");
#else
    Console.WriteLine("Executed if not in a debug configuration");
#endif

CodePudding user response:

You already have conditions in your code:

_options.Value.URL

Would it be a problem to create a _options.Value.UseCertificate and put an if around your certificate code?

CodePudding user response:

another approach could be having a configuration setting and use it to condition the use of the certificate

dev config:

<add key="UseCertificate" value="False"/>

prod config:

<add key="UseCertificate" value="True"/>

condition with:

ConfigurationManager.AppSettings["UseCertificate"]
  • Related