zoukankan      html  css  js  c++  java
  • .NET webapi 的单元测试

     public abstract class MirAPIUnitTestCommon
        {
            public abstract string GetBaseAddress();
            /// <summary>
            /// CRUD
            /// </summary>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="api"></param>
            /// <returns></returns>
            protected TResult InvokeRequest<TResult, TArguemnt>(string api, HttpMethod httpMethod, TArguemnt arg)
            {
                using (var invoker = CreateMessageInvoker())
                {
                    using (var cts = new CancellationTokenSource())
                    {
                        var request = new HttpRequestMessage(httpMethod, api);if (null != arg)
                        {
                            request.Content = new ObjectContent<TArguemnt>(arg, new JsonMediaTypeFormatter());
                        }
                        HttpClient httpClient = new HttpClient();
                        using (HttpResponseMessage response = httpClient.SendAsync(request, cts.Token).Result)
                        {
                            string result = response.Content.ReadAsStringAsync().Result;
                            return JsonConvert.DeserializeObject<TResult>(result);
                        }
                    }
                }
            }
            private HttpMessageInvoker CreateMessageInvoker()
            {
                var config = new HttpConfiguration();
                WebApiConfig.Register(config);
                var server = new HttpServer(config);
                var messageInvoker = new HttpMessageInvoker(server);
                return messageInvoker;
            }
        }
  • 相关阅读:
    利用js在Table中追加数据
    C#API配置跨域
    C#linq查询DataTable
    erlang格式化输出
    erlang 的源代码保护机制
    MP3格式音频文件结构解析
    使用异步 I/O 大大提高应用程序的性能
    虚拟机安装mac 关键是换引导
    C/C++规则整理
    字节对齐
  • 原文地址:https://www.cnblogs.com/mibing/p/8778226.html
Copyright © 2011-2022 走看看