Example #1
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = "http://www.baidu.com";

            var attr = new UrlAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com"));

            parameter.Value = "/login";
            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login"));
        }
Example #2
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new HttpOptionsAttribute();
            await attr.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Options);

            var attr2 = new HttpOptionsAttribute("/login");
            await attr2.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Options);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.webapi.com/login"));

            var attr3 = new HttpOptionsAttribute("http://www.baidu.com");
            await attr3.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Options);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com"));
        }
        public async Task IApiParameterAttributeTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = new { @class = 123, User_Agent = "WebApiClient" };

            var attr = new HeadersAttribute();
            await attr.BeforeRequestAsync(context, parameter);

            context.RequestMessage.Headers.TryGetValues("User-Agent", out IEnumerable <string> values);
            Assert.Equal("WebApiClient", values.FirstOrDefault());

            context.RequestMessage.Headers.TryGetValues("class", out IEnumerable <string> cValues);
            Assert.Equal("123", cValues.FirstOrDefault());
        }
Example #4
0
        public async Task Test()
        {
            string get(string name, string value)
            {
                return($@"Content-Disposition: form-data; name=""{name}""

{HttpUtility.UrlEncode(value, Encoding.UTF8)}");
            }

            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];
            IApiParameterable mulitpartText = new MulitpartText("laojiu");
            await mulitpartText.BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.Contains(get("name", "laojiu"), body);
        }
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = new
            {
                name     = "老 九",
                birthDay = DateTime.Parse("2010-10-10")
            };

            var attr = new FormContentAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            var time   = context.HttpApiConfig.FormatOptions.CloneChange(attr.DateTimeFormat).FormatDateTime(DateTime.Parse("2010-10-10"));
            var target = $"name={HttpUtility.UrlEncode("老 九",Encoding.UTF8 )}&birthDay={HttpUtility.UrlEncode(time, Encoding.UTF8)}";

            Assert.True(body.ToUpper() == target.ToUpper());
        }
        public async Task IApiParameterAttributeTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = "laojiu";

            IApiParameterAttribute attr = new FormFieldAttribute();
            await attr.BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.Equal("name=laojiu", body);

            // IgnoreWhenNull Test
            parameter.Value = null;
            ((FormFieldAttribute)attr).IgnoreWhenNull = true;
            await attr.BeforeRequestAsync(context, parameter);

            body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.Equal("name=laojiu", body);
        }
Example #7
0
        public async Task IApiActionAttributeTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new MulitpartTextAttribute("name", "laojiu");
            await attr.BeforeRequestAsync(context);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.Contains(get("name", "laojiu"), body);


            // IgnoreWhenNull Test
            var attr2 = new MulitpartTextAttribute("age", null)
            {
                IgnoreWhenNull = true
            };
            await attr2.BeforeRequestAsync(context);

            body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.DoesNotContain("age", body);
        }
        public async Task EnsureSuccessStatusCodeTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ResponseMessage     = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound),
                ApiActionDescriptor = ApiDescriptorCache
                                      .GetApiActionDescriptor(typeof(IMyApi)
                                                              .GetMethod("JsonXmlAsync"))
            };

            var model = new Model();
            var xml   = context.HttpApiConfig.XmlFormatter.Serialize(model, Encoding.UTF8);

            context.ResponseMessage.Content = new StringContent(xml, Encoding.UTF8, "application/xml");

            var attr = new AutoReturnAttribute()
            {
                EnsureSuccessStatusCode = true
            };
            await Assert.ThrowsAsync <HttpStatusFailureException>(() => ((IApiReturnAttribute)attr).GetTaskResult(context));
        }
        public async Task XmlResultTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ResponseMessage     = new HttpResponseMessage(System.Net.HttpStatusCode.OK),
                ApiActionDescriptor = ApiDescriptorCache
                                      .GetApiActionDescriptor(typeof(IMyApi)
                                                              .GetMethod("JsonXmlAsync"))
            };

            var model = new Model();
            var xml   = context.HttpApiConfig.XmlFormatter.Serialize(model, Encoding.UTF8);

            context.ResponseMessage.Content = new StringContent(xml, Encoding.UTF8, "application/xml");

            var attr   = new AutoReturnAttribute();
            var result = await((IApiReturnAttribute)attr).GetTaskResult(context) as Model;

            Assert.True(model.Name == result.Name && model.Age == result.Age);
        }
Example #10
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = new
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            };

            var attr = new PathQueryAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);

            var birthday = context.HttpApiConfig.FormatOptions.CloneChange(attr.DateTimeFormat).FormatDateTime(DateTime.Parse("2010-10-10"));
            var target   = new Uri("http://www.webapi.com?name=laojiu&birthDay=" + HttpUtility.UrlEncode(birthday, Encoding.GetEncoding(attr.Encoding)));

            Assert.True(context.RequestMessage.RequestUri == target);
        }
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = new Model
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            };

            var attr = new XmlContentAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            var target = context.HttpApiConfig.XmlFormatter.Serialize(parameter.Value, Encoding.UTF8);

            Assert.True(body == target);
        }
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig       = new HttpApiConfig(),
                RequestMessage      = new HttpApiRequestMessage(),
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new ProxyAttribute("localhost", 5555);
            await attr.BeforeRequestAsync(context);

            var handler = context.HttpApiConfig.HttpClient.Handler;

            Assert.True(handler.UseProxy == true);
            Assert.True(handler.Proxy != null);
            Assert.True(handler.Proxy.Credentials == null);
            Assert.True(handler.Proxy.GetProxy(new Uri("http://www.baidu.com")).Authority == "localhost:5555");

            var attr2 = new ProxyAttribute("localhost", 5555, "laojiu", "123456");
            await attr2.BeforeRequestAsync(context);

            handler = context.HttpApiConfig.HttpClient.Handler;
            Assert.True(handler.UseProxy == true);
            Assert.True(handler.Proxy != null);
            Assert.True(handler.Proxy.GetProxy(new Uri("http://www.baidu.com")).Authority == "localhost:5555");
            Assert.True(handler.Proxy.Credentials != null);
        }
