public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; var attr = new HttpPostAttribute(); await attr.BeforeRequestAsync(context); Assert.True(context.RequestMessage.Method == HttpMethod.Post); var attr2 = new HttpPostAttribute("/login"); await attr2.BeforeRequestAsync(context); Assert.True(context.RequestMessage.Method == HttpMethod.Post); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.webapi.com/login")); var attr3 = new HttpPostAttribute("http://www.baidu.com"); await attr3.BeforeRequestAsync(context); Assert.True(context.RequestMessage.Method == HttpMethod.Post); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com")); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.mywebapi.com"); context.RequestMessage.Method = HttpMethod.Post; var parameter = context.ApiActionDescriptor.Parameters[0].Clone(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 BeforeRequestAsync_Parameter_Timespan_Test() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); IApiParameterAttribute attr = new TimeoutAttribute(); var parameter = context.ApiActionDescriptor.Parameters[0].Clone(TimeSpan.FromMilliseconds(5)); await attr.BeforeRequestAsync(context, parameter); await Task.Delay(10); var canceled = context.CancellationTokens[0].IsCancellationRequested; Assert.True(canceled); parameter = context.ApiActionDescriptor.Parameters[0].Clone(Guid.NewGuid()); await Assert.ThrowsAsync <HttpApiConfigException>(() => attr.BeforeRequestAsync(context, parameter)); parameter = context.ApiActionDescriptor.Parameters[0].Clone(null); await attr.BeforeRequestAsync(context, parameter); Assert.True(context.CancellationTokens.Count == 1); }
public async Task IApiParameterAttributeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; var parameter = context.ApiActionDescriptor.Parameters[0].Clone("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 = parameter.Clone(null); ((FormFieldAttribute)attr).IgnoreWhenNull = true; await attr.BeforeRequestAsync(context, parameter); body = await context.RequestMessage.Content.ReadAsStringAsync(); Assert.Equal("name=laojiu", body); }
public async Task IApiActionAttributeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; var attr = new FormFieldAttribute("name", "laojiu"); await attr.BeforeRequestAsync(context); var body = await context.RequestMessage.Content.ReadAsStringAsync(); Assert.Equal("name=laojiu", body); // IgnoreWhenNull Test var attr2 = new FormFieldAttribute("age", null) { IgnoreWhenNull = true }; await attr2.BeforeRequestAsync(context); body = await context.RequestMessage.Content.ReadAsStringAsync(); Assert.Equal("name=laojiu", body); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; var parameter = context.ApiActionDescriptor.Parameters[0].Clone(new { name = "laojiu", birthDay = DateTime.Parse("2010-10-10") }); var attr = new JsonContentAttribute(); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); var body = await context.RequestMessage.Content.ReadAsStringAsync(); var options = context.HttpApiConfig.FormatOptions.CloneChange(attr.DateTimeFormat); var target = context.HttpApiConfig.JsonFormatter.Serialize(parameter.Value, options); Assert.True(body == target); }
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 TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; 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 TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; var parameter = context.ApiActionDescriptor.Parameters[0].Clone(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 IApiActionAttributeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(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()); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(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); }
public async Task IApiActionAttributeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig { HttpHost = new Uri("http://www.mywebapi.com") }, apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); var attr = new TagsAttribute("key", "laojiu"); await attr.BeforeRequestAsync(context); Assert.Equal("laojiu", context.Tags.Get("key").As <string>()); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(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")); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(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); }
public async Task StringResultTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("StringAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.mywebapi.com"); context.RequestMessage.Method = HttpMethod.Post; context.ResponseMessage.Content = new StringContent("laojiu"); var attr = new AutoReturnAttribute(); var result = await((IApiReturnAttribute)attr).GetTaskResult(context); Assert.True(result?.ToString() == "laojiu"); }
public async Task Test() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(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); }
public async Task BeforeRequestAsync_Parameter_Double_Test() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); IApiParameterAttribute attr = new TimeoutAttribute(); var parameter = context.ApiActionDescriptor.Parameters[0].Clone(10); await attr.BeforeRequestAsync(context, parameter); await Task.Delay(20); var canceled = context.CancellationTokens[0].IsCancellationRequested; Assert.True(canceled); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); var parameter = context.ApiActionDescriptor.Parameters[0].Clone("http://www.baidu.com"); var attr = new UriAttribute(); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com")); parameter = parameter.Clone("/login"); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login")); }
public async Task Test() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/"); context.RequestMessage.Method = HttpMethod.Post; 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); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.mywebapi.com"); context.RequestMessage.Method = HttpMethod.Post; var parameter = context.ApiActionDescriptor.Parameters[0].Clone(new StringContent("laojiu")); var attr = new HttpContentAttribute(); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); var body = await context.RequestMessage.Content.ReadAsStringAsync(); Assert.True(body == "laojiu"); }
public void BeforeRequestTest() { var method = typeof(IAubTestApi).GetMethod("PostAsync"); var context = new TestActionContext( httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor("http://api.dev/", method, HttpMethod.Post) { Url = new Uri("http://api.dev/api/test/order"), Method = HttpMethod.Post }, null); var attr = new HeaderAttribute(); var parameter = new ApiParameterDescriptor(name: "logkey", value: "1234567", attributes: null); attr.BeforeRequest(context, parameter); context.RequestMessage.Headers.TryGetValues("logkey", out IEnumerable <string> values); Assert.Equal("1234567", values.FirstOrDefault()); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); var attr = new ProxyAttribute("localhost", 5555); await attr.BeforeRequestAsync(context); var handler = context.HttpApiConfig.HttpHandler; 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 Assert.ThrowsAsync <HttpApiConfigException>(() => attr2.BeforeRequestAsync(context)); }
public async Task JsonResultTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("JsonXmlAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.mywebapi.com"); context.RequestMessage.Method = HttpMethod.Post; var model = new Model(); var json = context.HttpApiConfig.JsonFormatter.Serialize(model, context.HttpApiConfig.FormatOptions); context.ResponseMessage.Content = new StringContent(json, Encoding.UTF8, "application/json"); var attr = new AutoReturnAttribute(); var result = await((IApiReturnAttribute)attr).GetTaskResult(context) as Model; Assert.True(model.Name == result.Name && model.Age == result.Age); }
public async Task IApiParameterAttributeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); var parameter = context.ApiActionDescriptor.Parameters[0].Clone(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()); }
public async Task EnsureSuccessStatusCodeTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("JsonXmlAsync"))); context.RequestMessage.RequestUri = new Uri("http://www.mywebapi.com"); context.RequestMessage.Method = HttpMethod.Post; context.ResponseMessage.StatusCode = System.Net.HttpStatusCode.InternalServerError; 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 void BeforeRequestTest() { var method = typeof(IAubTestApi).GetMethod("PostAsync"); var context = new TestActionContext( httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor("http://api.dev/", method, HttpMethod.Post) { Url = new Uri("http://api.dev/api/test/order"), Method = HttpMethod.Post }, null); var attr = new QueryAttribute(); var p1 = new ApiParameterDescriptor(name: "orderid", value: "21", attributes: null); attr.BeforeRequest(context, p1); Assert.True(context.RequestMessage.RequestUri == new Uri("http://api.dev/api/test/order?orderid=21")); var p2 = new ApiParameterDescriptor(name: "date", value: "20191201", attributes: null); attr.BeforeRequest(context, p2); Assert.True(context.RequestMessage.RequestUri == new Uri("http://api.dev/api/test/order?orderid=21&date=20191201")); }