UWP Windows.Web.HttpClient假单元测试

时间:2016-04-21 14:40:29

标签: c# unit-testing win-universal-app

我尝试为UWP客户端进行单元测试REST通信逻辑。参考the answer for System.Web.HttpClient,我发现Windows.Net.HttpClient也接受了一个名为IHttpFilter的争论。

所以,我尝试使用IHttpFilter进行自定义回复,但我不知道做出回应的正确方法。

    public class TestFilter : IHttpFilter
    {
        public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
        {
            if (request.Method == HttpMethod.Get)
            {
                // response fake response for GET...
            }
        }

        public void Dispose()
        {
            // do nothing
        }
    }           

单元测试的目标方法如下。

    public async Task<string> PostResult(HttpClient httpClient, string username)
    {
        var json = new JsonObject
        {
            {"Username",
                JsonValue.CreateStringValue(string.IsNullOrEmpty(username) ? CommonKey.UnAuthorizedUserPartitionKey : username)
            },
        };

        var content = new HttpStringContent(json.Stringify());
        content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");

        // I want to make below line testable...
        var response = await httpClient.PostAsync(new Uri(Common.ProcessUrl), content);
        try
        {
            response.EnsureSuccessStatusCode();
            return null;
        }
        catch (Exception exception)
        {
            return exception.Message ?? "EMPTY ERROR MESSAGE";
        }
    }

请注意,与System.Web.HttpClient嘲讽/伪造相关的重复问题并非如此。我问的具体是Windows.Web.HttpClient。我没能实现

请注意,Windows.Web.Http.IHttpClient 内部可访问且HttpClient已被密封。很难做Mock或继承并覆盖它。

1 个答案:

答案 0 :(得分:0)

虽然我同意一些有更好的方法来测试HttpClient调用,但我会回答你如何使用IHttpFilter实现创建“假”响应的问题(System.Runtime.InteropServices.WindowsRuntime是你的朋友)< / p>

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Web.Http;
using Windows.Web.Http.Filters;

namespace Project.UnitTesting
{
    public class FakeResponseFilter : IHttpFilter
    {
        private readonly Dictionary<Uri, HttpResponseMessage> _fakeResponses = new Dictionary<Uri, HttpResponseMessage>();

        public void AddFakeResponse(Uri uri, HttpResponseMessage responseMessage)
        {
            _fakeResponses.Add(uri, responseMessage);
        }

        public void Dispose()
        {
            // Nothing to dispose
        }

        public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
        {
            if (_fakeResponses.ContainsKey(request.RequestUri))
            {
                var fakeResponse = _fakeResponses[request.RequestUri];
                return DownloadStringAsync(fakeResponse);
            }

            // Alternatively, you might want to throw here if a request comes 
            // in that is not in the _fakeResponses dictionary.
            return DownloadStringAsync(new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = request });
        }

        private IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> DownloadStringAsync(HttpResponseMessage message)
        {
            return AsyncInfo.Run(delegate (CancellationToken cancellationToken, IProgress<HttpProgress> progress)
            {
                progress.Report(new HttpProgress());

                try
                {
                    return Task.FromResult(message);
                }
                finally
                {
                    progress.Report(new HttpProgress());
                }

            });
        }
    }
}