Example #13
0
        /// <summary>
        /// 获取api的描述
        /// </summary>
        /// <param name="method">接口的方法</param>
        /// <param name="parameters">参数集合</param>
        /// <returns></returns>
        protected virtual ApiActionDescriptor GetApiActionDescriptor(MethodInfo method, object[] parameters)
        {
            var actionDescripter = ApiDescriptorCache.GetApiActionDescriptor(method).Clone();

            for (var i = 0; i < actionDescripter.Parameters.Length; i++)
            {
                actionDescripter.Parameters[i].Value = parameters[i];
            }
            return(actionDescripter);
        }
        public void GetApiActionDescriptorTest()
        {
            var method      = typeof(IMyApi).GetMethod("Login");
            var descriptor1 = ApiDescriptorCache.GetApiActionDescriptor(method);
            var descriptor2 = ApiDescriptorCache.GetApiActionDescriptor(method);

            Assert.True(object.ReferenceEquals(descriptor1, descriptor2));

            Assert.True(descriptor1.Name == "Login");
            Assert.True(descriptor1.Parameters.Length == 1);
            Assert.True(descriptor1.Filters.Length == 0);
            Assert.True(descriptor1.Attributes.Length == 0);
        }
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig       = new HttpApiConfig(),
                RequestMessage      = new HttpApiRequestMessage(),
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new TimeoutAttribute(5000);
            await attr.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Timeout == TimeSpan.FromSeconds(5));
        }
Example #16
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage      = new HttpApiRequestMessage(),
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new BasicAuthAttribute("laojiu", "123456");
            await attr.BeforeRequestAsync(context);

            var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes("laojiu:123456"));

            Assert.True(context.RequestMessage.Headers.Authorization.Parameter == auth);
        }
Example #17
0
        public async Task IApiActionAttributeTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new TagsAttribute("key", "laojiu");
            await attr.BeforeRequestAsync(context);

            Assert.Equal("laojiu", context.Tags.Get("key").As <string>());
        }
Example #18
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig       = new HttpApiConfig(),
                RequestMessage      = new HttpApiRequestMessage(),
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            Assert.Throws <ArgumentNullException>(() => new HttpHostAttribute(null));
            Assert.Throws <UriFormatException>(() => new HttpHostAttribute("/"));

            var attr = new HttpHostAttribute("http://www.webapiclient.com");
            await attr.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.webapiclient.com"));
        }
Example #19
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig       = new HttpApiConfig(),
                RequestMessage      = new HttpApiRequestMessage(),
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new TimeoutAttribute(50);
            await attr.BeforeRequestAsync(context);

            await Task.Delay(100);

            var canceled = context.CancellationTokens[0].IsCancellationRequested;

            Assert.True(canceled);
        }
Example #20
0
        public async Task IApiActionAttributeTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new HeaderAttribute("MyHeader", "laojiu");
            await attr.BeforeRequestAsync(context);

            context.RequestMessage.Headers.TryGetValues("MyHeader", out IEnumerable <string> values);
            Assert.Equal("laojiu", values.First());
        }
Example #21
0
        public async Task Test()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter             = context.ApiActionDescriptor.Parameters[0];
            IApiParameterable timeout = new Timeout(5000);
            await timeout.BeforeRequestAsync(context, parameter);

            Assert.True(context.RequestMessage.Timeout == TimeSpan.FromMilliseconds(5000));
        }
Example #22
0
        public async Task Test()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];
            IApiParameterable formField = new FormField("laojiu");
            await formField.BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.Equal("name=laojiu", body);
        }
Example #23
0
        public async Task StringResultTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK),
                ApiActionDescriptor = ApiDescriptorCache
                .GetApiActionDescriptor(typeof(IMyApi)
                .GetMethod("StringAsync"))
            };
            context.ResponseMessage.Content = new StringContent("laojiu");

            var attr = new AutoReturnAttribute();
            var result = await ((IApiReturnAttribute)attr).GetTaskResult(context);
            Assert.True(result?.ToString() == "laojiu");
        }
Example #24
0
        public async Task Test()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.mywebapi.com"),
                    Method     = HttpMethod.Post
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter             = context.ApiActionDescriptor.Parameters[0];
            IApiParameterable timeout = new Timeout(50);
            await timeout.BeforeRequestAsync(context, parameter);

            await Task.Delay(100);

            var canceled = context.CancellationTokens[0].IsCancellationRequested;

            Assert.True(canceled);
        }
Example #25
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                HttpApiConfig  = new HttpApiConfig(),
                RequestMessage = new HttpApiRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var parameter = context.ApiActionDescriptor.Parameters[0];

            parameter.Value = new StringContent("laojiu");
            var attr = new HttpContentAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            Assert.True(body == "laojiu");
        }